Curl Command to Powershell 5 with XML

319 views Asked by At

I have poked around a bit and I have found good information, but nothing really well on how to handle custom xml data. The code below is what I have done so far in powershell. I know there is an invoke-restmethod but I am struggling to convert this one.

$username = "tuser"
$Date = (Get-Date).AddDays(-7).ToString()
$CurlData = @"
<?xml version="1.0" standalone="yes"?>
<ActivityRecordSearch xmlns="http://schemas.netwrix.com/api/v1/activity_records/">
<FilterList>
<Who Operator="Contains>$username</Who>
<ObjectType Operator="Equals">Logon</ObjectType>
<when>
    "From": $date
    "To": $((Get-date).tostring())
</when>
</FilterList>
</ActivityRecordSearch>
"@


$CurlArgument = '-H "Content-Type:application/xml; Charset=UTF-8"', 
                "http://netwrix:10000/netwrix/api/v1/activity_records/",
                '--data-binary', "$CurlData"
Start-Process "C:\TMP\curl-7.72.0-win64-mingw\bin\curl.exe" -ArgumentList $CurlArgument

How would I convert to an invoke-restmethod on PowerShell 5?

1

There are 1 answers

0
Doug Maurer On

You have an unequal amount of quotes, contains has a double quote at the beginning but no closing quote.

<Who Operator="Contains>$username</Who>

You may also want to cast it to XML

$CurlData = [xml]@"
<?xml version="1.0" standalone="yes"?>
<ActivityRecordSearch xmlns="http://schemas.netwrix.com/api/v1/activity_records/">
<FilterList>
<Who Operator="Contains">$username</Who>
<ObjectType Operator="Equals">Logon</ObjectType>
<when>
    "From": $date
    "To": $((Get-date).tostring())
</when>
</FilterList>
</ActivityRecordSearch>
"@