Created
September 10, 2017 17:23
-
-
Save JarbasHorst/3176028c80b9edb5ca76a5184cc366ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$true)] | |
[String] | |
$VirtualMachineName, | |
[Parameter(Mandatory=$true)] | |
[String] | |
$ResourceGroupName, | |
[Parameter(Mandatory=$true)] | |
[Boolean] | |
$Start | |
) | |
try | |
{ | |
# Supported days. | |
$weekdays = @("Monday","Tuesday","Wednesday","Thursday","Friday") | |
# Checks if current day is a supported day (Monday - Friday). | |
If (-Not ($weekdays -contains (Get-Date).DayOfWeek)) | |
{ | |
Write-Output "Process stopped because current day is not supported." | |
return | |
} | |
# Gets the connection AzureRunAsConnection | |
$conn = Get-AutomationConnection -Name "AzureRunAsConnection" | |
# Logs in to Azure. | |
Write-Output "Logging in to Azure." | |
Add-AzureRMAccount ` | |
-ServicePrincipal ` | |
-Tenant $conn.TenantID ` | |
-ApplicationID $conn.ApplicationID ` | |
-CertificateThumbprint $conn.CertificateThumbprint | |
Write-Output "Successfully logged into Azure." | |
Write-Output "Started getting vm with name: $($VirtualMachineName)" | |
$vm = Get-AzureRmResource ` | |
-ResourceName $VirtualMachineName ` | |
-ResourceGroupName $ResourceGroupName ` | |
-ResourceType "Microsoft.Compute/virtualMachines" | |
# Starts or stops vm depending on user input. | |
if($Start){ | |
Write-Output "Starting $($vm.Name)"; | |
Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName; | |
} | |
else{ | |
Write-Output "Stopping $($vm.Name)"; | |
Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force; | |
} | |
} | |
catch { | |
Write-Error -Message $_.Exception | |
throw $_.Exception | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment