First you have to identify the XML element that you need to retrieve from the response. This may change depending on the CLI command that you use. To identify the element, you can open the below URL (update the highlighted fields as required) in a web browser and it will show the response XML in the browser.
https://
pam.mydomain.com/cspm/servlet/adminCLI?adminUserID=
cliuser&adminPassword=
clit3st&cmdName=
searchTargetAccounts&
TargetAccount.userName=testNote: If your PAM hostname doesn't match with the SSL cert, then you need to use PowerShell v6.0 or above to make use of the "SkipCertificateCheck" option with
Invoke-RestMethod.Please find the sample PowerShell code below.
$pamServer = "pam.mydomain.com"
$adminUserID = "super"
$adminPassword = "P@ssw0rd"
$authentication = "CSPM"
$request = @{
"adminUserID" = "$adminUserID"
"adminPassword" = "$adminPassword"
"authentication" = "$authentication"
"cmdName" = "searchTargetAccount"
"TargetAccount.userName" = "test"
}
$results = Invoke-RestMethod -Method Get -Uri "https://$pamServer/cspm/servlet/adminCLI" -Body $request -TimeoutSec 30 -SkipCertificateCheck
[xml]$xmlResult = $results.'cw.appMessage'.content.'
if ($xmlResult.CommandResult.'cr.statusCode' -eq "400") {
Write-Host "CLI invocation successful"
$resultElementName='TargetAccount'
foreach ($item in $xmlResult.CommandResult.'cr.result'.$resultElementName) {
Write-host "Username:" $item.userName
Write-Host "TargetApplicationID:"$item.targetApplicationID
}
}
else {
$failureDesc = $xmlResult.CommandResult.'cr.statusDescription'
Write-Host "CLI invocation failed with error - $failureDesc"
}
------------------------------
Thanks,
Shinu
------------------------------
Original Message:
Sent: 07-29-2020 09:56 PM
From: Chris Scott
Subject: Parsing XML via powershell
Hello,
Would you please share any Powershell scripts to parse out xml data from CLI / API
thanks
Chris