You can manage your virtual machines directly on PowerShell using the Windows Remote Managing or WinRM and a PowerShell technique called Remote Shell, but locally.. so how you can use the same with you Azure VMs. let’s run this Script and wonder with the potential of this RemoteShell…
<#PSScriptInfo
This runbook sets up a connection to an Azure virtual machine. It requires the Azure virtual machine to
have the Windows Remote Management service enabled, which is the default. It sets up a connection to the Azure
subscription and then imports the certificate used for the Azure VM so remote PowerShell calls can be made to it.
Param()
.SYNOPSIS
Sets up the connection to an Azure VM
This runbook sets up a connection to an Azure virtual machine. It requires the Azure virtual machine to
have the Windows Remote Management service enabled, which is the default. It sets up a connection to the Azure
subscription and then imports the certificate used for the Azure VM so remote PowerShell calls can be made to it.
Name of the Azure subscription to connect to.
.PARAMETER AzureOrgIdCredential
A credential containing an Org Id username / password with access to this Azure subscription.
name of an Azure Automation PSCredential asset instead. Azure Automation will automatically grab the asset with
that name and pass it into the runbook.
Name of the cloud service where the VM is located.
Name of the virtual machine that you want to connect to.
Connect-AzureVM -AzureSubscriptionName «Visual Studio Ultimate with MSDN» -ServiceName «Finance» -VMName «WebServer01» -AzureOrgIdCredential $cred
AUTHOR: System Center Automation Team
LASTEDIT: Dec 18, 2014
#>
workflow Connect-AzureVM
{
[OutputType([System.Uri])]
(
[parameter(Mandatory=$true)]
[String]
$AzureSubscriptionName,
[PSCredential]
$AzureOrgIdCredential,
[parameter(Mandatory=$true)]
[String]
$ServiceName,
[parameter(Mandatory=$true)]
[String]
$VMName
)
Add-AzureAccount -Credential $AzureOrgIdCredential | Write-Verbose
Select-AzureSubscription -SubscriptionName $AzureSubscriptionName | Write-Verbose
# Get the Azure certificate for remoting into this VM
$winRMCert = (Get-AzureVM -ServiceName $Using:ServiceName -Name $Using:VMName | select -ExpandProperty vm).DefaultWinRMCertificateThumbprint
$AzureX509cert = Get-AzureCertificate -ServiceName $Using:ServiceName -Thumbprint $winRMCert -ThumbprintAlgorithm sha1
if ((Test-Path Cert:\LocalMachine\Root\$winRMCert) -eq $false)
{
Write-Progress «VM certificate is not in local machine certificate store – adding it»
$certByteArray = [System.Convert]::fromBase64String($AzureX509cert.Data)
$CertToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList (,$certByteArray)
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store «Root», «LocalMachine»
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($CertToImport)
$store.Close()
}
# Return the WinRM Uri so that it can be used to connect to this VM
Get-AzureWinRMUri -ServiceName $Using:ServiceName -Name $Using:VMName
}
}