Skip to content

Instantly share code, notes, and snippets.

@mgreenegit
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgreenegit/135893b4018baeed7f25 to your computer and use it in GitHub Desktop.
Save mgreenegit/135893b4018baeed7f25 to your computer and use it in GitHub Desktop.
Connect to Azure VM using WinRM
function Connect {
param (
[parameter (mandatory = $true)]
[system.string]$vm,
[parameter (mandatory = $true)]
[system.string]$service,
[parameter (mandatory = $true, valuefrompipeline = $true)]
[pscredential]$cred
)
$Services = Get-AzureService | % ServiceName
if ($Services -notcontains $Service) {throw "The Azure account does not contain a service '$service'"}
$vmData = Get-AzureVM -Name $VM -ServiceName $Service
if ($vmData.GuestAgentStatus.Status -ne 'Ready') {throw $($vmData.GuestAgentStatus.FormattedMessage.Message)}
$certs = (get-childitem Cert:\CurrentUser\Root | % {$_.Subject.split(',')[0].replace('CN=','')})
if ($certs -notcontains "$service.cloudapp.net") {
$guid = [system.guid]::NewGuid().guid
$cert = Get-AzureCertificate -ServiceName $service -Thumbprint $vmData.VM.DefaultWinRmCertificateThumbprint -ThumbprintAlgorithm sha1
$cert.data | out-file "$env:temp\$guid.tmp"
$certResult = Import-Certificate -FilePath "$env:temp\$guid.tmp" -CertStoreLocation Cert:\CurrentUser\Root
Remove-Item "$env:temp\$guid.tmp"
}
if (!$cred) {$cred = Get-Credential}
$uri = Get-AzureWinRMUri -ServiceName $service -Name $vm
Enter-PSSession -ConnectionURI $uri.AbsoluteUri -Credential $cred
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment