Windows 沙箱 範例組態檔
範例 1 - 對應資料夾,並在沙箱中測試未知的下載檔
下列組態檔可用來輕鬆地測試沙箱內未知下載的檔案。 為了達到這項測試,網路和 vGPU 會停用,而且沙箱允許從主機只讀存取 downloads 資料夾,並放在沙箱的 'temp' 資料夾內。 為了方便起見,登入命令會在啟動時開啟沙箱內的 downloads 資料夾。
Downloads.wsb
<Configuration>
<VGpu>Disable</VGpu>
<Networking>Disable</Networking>
<MappedFolders>
<MappedFolder>
<HostFolder>C:\Users\Public\Downloads</HostFolder>
<SandboxFolder>C:\temp</SandboxFolder>
<ReadOnly>true</ReadOnly>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>explorer.exe C:\temp</Command>
</LogonCommand>
</Configuration>
範例 2 - 在沙箱中啟動時安裝 Visual Studio Code
下列組態檔會在沙箱中安裝 Visual Studio Code,這需要稍微複雜的LogonCommand安裝程式。
兩個資料夾會對應到沙箱中;第一個 (SandboxScripts
) 包含VSCodeInstall.cmd,其會安裝並執行 Visual Studio Code。 假設第二個資料夾 (CodingProjects
) 包含開發人員想要使用 Visual Studio Code 修改的項目檔。
當 Visual Studio Code 安裝程式文本已對應至沙箱時, <LogonCommand>
可以參考它。
VSCodeInstall.cmd
此批處理檔應建立在 C:\SandboxScripts
主機上的目錄中。 它會將 VS Code 下載到 temp
沙箱內的資料夾,並從 temp
資料夾執行安裝。
REM Download Visual Studio Code
curl -L "https://update.code.visualstudio.com/latest/win32-x64-user/stable" --output C:\temp\vscode.exe
REM Install and run Visual Studio Code
C:\temp\vscode.exe /verysilent /suppressmsgboxes
VSCode.wsb
<Configuration>
<MappedFolders>
<MappedFolder>
<HostFolder>C:\SandboxScripts</HostFolder>
<SandboxFolder>C:\temp\sandbox</SandboxFolder>
<ReadOnly>true</ReadOnly>
</MappedFolder>
<MappedFolder>
<HostFolder>C:\CodingProjects</HostFolder>
<SandboxFolder>C:\temp\Projects</SandboxFolder>
<ReadOnly>false</ReadOnly>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>C:\temp\sandbox\VSCodeInstall.cmd</Command>
</LogonCommand>
</Configuration>
範例 3 - 將資料夾對應並執行 PowerShell 腳本作為登入命令
從 Windows 11 24H2 版開始,Windows 沙箱 會遵守主機系統的滑鼠設定。 如果您使用較舊的組建,而且主機系統設定為使用左手滑鼠,則必須在 Windows 沙箱 啟動時手動將這些設定套用 Windows 沙箱。 或者,您可以使用沙箱組態檔來執行登入命令來交換滑鼠設定。
在此範例中 C:\sandbox
,主機上的資料夾會對應至 C:\sandbox
沙箱中的資料夾,因此 SwapMouse.ps1
可以在沙盒組態檔中參考腳本。
SwapMouse.ps1
使用下列程式代碼建立 PowerShell 文稿,並將它儲存在 目錄中 C:\sandbox
作為 SwapMouse.ps1
。
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$SwapButtons = Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool SwapMouseButton(bool swap);
'@ -Name "NativeMethods" -Namespace "PInvoke" -PassThru
$SwapButtons::SwapMouseButton(!([System.Windows.Forms.SystemInformation]::MouseButtonsSwapped))
SwapMouse.wsb
<Configuration>
<MappedFolders>
<MappedFolder>
<HostFolder>C:\sandbox</HostFolder>
<SandboxFolder>C:\sandbox</SandboxFolder>
<ReadOnly>True</ReadOnly>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>powershell.exe -ExecutionPolicy Bypass -File C:\sandbox\SwapMouse.ps1</Command>
</LogonCommand>
</Configuration>