我正在研究一个使用 docker 创建 Fedora WSL 的PowerShell script
,这一切都可以工作,但我无法在settings.json
文件中设置图标的代码部分。
JSON 的相关部分:
"profiles":
{
"defaults": {},
"list":
[
{
"commandline": "PATH\TO\WSL",
"guid": "{your-guid}",
"hidden": false,
"name": "fedora",
"icon": "PATH\TO\ICON"
},
{
"commandline": "cmd.exe",
"guid": "{your-guid}}",
"hidden": false,
"name": "Command Prompt"
},
{
"guid": "{your-guid}}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
这是我尝试过的:
$settings = Get-Content $env:localappdata'PackagesMicrosoft.WindowsTerminal_8wekyb3d8bbweLocalStatesettings.json' -raw | ConvertFrom-Json
$settings.profiles.list | % {if($_.name -eq $WSLname){$_.icon=$InstallPathfedora.ico}}
$settings | ConvertTo-Json -depth 32| set-content $env:localappdata'PackagesMicrosoft.WindowsTerminal_8wekyb3d8bbweLocalStatesettings.json'
变量取自脚本第一部分中的参数。
我的目标是检查用户给定输入的配置文件名称是否存在,如果是,则更改或将"icon"属性添加到 fedora.ico 路径。
编辑:脚本的这一部分需要在Windows终端重新启动后运行。
下面介绍如何处理代码的逻辑,以检查icon
属性是否存在并为其分配新值,如果不存在,则使用新值向对象添加新属性。希望内联注释可以帮助您理解逻辑。
$settings = Get-Content 'pathtosettings.json' -Raw | ConvertFrom-Json
# enumerate all objects in `.profiles.list`
foreach($item in $settings.profiles.list) {
# if the `Name` property is not equal to `$WSLName`
if($item.name -ne $WSLname) {
# we can skip this object, just go next
continue
}
# if we're here we know `$item.name` is equal to `$WSLname`
# so we need to check if the `icon` property exists, if it does
if($item.PSObject.Properties.Item('icon')) {
# assign a new value to it
$item.icon = "$InstallPathfedora.ico"
# and go to the next element
continue
}
# if we're here we know `$item.name` is equal to `$WSLname`
# but the `icon` property does not exist, so we need to add it
$item.PSObject.Properties.Add([psnoteproperty]::new('icon', "$InstallPathfedora.ico"))
}
$settings | ConvertTo-Json -Depth 32 | Set-Content 'pathtonewsetting.json'