使用PowerShell从变量内容中获取值以进行代码分析



我已经解析了对变量$a的API调用的内容。(下面的内容(,并且我只想解析出在";依赖关系";。有没有办法使用powershell只过滤依赖项?。

{
"name": "1package",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@babel/runtime": {
"version": "7",
"resolved": "https://registry.npmjs.org",
"integrity": "***",
"requires": {
"regenerator-runtime": "^0.13.4"
}
},
"@cloud": {
"version": "2",
"resolved": "https://registry.npmjs.org/",
"integrity": "***"
},
"@cloudnative/health-connect": {
"version": "2",
"resolved": "https://registry.npmjs.org/@***.tgz",
"integrity": "***",
"requires": {
"@cloudnative/health": "^2.1.1"
}
},

所以我只想解析出一个有的列表

babel/runtime version 7
cloud version 2
cloudnative/health-connect version 2

通过访问每个对象的PSObject.Properties,我们可以获得依赖项"名称">和属性"em>"的期望值;版本"和";已解决">。使用Select-Object的计算属性,我们可以构造一个新的对象。

注意,此代码假设您已经在Json字符串上使用了ConvertFrom-Json,并且对象存储在$json变量中。

$json.dependencies.PSObject.Properties | Select-Object Name,
@{
Name = 'Version'
Expression = { $_.Value.Version }
}, @{
Name = 'Resolved'
Expression = { $_.Value.Resolved }
}

输出

Name                        Version Resolved
----                        ------- --------
@babel/runtime              7       https://registry.npmjs.org
@cloud                      2       https://registry.npmjs.org/
@cloudnative/health-connect 2       https://registry.npmjs.org/@***.tgz

相关内容

  • 没有找到相关文章

最新更新