Last active
December 23, 2015 08:49
-
-
Save mikeplate/6610159 to your computer and use it in GitHub Desktop.
Start Remote Desktop Client against a locally running Hyper-V virtual machine by finding out the actual IP number
This file contains hidden or 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
| Param ( | |
| [string]$VirtualMachineName, | |
| [switch]$FullScreen, | |
| [string]$Width = '1600', | |
| [string]$Height = '900' | |
| ) | |
| function WaitAndExit() { | |
| Write-Host "Press any key to exit script" | |
| $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null | |
| Exit | |
| } | |
| if (-not (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) { | |
| Write-Host "This script must be run as Administrator in order to find virtual machines" | |
| WaitAndExit | |
| } | |
| $vm = Get-VM -Name $VirtualMachineName -ErrorAction SilentlyContinue | |
| if ($vm -eq $null) { | |
| Write-Host "No machine with name $VirtualMachineName was found" | |
| Write-Host "Note that script must run as Administrator to see virtual machines" | |
| WaitAndExit | |
| } | |
| if ($vm.State -eq 'Off') { | |
| Start-VM $vm | |
| } | |
| if ($vm.NetworkAdapters.Length -eq 0) { | |
| Write-Host "No network adapters found in machine with name $VirtualMachineName" | |
| WaitAndExit | |
| } | |
| $maxWaitCount = 10 | |
| while ($vm.NetworkAdapters[0].IPAddresses.Length -eq 0 -and $maxWaitCount -gt 0) { | |
| Write-Host "Waiting for IP address to appear in $VirtualMachineName ($maxWaitCount)" | |
| Start-Sleep -s 5 | |
| $maxWaitCount-- | |
| } | |
| $ip = $vm.NetworkAdapters[0].IPAddresses[0] | |
| if ($ip -eq $null) { | |
| Write-Host "No network adapter or IP address found" | |
| Write-Host "Check that the machine is started and has completed booting" | |
| WaitAndExit | |
| } | |
| if ($FullScreen -eq $true) { | |
| Start-Process mstsc -ArgumentList "/v:$ip /f" | |
| } | |
| else { | |
| Start-Process mstsc -ArgumentList "/v:$ip /w:$Width /h:$Height" -WindowStyle Maximized | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment