Advertisement
Guest User

Untitled

a guest
May 4th, 2019
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Connect to an Azure VM via RDP, starting it if it is not already running
  4.  
  5. .DESCRIPTION
  6. VM credentials are cached to Credential Manager and won't be required after the first connect
  7.  
  8. .PARAMETER Subscription
  9. The Azure Subscription that contains the VMS
  10. .PARAMETER ResourceGroupName
  11. The Azure Resource Group that contains the VM
  12. .PARAMETER VMName
  13. The name of the VM to connect to
  14.  
  15. .EXAMPLE
  16. Connect-AzureVmRDP -Subscription "SUB1" -ResourceGroup "dev" -VMName "MyVM"
  17.  
  18. .FUNCTIONALITY
  19. Azure
  20. #>
  21. function Connect-AzureVmRDP
  22. {
  23. param(
  24. [string]$Subscription,
  25. [string]$ResourceGroupName,
  26. [string]$VMName
  27. )
  28.  
  29. $vm = Start-AzureRmVM -Subscription $Subscription -ResourceGroupName $ResourceGroupName -VMName $VMName
  30.  
  31. Write-Host "Finding IP Address"
  32. $nic = Get-AzureRmNetworkInterface | ? { $_.VirtualMachine.Id -eq $vm.Id }
  33.  
  34. $ipId = $nic.IpConfigurations[0].PublicIpAddress.Id
  35.  
  36. $ipAddress = (Get-AzureRmPublicIpAddress | ?{ $_.Id -eq $ipId }).IpAddress
  37.  
  38. $publicIp = Get-AzureRmPublicIpAddress | ?{ $_.Id -eq $nic.IpConfigurations[0].PublicIpAddress.Id } | select -First 1
  39.  
  40. $vmHost = $publicIp.DnsSettings.Fqdn
  41.  
  42. if (-not $vmHost)
  43. {
  44. $vmHost = $publicIp
  45. }
  46.  
  47. Write-Host "Waiting for RDP port 3349 to be open on $vmHost"
  48. if (-not (Wait-TCPPort -Server $vmHost -Port 3389 -TimeoutSeconds 120))
  49. {
  50. throw "Timed out waiting for RDP to be available on $vmHost"
  51. }
  52.  
  53. Write-Host "Checking for stored credentials"
  54. Assert-StoredCredential $vmHost
  55.  
  56. Write-Host "Connecting to RDP session"
  57. & "start" "mstsc" "/v:$vmHost" | Out-Null
  58.  
  59. # IPs can change, so if we don't have public dns just remove the credential entry rather than filling up the vault with temporary IPs
  60. if ($vmHost -eq $publicIp)
  61. {
  62. Remove-StoredCredential $vmHost
  63. }
  64. }
  65.  
  66. function Start-AzureRmVM
  67. {
  68. param(
  69. [string]$Subscription,
  70. [string]$ResourceGroupName,
  71. [string]$VMName
  72. )
  73.  
  74. if ((Get-AzureRmContext).Subscription.Name -ne $Subscription)
  75. {
  76. Write-Host "Logging into Azure subscription '$Subscription'"
  77.  
  78. Login-AzureRmAccount | Out-Null
  79. Select-AzureRmSubscription -Subscription $Subscription | Out-Null
  80. }
  81.  
  82. Write-Host "Finding VM '$VMName'"
  83.  
  84. $vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -Status
  85.  
  86. if (-not ($vm | ?{ $_.Statuses.Code -eq "PowerState/running" }))
  87. {
  88. Write-Host "Starting VM '$VMName'"
  89.  
  90. Start-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName | Out-Null
  91. }
  92.  
  93. return (Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName)
  94. }
  95.  
  96. function Assert-StoredCredential
  97. {
  98. param($name)
  99.  
  100. if (Test-GenericCredential -name $name)
  101. {
  102. return
  103. }
  104.  
  105. $credential = Get-Credential -Message $name
  106.  
  107. if (-not $credential)
  108. {
  109. throw "Cancelled"
  110. }
  111.  
  112. $networkCredential = $credential.GetNetworkCredential()
  113.  
  114. & 'cmdkey' "/generic:$name" "/user:$($networkCredential.UserName)" "/password:`"$($networkCredential.Password)`"" | Out-Null
  115. }
  116.  
  117. function Remove-StoredCredential
  118. {
  119. param($name)
  120.  
  121. & 'cmdkey' "/delete:$name" | Out-Null
  122. }
  123.  
  124. function Test-GenericCredential {
  125. Param(
  126. [string]$name
  127. )
  128.  
  129. $cmd = (& 'cmdkey' "/list:$name" | Select-Object -Skip 3)
  130.  
  131. return $cmd -ne "* NONE *"
  132. }
  133.  
  134. function Test-TCPConnection
  135. {
  136. param(
  137. [string]$Server,
  138. [int]$Port
  139. )
  140.  
  141.  
  142. $Connection = New-Object System.Net.Sockets.TCPClient
  143.  
  144. try {
  145. $Connection.Connect($Server,$Port)
  146. $Connection.Close()
  147. return $true
  148. }
  149. catch {
  150. return $false
  151. }
  152. }
  153.  
  154. function Wait-TCPPort
  155. {
  156. param(
  157. [string]$Server,
  158. [int]$Port,
  159. [int]$TimeoutSeconds
  160. )
  161.  
  162. $TotalWaited = 0
  163. $SleepSeconds = 2
  164.  
  165. while ($TotalWaited -lt $TimeoutSeconds)
  166. {
  167. if (Test-TCPConnection -Server $Server -Port $Port) {
  168. return $true
  169. }
  170.  
  171. Start-Sleep -Seconds $SleepSeconds
  172. $TotalWaited += $SleepSeconds
  173. }
  174.  
  175. return $false
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement