Solved

Example needed: QOPERATION EXECUTE within POST QCOMMAND built in PowerShell?

  • 19 October 2022
  • 6 replies
  • 391 views

Can someone provide an example of invoking “qoperation execute” via a POST QCOMMAND call to the REST API? I’m trying to build the XML for the “qoperation execute” as a PowerShell variable (in a loop, updating backed on what we’re looping on), but since I’ve never done anything like this, I can’t tell why I’m getting an “internal error” response, apart from, “clearly I don’t know what I’m doing.” If I can see a successful example, I can see how different my attempt looks and hopefully adjust accordingly. 

Thanks,
Nick

Since someone is likely to ask: 

$napTime = "<?xml version=`"1.0`" encoding=`"UTF-8`"?><App_SetMediaAgentPropertiesReq> <mediaAgentInfo><mediaAgent mediaAgentName=`"$thisma`"/> <mediaAgentProps markMAOfflineForMaintenance=`"1`" enableMAAfterDelay=`"60`"/></mediaAgentInfo></App_SetMediaAgentPropertiesReq>"

            $takeNap = "$origin/Qcommand/qoperation%20execute -req $naptime"

            $a5 = Invoke-RestMethod -Uri $takeNap -Method Post -Headers $headers

icon

Best answer by christopherlecky 19 October 2022, 21:44

View original

6 replies

Userlevel 5
Badge +16

 

The @headers notation simply didn’t work. Moving the XML to -body was what I needed, though, so full credit for that. 😀

Userlevel 5
Badge +16

Hmm you may be right on that second one. 

I was free styling it so I just kind of went with my first thought on it. 

You actually should be able to pass the hash map as a parameter. 

I was confusing it with splatting all the parameters which would look something like so:

Starting with something like this:

$commserve = mycs.mydomain.com
$uri = $commserve + "/QCommand/qoperation%20execute"
$parameters = ${
$body = "<App_SetControlPanelParametersRequest>
<preventAdminAccessToUserData>true</preventAdminAccessToUserData>
</App_SetControlPanelParametersRequest>"
$headers = @{
$accept = "application/xml"
$authtoken = QSDK 38568012f4d1e8ee1841d283a47aa3ba78e124ea58354b5fc60f4dab8a63347d05cf5552484dafda3bfa4c5db84e580b1cb37bcf8e65b39f7f8549a443e6f78a2c7be3f31b3d845e24776c835e498e8e883bb40c46bd15af4f40ca94e823acedcdd4e9659e74b34a07a85c4586cd2ed914b6dce015874783ef768fda78183a4208930954a377f66eb56c8b92cexampl4s437a19317ca6ce7f3233d5a01aca35dbad93468b833f2cf71010809006a937670adce711ca8be46638e8
$Content-type = "application/xml"
}
}

$qoperationresult = Invoke-restmethod @parameters

 

Rewriting it to something like this : 

$commserve  = mycs.mydomain.com

$parameters = ${
uri = ""
body = ""
headers = @{
accept = ""
authtoken = ""
content-type= ""
}
}

$parameters.uri = $commserve + "/QCommand/qoperation%20execute"
$parameters.body = "<App_SetControlPanelParametersRequest<preventAdminAccessToUserData>true</preventAdminAccessToUserData></App_SetControlPanelParametersRequest>"
$parameters.headers.accept = "application/xml"
$parameters.headers.authtoken = "QSDK 38568012f4d1e8ee1841d283a47aa3ba78e124ea58354b5fc60f4dab8a63347d05cf5552484dafda3bfa4c5db84e580b1cb37bcf8e65b39f7f8549a443e6f78a2c7be3f31b3d845e24776c835e498e8e883bb40c46bd15af4f40ca94e823acedcdd4e9659e74b34a07a85c4586cd2ed914b6dce015874783ef768fda78183a4208930954a377f66eb56c8b92cexampl4s437a19317ca6ce7f3233d5a01aca35dbad93468b833f2cf71010809006a937670adce711ca8be46638e8"
$parameters.headers.content-type = "application/xml"


$qoperationresult = Invoke-restmethod @parameters

You could then set the object properties through dot notation.

OK, your corrections are clearly “things I wouldn’t have thought of in a million years,” so thank you for that. I’ll try to be amused (and impressed) that my $headers/@headers confusion was tolerated by Powershell in my prior work

Off to experiment, with gratitude. . 

Userlevel 5
Badge +16

There are a couple of things.

  1. The XML for the command goes into the body.
  2. When passing the headers you are actually passing a hash map of values so you don’t use $ you use @

so it should look something like this :

$commserve = mycs.mydomain.com
$uri = $commserve + "/QCommand/qoperation%20execute"
$body = "<App_SetControlPanelParametersRequest>
<preventAdminAccessToUserData>true</preventAdminAccessToUserData>
</App_SetControlPanelParametersRequest>"
$headers = @{
$accept = "application/xml"
$authtoken = QSDK 38568012f4d1e8ee1841d283a47aa3ba78e124ea58354b5fc60f4dab8a63347d05cf5552484dafda3bfa4c5db84e580b1cb37bcf8e65b39f7f8549a443e6f78a2c7be3f31b3d845e24776c835e498e8e883bb40c46bd15af4f40ca94e823acedcdd4e9659e74b34a07a85c4586cd2ed914b6dce015874783ef768fda78183a4208930954a377f66eb56c8b92cexampl4s437a19317ca6ce7f3233d5a01aca35dbad93468b833f2cf71010809006a937670adce711ca8be46638e8
$Content-type = "application/xml"
}

$qoperationresult = Invoke-restmethod -uri $uri -body $body -headers @headers

Based on the following link:

The uri should follow the format <webservice>/QCommand/qoperation execute

Please double check the second line to make sure it matches your web services. 

I don’t have time to test it, but its not far from this.

 

 

 

 

 

 

Userlevel 7
Badge +23

I bet @christopherlecky knows!

Reply