PS C:\tmp> invoke-vmssh -command "uname -a" -username $env:defaultVmUser -vmname $env:defaultVmName
Linux debianvm 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux
PS C:\tmp> docker info | grep Server
Server Version: 18.09.1
PS C:\tmp>
Last active
August 29, 2019 19:03
-
-
Save maxwellb/af0665af82960a4feed36c7c1667f960 to your computer and use it in GitHub Desktop.
PowerShell docker wrapper to a Hyper-V VM
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
# Work around an RS5/PSReadline-2.0.0+beta2 bug (Spacebar is not marked 'essential') | |
Set-PSReadlineKeyHandler "Shift+SpaceBar" -ScriptBlock { | |
[Microsoft.Powershell.PSConsoleReadLine]::Insert(' ') | |
} | |
$env:defaultVmName = "My VM" | |
$env:defaultVmUser = "myself" | |
function get-vmip { | |
param( | |
[parameter(position=0,valuefrompipeline=$true)] [string] $vmname | |
) | |
if ($vmname.length -eq 0) { | |
$vmname = (get-vm).name ` | |
| out-gridview -passthru -title "Select a VM to connect to" ` | |
| select -first 1 | |
} | |
("${vmname}" | get-vm | get-vmnetworkadapter).ipaddresses ` | |
| ?{ $_ -like "*.*.*.*" } ` | |
| select -first 1 | |
} | |
function connect-vm { | |
param( | |
[parameter(position=0,valuefrompipeline=$true)] [string] $vmname | |
) | |
& putty.exe (get-vmip $vmname) | |
} | |
function invoke-vmssh { | |
param( | |
[parameter(position=0,mandatory=$true)] [string] $vmname, | |
[parameter(position=1,mandatory=$true,valuefrompipeline=$true)] [string] $command, | |
[parameter()] [string] $username, | |
[parameter()] [switch] $ansi | |
) | |
$vmhost = ("{0}{1}" -f (&{if ($username) { "${username}@" } else { "" }}), (get-vmip $vmname)) | |
if (${ansi}) { | |
& "ansicon.exe" "plink.exe" -t -no-antispoof -no-sanitise-stdout -no-sanitise-stderr $vmhost $command | |
} else { | |
& "plink.exe" -T -no-antispoof -sanitise-stdout -sanitise-stderr $vmhost $command | |
} | |
} | |
function get-dockerports { | |
param( | |
[parameter(position=0,mandatory=$true)] [string] $vmname | |
) | |
$outp = @(invoke-vmssh -username "${env:defaultVmUser}" $vmname "docker ps --format='{{.Image}} {{.Names}} {{.Ports}}'") | |
$outp = @(@($outp | ?{ $_ -like "*0.0.0.0:*" }) -replace "0\.0\.0\.0:(.*)->(.*)/(.*)", "`$1 `$2 `$3") | |
$outp | |
} | |
function connect-vmweb { | |
param( | |
[parameter(position=0,mandatory=$true)] [string] $vmname, | |
[parameter(position=1)] [string] $containerport = "80" | |
) | |
$ip = get-vmip $vmname | |
if ($containerport -eq "443") { $scheme = "https" } else { $scheme = "http" } | |
@(get-dockerports $vmname) -match ".* .* .* ${containerport} tcp" -replace ".* .* (.*) ${containerport} tcp", "`$1" ` | |
| %{ "{0}://{1}:{2}/" -f $scheme, $ip, $_ } ` | |
| %{ start-process $_ } | |
} | |
function docker { | |
invoke-vmssh -username "${env:defaultVmUser}" "${env:defaultVmName}" "docker $($args -join " ")" | |
} | |
function vmbash { | |
invoke-vmssh -username "${env:defaultVmUser}" -ansi "${env:defaultVmName}" "bash -i" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment