| 1 | [cmdletbinding(PositionalBinding = $false)] |
| 2 | param ( |
| 3 | [string]$VmName, |
| 4 | [string]$Username, |
| 5 | [string]$Password, |
| 6 | [ValidateSet("X64", "arm64")][string]$Platform = "X64", |
| 7 | [ValidateSet("Debug", "Release")][string]$BuildType = "Debug", |
| 8 | [string]$RemoteTempFolder = "C:\\", |
| 9 | [string]$BuildOutputPath = [string](Get-Location), |
| 10 | [parameter(ValueFromRemainingArguments = $true)] |
| 11 | [string[]]$ExtraMsiArgs |
| 12 | ) |
| 13 | |
| 14 | $ErrorActionPreference = "Stop" |
| 15 | |
| 16 | if ([string]::IsNullOrEmpty($Password)) { |
| 17 | $SecurePassword = New-Object System.Security.SecureString |
| 18 | } |
| 19 | else { |
| 20 | $SecurePassword = ConvertTo-SecureString "$Password" -AsPlainText -Force |
| 21 | } |
| 22 | |
| 23 | $Credential = New-Object System.Management.Automation.PSCredential("$Username", $SecurePassword) |
| 24 | $Session = New-PSSession -VMName $VmName -Credential $Credential |
| 25 | |
| 26 | $Package = $BuildOutputPath + "/bin/$Platform/$BuildType/wsl.msi" |
| 27 | Copy-Item -ToSession $Session -Path $Package -Destination "$RemoteTempFolder" -Force |
| 28 | |
| 29 | |
| 30 | Invoke-Command -Session $Session -ScriptBlock { |
| 31 | |
| 32 | $MSIArguments = @( |
| 33 | "/i" |
| 34 | "$using:RemoteTempFolder\wsl.msi" |
| 35 | "/qn" |
| 36 | "/norestart" |
| 37 | ) |
| 38 | |
| 39 | if ($using:ExtraMsiArgs) |
| 40 | { |
| 41 | $MSIArguments += $using:ExtraMsiArgs |
| 42 | } |
| 43 | |
| 44 | $exitCode = (Start-Process -Wait "msiexec.exe" -ArgumentList $MSIArguments -NoNewWindow -PassThru).ExitCode |
| 45 | if ($exitCode -Ne 0) |
| 46 | { |
| 47 | Write-Host "Failed to install package: $exitCode" |
| 48 | exit 1 |
| 49 | } |
| 50 | |
| 51 | Write-Host "Package $using:Package successfully deployed on $using:VmName" |
| 52 | } |
| 53 | |
| 54 | Remove-PSSession -Session $Session |