Last active
August 29, 2015 14:18
-
-
Save mgreenegit/135893b4018baeed7f25 to your computer and use it in GitHub Desktop.
Connect to Azure VM using WinRM
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
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