如何在powershell中选择带有特殊字符的键?

2024-03-18

我有一组来自 JSON 文件的键/值对。

$ $p.dependencies

@architect/architect : ^5.7.0
@architect/functions : ^3.0.4
assert               : ^1.4.1
bcrypt               : ^3.0.6
find-parent-dir      : ^0.3.0
hashids              : ^1.2.2
http-status-codes    : ^1.3.2
lodash.get           : ^4.4.2
mini-web-server      : ^1.0.2
mini-webhook-server  : ^1.0.4
mocha                : ^5.2.0
opencorporates       : ^3.0.0
stripe               : ^6.23.1
uuid                 : ^3.3.2
whois-json           : ^2.0.4

看起来在 Powershell 中它们是notePropertys:

   TypeName: System.Management.Automation.PSCustomObject
Name                 MemberType   Definition
----                 ----------   ----------
Equals               Method       bool Equals(System.Object obj)
GetHashCode          Method       int GetHashCode()
GetType              Method       type GetType()
ToString             Method       string ToString()
@architect/architect NoteProperty string @architect/architect=^5.7.0
@architect/functions NoteProperty string @architect/functions=^3.0.4
assert               NoteProperty string assert=^1.4.1
bcrypt               NoteProperty string bcrypt=^3.0.6
find-parent-dir      NoteProperty string find-parent-dir=^0.3.0
hashids              NoteProperty string hashids=^1.2.2
http-status-codes    NoteProperty string http-status-codes=^1.3.2
lodash.get           NoteProperty string lodash.get=^4.4.2
mini-web-server      NoteProperty string mini-web-server=^1.0.2
mini-webhook-server  NoteProperty string mini-webhook-server=^1.0.4
mocha                NoteProperty string mocha=^5.2.0
opencorporates       NoteProperty string opencorporates=^3.0.0
stripe               NoteProperty string stripe=^6.23.1
uuid                 NoteProperty string uuid=^3.3.2
whois-json           NoteProperty string whois-json=^2.0.4

我想用以下键更改该项目@architect/functions到一个不同于的值^3.0.4(然后将数据保存回来)

下一部分似乎是从哈希表中选择正确的项目。我在用:

$p.dependencies | where $_.key -eq "@architect/functions"

但是这不会返回任何结果。如何用 key 选择项目@architect/functions?对于奖励积分,我如何更改其值!

Edit:

使用答案,如果有人觉得有用的话,这是我的最终脚本。

ls 'src/http' | foreach { 
    $packageJSONFile = "${PSItem}\package.json" 
    $packageJSON = cat $packageJSONFile | convertfrom-json
    if ( $packageJSON.dependencies.'@architect/functions'  ) {
        $packageJSON.dependencies.'@architect/functions' = '^3.0.4'
    }   
    $packageJSON | ConvertTo-Json -depth 100| set-content $packageJSONFile
} 

要获取名称中包含特殊字符的属性,请引用它:

$p.dependencies.'@architect/functions'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在powershell中选择带有特殊字符的键? 的相关文章

随机推荐