|
| 1 | +#grabbed from: https://gallery.technet.microsoft.com/scriptcenter/Enable-or-disable-auto-c7837c84 |
| 2 | +#thanks to: Floris van der Ploeg |
| 3 | +Function Set-AzVMAutoShutdown{ |
| 4 | + <# |
| 5 | + .SYNOPSIS |
| 6 | + Sets the auto-shutdown property for a virtual machine hosted in Microsoft Azure. |
| 7 | +
|
| 8 | + .DESCRIPTION |
| 9 | + The Set-AzVMAutoShutdown script set the auto-shutdown property for a virtual machine. |
| 10 | +
|
| 11 | + .PARAMETER ResourceGroupName |
| 12 | + Specifies the name of a resource group. |
| 13 | +
|
| 14 | + .PARAMETER Name |
| 15 | + Specifies the name of the virtual machine for which auto-shutdown should be enabled or disabled. |
| 16 | +
|
| 17 | + .PARAMETER Disable |
| 18 | + Sets the auto-shutdown property to disabled. |
| 19 | +
|
| 20 | + .PARAMETER Enable |
| 21 | + Sets the auto-shutdown property to enabled. |
| 22 | +
|
| 23 | + .PARAMETER Time |
| 24 | + The time of day the schedule will occur. |
| 25 | +
|
| 26 | + .PARAMETER TimeZone |
| 27 | + The timezone |
| 28 | +
|
| 29 | + .PARAMETER WebhookUrl |
| 30 | + The webhook URL to which the notification will be sent. |
| 31 | +
|
| 32 | + .PARAMETER Email |
| 33 | + The e-mail address to which the notification will be sent. |
| 34 | +
|
| 35 | + .EXAMPLE |
| 36 | + Set-AzVMAutoShutdown -ResourceGroupName RG-WE-001 -Name MYVM001 -Enable -Time 19:00 |
| 37 | +
|
| 38 | + Enables auto-shutdown on virtual machine MYVM001 in resource group RG-WE-001 and sets the daily shutdown to take place at 19:00. |
| 39 | +
|
| 40 | + .EXAMPLE |
| 41 | + Set-AzVMAutoShutdown -ResourceGroupName RG-WE-001 -Name MYVM001 -Enable -Time 19:00 -TimeZone "W. Europe Standard Time" |
| 42 | +
|
| 43 | + Enables auto-shutdown on virtual machine MYVM001 in resource group RG-WE-001 and sets the daily shutdown to take place at 19:00 in "W. Europe Standard Time" time zone. |
| 44 | +
|
| 45 | + .EXAMPLE |
| 46 | + Set-AzVMAutoShutdown -ResourceGroupName RG-WE-001 -Name MYVM001 -Enable -Time 19:00 -TimeZone "W. Europe Standard Time" -WebhookURL "https://myapp.azurewebsites.net/webhook" |
| 47 | +
|
| 48 | + Enables auto-shutdown on virtual machine MYVM001 in resource group RG-WE-001 and sets the daily shutdown to take place at 19:00 in "W. Europe Standard Time" time zone. Notifications will be enabled and the WebhookURL will be set to "https://myapp.azurewebsites.net/webhook". |
| 49 | +
|
| 50 | + .EXAMPLE |
| 51 | + Set-AzVMAutoShutdown -ResourceGroupName RG-WE-001 -Name MYVM001 -Enable -Time 19:00 -TimeZone "W. Europe Standard Time" -Email "alerts@mycompany.com" |
| 52 | +
|
| 53 | + Enables auto-shutdown on virtual machine MYVM001 in resource group RG-WE-001 and sets the daily shutdown to take place at 19:00 in "W. Europe Standard Time" time zone. Notifications will be enabled and sent to alerts@mycompany.com |
| 54 | +
|
| 55 | + .EXAMPLE |
| 56 | + Set-AzVMAutoShutdown -ResourceGroupName RG-WE-001 -Name MYVM001 -Disable |
| 57 | +
|
| 58 | + Disables auto-shutdown on virtual machine MYVM001 in resource group RG-WE-001. |
| 59 | + #> |
| 60 | + param ( |
| 61 | + [Parameter(Mandatory=$true)][string]$ResourceGroupName, |
| 62 | + [Parameter(Mandatory=$true)][string]$Name, |
| 63 | + [Parameter(ParameterSetName="PsDisable",Mandatory=$true)][switch]$Disable, |
| 64 | + [Parameter(ParameterSetName="PsEnable",Mandatory=$true)][switch]$Enable, |
| 65 | + [Parameter(ParameterSetName="PsEnable",Mandatory=$true)][DateTime]$Time, |
| 66 | + [Parameter(ParameterSetName="PsEnable",Mandatory=$false)][string]$TimeZone = (Get-TimeZone | Select-Object -ExpandProperty Id), |
| 67 | + [Parameter(ParameterSetName="PsEnable",Mandatory=$false)][AllowEmptyString()][string]$WebhookUrl = "", |
| 68 | + [Parameter(ParameterSetName="PsEnable",Mandatory=$false)][string]$Email |
| 69 | + ) |
| 70 | + |
| 71 | + # Check the loaded modules |
| 72 | + $modules = @("Az.Compute", "Az.Resources", "Az.Accounts") |
| 73 | + foreach ($module in $modules) { |
| 74 | + if ((Get-Module -Name $module) -eq $null) { |
| 75 | + Write-Error -Message "PowerShell module '$module' is not loaded" -RecommendedAction "Please download the Azure PowerShell command-line tools from https://azure.microsoft.com/en-us/downloads/" |
| 76 | + return |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + # Check if currently logged-on to Azure |
| 81 | + if ((Get-AzContext).Account -eq $null) { |
| 82 | + Write-Error -Message "No account found in the context. Please login using Login-AzAccount." |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + # Validate the set timezone |
| 87 | + if ( (Get-TimeZone -ListAvailable | Select-Object -ExpandProperty Id) -notcontains $TimeZone) { |
| 88 | + Write-Error -Message "TimeZone $TimeZone is not valid" |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + # Retrieve the VM from the defined resource group |
| 93 | + $vm = Get-AzVm -ResourceGroupName $ResourceGroupName -Name $Name -ErrorAction SilentlyContinue |
| 94 | + if ($vm -eq $null) { |
| 95 | + Write-Error -Message "Virtual machine '$Name' under resource group '$ResourceGroupName' was not found." |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + # Check if Auto-Shutdown needs to be enabled or disabled |
| 100 | + $properties = @{} |
| 101 | + if ($PsCmdlet.ParameterSetName -eq "PsEnable") { |
| 102 | + # Construct the notifications (only enable if webhook is enabled) |
| 103 | + if ([string]::IsNullOrEmpty($WebhookUrl) -and [string]::IsNullOrEmpty($Email)) { |
| 104 | + $notificationsettings = @{ |
| 105 | + "status" = "Disabled"; |
| 106 | + "timeInMinutes" = 30 |
| 107 | + } |
| 108 | + } else { |
| 109 | + $notificationsettings = @{ |
| 110 | + "status" = "Enabled"; |
| 111 | + "timeInMinutes" = 30 |
| 112 | + } |
| 113 | + |
| 114 | + # Add the Webhook URL if defined |
| 115 | + if ([string]::IsNullOrEmpty($WebhookUrl) -ne $true) { $notificationsettings.Add("WebhookUrl", $WebhookUrl) } |
| 116 | + |
| 117 | + # Add the recipient email address if it is defined |
| 118 | + if ([string]::IsNullOrEmpty($Email) -ne $true) { |
| 119 | + $notificationsettings.Add("emailRecipient", $Email) |
| 120 | + $notificationsettings.Add("notificationLocale", "en") |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + # Construct the properties object |
| 125 | + $properties = @{ |
| 126 | + "status" = "Enabled"; |
| 127 | + "taskType" = "ComputeVmShutdownTask"; |
| 128 | + "dailyRecurrence" = @{"time" = ("{0:HHmm}" -f $Time) }; |
| 129 | + "timeZoneId" = $TimeZone; |
| 130 | + "notificationSettings" = $notificationsettings; |
| 131 | + "targetResourceId" = $vm.Id |
| 132 | + } |
| 133 | + } elseif ($PsCmdlet.ParameterSetName -eq "PsDisable") { |
| 134 | + # Construct the properties object |
| 135 | + $properties = @{ |
| 136 | + "status" = "Disabled"; |
| 137 | + "taskType" = "ComputeVmShutdownTask"; |
| 138 | + "dailyRecurrence" = @{"time" = "1900" }; |
| 139 | + "timeZoneId" = (Get-TimeZone).Id; |
| 140 | + "notificationSettings" = @{ |
| 141 | + "status" = "Disabled"; |
| 142 | + "timeInMinutes" = 30 |
| 143 | + }; |
| 144 | + "targetResourceId" = $vm.Id |
| 145 | + } |
| 146 | + } else { |
| 147 | + Write-Error -Message "Unable to determine auto-shutdown action. Use -Enable or -Disable as parameter." |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + # Create the auto-shutdown resource |
| 152 | + try { |
| 153 | + $output = New-AzResource -ResourceId ("/subscriptions/{0}/resourceGroups/{1}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{2}" -f (Get-AzContext).Subscription.Id, $ResourceGroupName, $Name) -Location $vm.Location -Properties $properties -ApiVersion "2017-04-26-preview" -Force -ErrorAction SilentlyContinue |
| 154 | + } catch {} |
| 155 | + |
| 156 | + # Check if resource deployment threw an error |
| 157 | + if ($? -eq $true) { |
| 158 | + # OK, return deployment object |
| 159 | + return $output |
| 160 | + } else { |
| 161 | + # Write error |
| 162 | + Write-Error -Message $Error[0].Exception.Message |
| 163 | + } |
| 164 | + } |
0 commit comments