无法从PowerShell API的Invoke-RestMethod访问数据 [英] Can't access data from Invoke-RestMethod from Powershell API
本文介绍了无法从PowerShell API的Invoke-RestMethod访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经尝试了我能想到的所有方法,但我是PowerShell的新手,一无所获。希望有人能帮忙。
我有一个PowerShell调用:
$appSettings = Invoke-RestMethod -Uri $url -Method Get -Headers $header
如果我登录$appSettings
以增强IDE的销售能力,我会收到:
PS H:> $appSettings
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
但是,我无法访问这些属性中的任何一个。
PS H:> $appSettings.DetailedErrors
不返回任何内容...
PS H:> ConvertTo-Json -InputObject $appSettings
"{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Trace",
PS H:> $appSettings.'Logging'
PS H:> $appSettings | Select -Property Logging
Logging
-------
那么如何获取Logging.LogLevel.Default(意思是我想知道它显示的是&Quot;跟踪&Quot;)??
使用完整错误日志进行更新
PS H:> $AppSettinsJson = $appsettings | ConvertFrom-Json
ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (362): {
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"connectionStrings": {
"Default": "Something"
},
"LockingTimeout": "60", //in seconds,
"Major": "1",
"Minor" : "0",
"buildDate": "#{buildDate}#",
}
At line:1 char:34
+ $AppSettinsJson = $appsettings | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
推荐答案
这与您尝试访问它的方式有关。
$APPSETINGS不是您要使用的。它是返回的JSON字符串$APPSETINGS。
所以,这...
$JsonData = @'
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
'@ | ConvertFrom-Json
$JsonData.DetailedErrors
# Results
<#
True
#>
$JsonData.Logging
# Results
<#
LogLevel
--------
@{Default=Trace; Microsoft=Warning; Microsoft.Hosting.Lifetime=Information}
#>
$JsonData.Logging.LogLevel
# Results
<#
Default Microsoft Microsoft.Hosting.Lifetime
------- --------- --------------------------
Trace Warning Information
#>
所以,如果我重构你所做的。我会这么做:
$appSettings = (Invoke-RestMethod -Uri $url -Method Get -Headers $header) | ConvertFrom-Json
$appSettings.DetailedErrors
# Results
<#
True
#>
$appSettings.Logging
# Results
<#
LogLevel
--------
@{Default=Trace; Microsoft=Warning; Microsoft.Hosting.Lifetime=Information}
#>
$appSettings.Logging.LogLevel
# Results
<#
Default Microsoft Microsoft.Hosting.Lifetime
------- --------- --------------------------
Trace Warning Information
#>
根据您对您正在使用的PS版本的回应进行了更新
*Json cmdlet仅适用于PSv5和更高版本。
# Results
ModuleType Name CmdletName_FinctionName PowerShellVersion
---------- ---- ----------------------- -----------------
Manifest Microsoft.PowerShell.Utility ConvertTo-Json 5.1
Manifest Microsoft.PowerShell.Utility ConvertFrom-Json 5.1
#>
因此您会出错。
如果您坚持使用PSv2,那么您可以这样解析JSON。
# Another approach in Windows 8 and higher
$jsonObjectClass = [Windows.Data.Json.JsonObject,Windows.Data.Json,ContentType=WindowsRuntime]
($jsonObject = $jsonObjectClass::Parse('{"data":["powershell","rocks"]}'))
$jsonObjectClass = [Windows.Data.Json.JsonObject,Windows.Data.Json,ContentType=WindowsRuntime]
($jsonObject = $jsonObjectClass::Parse($JsonData))
更新-您也可以只将字符串保存到文件,然后硬分析该字符串。
Clear-Host
((Get-Content -LiteralPath 'D:Tempappsettings.txt' |
Select-String 'Default' -Context 0) -replace 's+|"|,' -split(':'))[1]
# Results
<#
Trace
#>
这篇关于无法从PowerShell API的Invoke-RestMethod访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文