我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