Repository navigation
Expand file tree
/
Copy pathStart-DevelopmentWindowsSandbox.ps1
More file actions
55 lines (45 loc) · 2.06 KB
/
Copy pathStart-DevelopmentWindowsSandbox.ps1
File metadata and controls
55 lines (45 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
param(
[parameter(Mandatory = $false)][string]$MappedFolder = 'C:\WindowsSandbox',
[parameter(Mandatory = $false)][string]$LogonCommand = 'Install_WinGet_and_Software.ps1'
)
#https://powershellisfun.com/2024/08/02/creating-a-development-windows-sandbox-using-powershell-and-winget/
#Check if Windows Sandbox is already running. Exit if yes
if (Get-Process -Name 'WindowsSandbox' -ErrorAction SilentlyContinue) {
Write-Warning ("Windows Sandbox is already running, exiting...")
return
}
#Validate if $mappedfolder exists
if ($MappedFolder) {
if (Test-Path $MappedFolder -ErrorAction SilentlyContinue) {
Write-Host ("Specified {0} path exists, continuing..." -f $MappedFolder) -ForegroundColor Green
}
else {
Write-Warning ("Specified {0} path doesn't exist, exiting..." -f $MappedFolder)
return
}
}
#Create .wsb config file, overwrite existing file if present and check if specified logoncommand exist
$wsblocation = "$($MappedFolder)\WindowsSandbox.wsb"
if (-not (Test-Path "$($MappedFolder)\$($LogonCommand)")) {
Write-Warning ("Specified LogonCommand {0} doesn't exist in {1}, exiting..." -f $MappedFolder, $LogonCommand)
return
}
Tee-Object -FilePath $wsblocation -Append:$false
$wsb = @()
$wsb += "<Configuration>"
$wsb += "<MappedFolders>"
$wsb += "<MappedFolder>"
$wsb += "<HostFolder>$($MappedFolder)</HostFolder>"
$wsb += "<ReadOnly>true</ReadOnly>"
$wsb += "</MappedFolder>"
$wsb += "</MappedFolders>"
$LogonCommandFull = 'Powershell.exe -ExecutionPolicy bypass -File C:\users\wdagutilityaccount\desktop\' + $(Get-childitem -Path $($wsblocation) -Directory).Directory.Name + '\' + $logoncommand
$wsb += "<LogonCommand>"
$wsb += "<Command>$($LogonCommandFull)</Command>"
$wsb += "</LogonCommand>"
$wsb += "</Configuration>"
#Create sandbox .wsb file in $mappedfolder and start Windows Sandbox using it
$wsb | Out-File $wsblocation -Force:$true
Write-Host ("Saved configuration in {0} and Starting Windows Sandbox..." -f $wsblocation) -ForegroundColor Green
Invoke-Item $wsblocation
Write-Host ("Done!") -ForegroundColor Green