Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created October 22, 2014 08:25
Show Gist options
  • Save joeriks/d5f96c15c59efb7b6b8a to your computer and use it in GitHub Desktop.
Save joeriks/d5f96c15c59efb7b6b8a to your computer and use it in GitHub Desktop.
Azure Powershell to start, stop and remote desktop into VMs
function stopvm($azurecloud,$vmname)
{
if ($vmname -eq $null)
{
Get-AzureVM -ServiceName $azurecloud |Foreach-object {Stop-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force}
}
else
{
Stop-AzureVM -ServiceName $azurecloud -Name $vmname
}
}
function startvm($azurecloud,$vmname)
{
if ($vmname -eq $null)
{
Get-AzureVM -ServiceName $azurecloud |Foreach-object {Start-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force}
}
else
{
Start-AzureVM -ServiceName $azurecloud -Name $vmname
}
}
################################################################
# Please Change These Variables to Suit Your Environment
#
$azuresettings = "C:\users\azuresettingsfile"
$azurecloud = ""
#
#
#
################################################################
write-host "Importing Azure Settings"
Import-AzurePublishSettingsFile $azuresettings
write-host "Choose the options to Start and Stop your Azure VMS"
write-host "1. Start All VMs"
write-host "2. Stop All VMs"
write-host "3. Start One VM"
write-host "4. Stop One VM"
write-host "5. RDP into One VM"
$answer = read-host "Please Select Your Choice"
Switch($answer)
{
1{ $vmname = $null;StartVM $azurecloud $vmname}
2{ $vmname = $null;StopVM $azurecloud $vmname}
3{ $vmname = read-host "Please Enter VM Name";StartVM $azurecloud $vmname}
4{ $vmname = read-host "Please Enter VM Name";StopVM $azurecloud $vmname}
5{
$vmname = read-host "Please Enter VM Name";
$vm = Get-AzureVM -ServiceName $azurecloud -Name $vmName
$rdp = Get-AzureEndpoint -Name "Remote Desktop" -VM $vm
$hostdns = (New-Object "System.Uri" $vm.DNSName).Authority
$port = $rdp.Port
Start-Process "mstsc" -ArgumentList "/V:$hostdns`:$port"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment