繁体   English   中英

从 PowerShell 编辑 Windows 终端配置文件设置 JSON

[英]Edit Windows Terminal profiles settings JSON from PowerShell

我正在研究一个PowerShell script ,该脚本使用 docker 创建 Fedora WSL,一切正常,但我无法使用在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'\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json' -raw | ConvertFrom-Json
$settings.profiles.list | % {if($_.name -eq $WSLname){$_.icon=$InstallPath\fedora.ico}}
$settings | ConvertTo-Json -depth 32| set-content $env:localappdata'\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json'

变量取自脚本第一部分的参数。
我的目标是检查具有用户给定输入的配置文件名称是否存在,如果存在,则将“icon”属性更改或添加到 fedora.ico 路径。

这是您如何处理代码的逻辑以检查icon属性是否存在并为其分配新值,如果不存在,则使用新值将新属性添加到 object。 希望内联注释可以帮助您理解逻辑。

$settings = Get-Content 'path\to\settings.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 = "$InstallPath\fedora.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', "$InstallPath\fedora.ico"))
}
$settings | ConvertTo-Json -Depth 32 | Set-Content 'path\to\newsetting.json'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM