diff --git a/UpdateManagement-TurnOffVms.ps1 b/UpdateManagement-TurnOffVms.ps1 index 7cc86d1..f173181 100644 --- a/UpdateManagement-TurnOffVms.ps1 +++ b/UpdateManagement-TurnOffVms.ps1 @@ -1,7 +1,7 @@ <#PSScriptInfo -.VERSION 1.1 +.VERSION 1.2 .GUID 9606f2a1-49f8-4a67-91d6-23fc6ebf5b3b @@ -67,13 +67,13 @@ param( #This requires a RunAs account $ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection' -Add-AzureRmAccount ` +Add-AzAccount ` -ServicePrincipal ` -TenantId $ServicePrincipalConnection.TenantId ` -ApplicationId $ServicePrincipalConnection.ApplicationId ` -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint -$AzureContext = Select-AzureRmSubscription -SubscriptionId $ServicePrincipalConnection.SubscriptionID +$AzureContext = Select-AzSubscription -SubscriptionId $ServicePrincipalConnection.SubscriptionID #endregion BoilerplateAuthentication #If you wish to use the run context, it must be converted from JSON @@ -94,11 +94,11 @@ if (!$variable) # In order to prevent asking for an Automation Account name and the resource group of that AA, # search through all the automation accounts in the subscription # to find the one with a job which matches our job ID -$AutomationResource = Get-AzureRmResource -ResourceType Microsoft.Automation/AutomationAccounts +$AutomationResource = Get-AzResource -ResourceType Microsoft.Automation/AutomationAccounts foreach ($Automation in $AutomationResource) { - $Job = Get-AzureRmAutomationJob -ResourceGroupName $Automation.ResourceGroupName -AutomationAccountName $Automation.Name -Id $PSPrivateMetadata.JobId.Guid -ErrorAction SilentlyContinue + $Job = Get-AzAutomationJob -ResourceGroupName $Automation.ResourceGroupName -AutomationAccountName $Automation.Name -Id $PSPrivateMetadata.JobId.Guid -ErrorAction SilentlyContinue if (!([string]::IsNullOrEmpty($Job))) { $ResourceGroup = $Job.ResourceGroupName @@ -122,14 +122,14 @@ $vmIds | ForEach-Object { $rg = $split[4]; $name = $split[8]; Write-Output ("Subscription Id: " + $subscriptionId) - $mute = Select-AzureRmSubscription -Subscription $subscriptionId + $mute = Select-AzSubscription -Subscription $subscriptionId - $vm = Get-AzureRmVM -ResourceGroupName $rg -Name $name -Status -DefaultProfile $mute + $vm = Get-AzVM -ResourceGroupName $rg -Name $name -Status -DefaultProfile $mute $state = ($vm.Statuses[1].DisplayStatus -split " ")[1] if($state -in $stoppableStates) { Write-Output "Stopping '$($name)' ..." - $newJob = Start-ThreadJob -ScriptBlock { param($resource, $vmname, $sub) $context = Select-AzureRmSubscription -Subscription $sub; Stop-AzureRmVM -ResourceGroupName $resource -Name $vmname -Force -DefaultProfile $context} -ArgumentList $rg,$name,$subscriptionId + $newJob = Start-ThreadJob -ScriptBlock { param($resource, $vmname, $sub) $context = Select-AzSubscription -Subscription $sub; Stop-AzVM -ResourceGroupName $resource -Name $vmname -Force -DefaultProfile $context} -ArgumentList $rg,$name,$subscriptionId $jobIDs.Add($newJob.Id) }else { Write-Output ($name + ": already stopped. State: " + $state) @@ -152,4 +152,4 @@ foreach($id in $jobsList) } } #Clean up our variables: -Remove-AzureRmAutomationVariable -AutomationAccountName $AutomationAccount -ResourceGroupName $ResourceGroup -name $runID +Remove-AzAutomationVariable -AutomationAccountName $AutomationAccount -ResourceGroupName $ResourceGroup -name $runID