【问题标题】:Get values from Invoke-RestMethod PSobject从 Invoke-RestMethod PSobject 获取值
【发布时间】:2017-07-20 18:45:05
【问题描述】:

我正在使用Invoke-RestMethod 从我们的 HRIS(人力资源信息系统)中提取员工数据:

$employee = Invoke-RestMethod -Method Get -Headers $headers -Uri $URI -ContentType 'application/json'

它返回这个 PSobject,我无法引用这些值:

employees                                                                                                                                                                                                                                                        
---------                                                                                                                                                                                                                                                        
{@{account_id=12345; username=12345; is_locked=False; employee_id=12345; first_name=John; middle_initial=Roger; last_name=Doe; full_name=John Roger Doe}}

我正在尝试提取单个值以在脚本的其余部分中用作变量。


我尝试过的事情:

Write-Output ($employee | Select -ExpandProperty "first_name")

Write-Output $employee.Properties["first_name"].Value


按要求完成脚本

$APIkey = "supersecret"
$KronosAccount = Read-Host -Prompt 'Input your Kronos admin ID'
$KronosPassword = Read-Host -Prompt 'Input your Kronos password' -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($KronosPassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

$loginheaders = @{}
$loginheaders.Add("Api-Key", $APIkey)
$loginheaders.Add("Accept", "application/json")


$json = @{
  credentials = @{
    username = $KronosAccount
    password = $PlainPassword
    company = '123456'
  }
}

$token = Invoke-RestMethod -Method Post -Headers $loginheaders -Uri https://secure3.saashr.com/ta/rest/v1/login -ContentType 'application/json' -Body (ConvertTo-json $json) 
$tokenvalue = ($token | Select -ExpandProperty "token")

$NewEmployeeID = Read-Host -Prompt 'Input the employee ID to create an account for'

$headers = @{}
$headers.Add("Api-Key", $APIkey)
$headers.Add("Accept", "application/json")
$headers.Add("Authentication", "Bearer $tokenvalue")

$URI = "https://secure3.saashr.com/ta/rest/v1/employees/?company=123456&filter=username::$NewEmployeeID"
$employee = Invoke-WebRequest -Method Get -Headers $headers -Uri $URI -ContentType 'application/json'

$employee.employees

【问题讨论】:

    标签: powershell psobject


    【解决方案1】:

    好的,你有一些时髦的东西。您作为 Invoke-RestMethod 的返回结果提供的值实际上是反序列化的 PowerShell 对象,而不是 JSON。它似乎也在某个时候删除了它的引号。

    如果你这样做: $x = @{account_id="12345"; username="12345"; is_locked="False"; employee_id="12345"; first_name="John"; middle_initial="Roger"; last_name="Doe"; full_name="John Roger Doe"}

    那么你可以这样做:

    $x.full_name

    得到你想要的值。我认为您会想要联系托管该 API 的任何人,并让他们在那里解决此问题。

    为了确保这个问题没有被引入客户端,你可以用Invoke-WebRequest替换Invoke-RestRequest(它应该采用所有相同的参数)。然后运行$employee.rawContent 并发布结果。这将使我们确切地知道发生了什么。

    【讨论】:

    • 什么是"funky stuff"?为什么 Invoke-RestMethod 应该返回 JSON 字符串?
    • Invoke-RestMethod 最终不会返回 JSON,它会接收 JSON 并将其解析为 PowerShell 对象。鉴于上面的输出,解析的 PowerShell 对象包含一个反序列化的 PowerShell 对象,只是没有您期望的双引号。如果我对返回 JSON 的 API 执行 Invoke-RestMethod,我最终会得到一个有效的、已解析的 PowerShell 对象。在 Brian 的输出中,他似乎拥有一个包含未解析数据的顶级属性。
    • 由于评论长度而省略了标题。这是来自$employee.rawContent {"employees":[{"account_id":123456,"username":"123456","is_locked":false,"employee_id":"123456","first_name":"JOhn"," middle_initial":"Mac","last_name":"Doe","full_name":"John Mac Doe"}]}`
    • 这很奇怪,因为Invoke-WebRequest 会返回有效的 JSON。我接受了响应字符串并通过ConverFrom-JSON 运行它来进行这样的测试:$x = '{"employees":[{"account_id":123456,"username":"123456","is_l‌​ocked":false,"employ‌​ee_id":"123456","fir‌​st_name":"JOhn","mid‌​dle_initial":"Mac","‌​last_name":"Doe","fu‌​ll_name":"John Mac Doe"}]}' $y = $x | Convert-FromJSON 然后我输出了数据:$y.employees,它工作正常。这表明与Invoke-RestMethod 相关的代码正在弄乱它的内容。你能为我做两件事吗(见下)
    • 1.运行它以确保它不起作用:$x = Invoke-RestMethod -Method Get -Headers $headers -Uri $URI$x.employees 2. 发布您的完整脚本,以便我进一步查看。作为一种解决方法,您没有理由不能使用上面的 Invoke-WebRequestConvertFrom-JSON 代码代替 Invoke-RestMethod
    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多