code365scripts.sandbox.psm1
function Start-WindowsSandbox { [CmdletBinding()] [Alias("sandbox")] param( [string[]]$apps, [Parameter()] [ValidateSet("en", "zh")] [string]$lang = "en" ) # get the path of the sandbox image folder, it might be C:\ProgramData\Microsoft\Windows\Containers\BaseImages\4e8e61c5-f665-425b-be6e-c1a9d8dd358a\BaseLayer\Files\Users\WDAGUtilityAccount in your machin, but the GUID might be different $sandboxImageFolder = Get-ChildItem -Path "C:\ProgramData\Microsoft\Windows\Containers\BaseImages" -Directory | Select-Object -First 1 $sandboxImageFolder = Join-Path $sandboxImageFolder.FullName "BaseLayer\Files\Users\WDAGUtilityAccount" # if user specify the lang parameter, then replace the ntuser.dat file in the sandbox image folder, with the ntuser.dat file in the lang folder, each language has its own ntuser.dat file, the folder name is the language code, like en, zh # if ($PSBoundParameters.ContainsKey("lang")) { # $langFolder = Join-Path $PSScriptRoot "languages\$lang" # $ntuserDatFile = Join-Path $langFolder "ntuser.dat" # Copy-Item -Path $ntuserDatFile -Destination (Join-Path $sandboxImageFolder "ntuser.dat") -Force # } # create a ps1 file in the downloads folder of current user, the name is fixed, sandbox.ps1 $ps1File = Join-Path $env:USERPROFILE "Downloads\sandbox.ps1" Set-Content -Path $ps1File -Value "# This is a temp file, will be used to start the sandbox, generated by code365scripts.sandbox module" # hide this file Set-ItemProperty -Path $ps1File -Name Attributes -Value ([System.IO.FileAttributes]::Hidden) # if user specify the apps parameter, create a temp ps1 in the sandbox folder, and write the code to install those apps in the ps1 file, I will use choco to install all the apps if ($PSBoundParameters.ContainsKey("apps")) { # copy the content of the InstallApps.ps1 file to the temp ps1 file $installAppsFile = Join-Path $PSScriptRoot "InstallApps.ps1" Get-Content -Path $installAppsFile | Add-Content -Path $ps1File # add the install app code to the temp ps1 file Add-Content -Path $ps1File -Value "Install-Apps -apps @('$($apps -join "','")')" } # read the template.wsb, replace the {{downloadfolder}} to the download folder of current user, and {{scriptfile}} to the temp ps1 file $wsbFile = Join-Path $PSScriptRoot "template.wsb" $wsbContent = Get-Content -Path $wsbFile -Raw $wsbContent = $wsbContent -replace "{{downloadfolder}}", "$($env:USERPROFILE)\Downloads" # save the content to the downloads folder of current user, the name is fixed, sandbox.wsb, hide it as well $wsbTempFile = Join-Path $env:USERPROFILE "Downloads\sandbox.wsb" Set-Content -Path $wsbTempFile -Value $wsbContent Set-ItemProperty -Path $wsbTempFile -Name Attributes -Value ([System.IO.FileAttributes]::Hidden) # if lang is zh then copy the code from setchineseime.ps1 to the temp ps1 file, and call the set-chineseime function if ($lang -eq "zh") { $setChineseIMEFile = Join-Path $PSScriptRoot "SetChineseIME.ps1" Get-Content -Path $setChineseIMEFile -Encoding UTF8 | Add-Content -Path $ps1File -Encoding UTF8 Add-Content -Path $ps1File -Value "Set-ChineseIME" } # start the sandbox, just try to run the wsb file Start-Process -FilePath $wsbTempFile } |