我正在工作的PowerShell script,创建了一个Fedora WSL使用docker,它所有的工作,但我不能得到工作的代码部分,设置在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"
            },

以下是我try 过的:

$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'

Variables are taken from params in first part of the script.
My goal is to chech if the profile name with given input by the user exists, if so, changes or adds the "icon" property to the fedora.ico path.

Edit: This part of the script needs to run after windows terminal has been relaunched.

推荐答案

下面是如何处理代码的逻辑,以判断icon属性是否存在并为其分配新值,如果不存在,则使用新值向对象添加新属性.希望内联 comments 能帮助您理解其中的逻辑.

$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'

Json相关问答推荐

使用单元和非单元版本反序列化Rust中的枚举,而无需编写自定义反序列化程序

解析JSON说函数parse_json不存在?

如何在我的响应模型中修复此问题:[期望的值类型为';Map<;Dynamic,Dynamic&>;,但获得的值类型为';NULL&39;]

在Ansible中从json中提取特定数据

使用Kotlin限制序列化类属性的允许整数值

使用 jq,如何将两个属性构成键的对象数组转换为对象的索引对象?

jq EOF 处的无效数字文字将 json 值更新为 0.0.x

jq - 将父键值提取为子元素旁边的逗号分隔值

TSQL FOR JSON 嵌套值

如何为包含一些固定值并可能具有其他附加值的数组字符串创建数组 json 架构

如何使用 Swift 从 NSURLSession 获取 cookie?

在 JSON API Wordpress 上启用 CORS

如何为名称/值 struct 创建 JSON 模式?

在 JSON 对象中强制执行非空字段

将 CoffeeScript 项目转换为 JavaScript(不缩小)?

区分字符串和列表的 Pythonic 方式是什么?

Peewee 模型转 JSON

使用 JSON.NET 序列化/反序列化对象字典

Backbone.js 模型与集合

如何在 postgresql 9.3 中循环 JSON 数组