Author Posts

May 13, 2015 at 11:13 am

Hi,

I have the following command I would like to run in Powershell:

plink -load session curl -vvvv -XPUT –negotiate -u : –insecure -H "Content-Type: application/json" -d '{"hosttype":"server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "umiworker": "nowwhere", "notes": "this is a test", "url": "http:/www.google.com"}' https://somesite/v11/host/test/Joe

With all of the quotes, how can I run the command?

Can anyone help?

Thanks!

May 13, 2015 at 11:27 am

Single-quotes wrapped around double-quotes works just fine. You have to escape the single-quotes in your string though. You do that by using two single-quotes. Edit: .. which WordPress conveniently converts into a double-quote...

Invoke-Expression 'plink -load session curl -vvvv -XPUT –negotiate -u : –insecure -H "Content-Type: application/json" -d ''{"hosttype":"server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "umiworker": "nowwhere", "notes": "this is a test", "url": "http:/www.google.com"}'' https://somesite/v11/host/test/Joe'

May 13, 2015 at 11:30 am

Try a herestring:

$website = "https://somesite/v11/host/test/Joe"
$address = "127.0.0.1"
$notes= "this is a test"

$hereString = @"
plink -load session curl -vvvv -XPUT –negotiate -u : –insecure -H "Content-Type: application/json" -d '{"hosttype":"server", "address": ["$address"], "parents": [], "tag": ["demo"], "umiworker": "nowwhere", "notes": "$notes", "url": "http:/www.google.com"}' $website
"@

&$hereString

May 13, 2015 at 11:30 am

about_Quoting_Rules explains the intricacies of single-quotes vs. double-quotes: https://technet.microsoft.com/en-us/library/hh847740.aspx

May 13, 2015 at 11:50 am

In v3 or later, just use the "stop parsing" operator, –%:

plink --% -load session # etc

May 13, 2015 at 11:51 am

Bah, our forum software is converting two hyphens into that goofy dash character. The operator is hyphen-hyphen-percent, and anything after it on the line is sent directly to the console application without PS doing any parsing. (This means PowerShell variables after the operator are also not expanded, so be aware of that.)

May 13, 2015 at 11:53 am

Oh yeah, I always forget about that one.

May 19, 2015 at 8:53 am

Thanks so much Everyone.

I am battling the json from Windows to Linux battle. I know there is some crazy issue with trying to XPUT from Windows. Here is what I am sending which is erroring, out:

curl -vvvv -XPUT –negotiate -u : –insecure -H "Content-Type: application/json" -d "{ "hosttype": "server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "worker": "seattle", "notes": "this is a test", "url": "http://www.google.com"}" http://hostname/v1/host/testserver

Can someone try to help me? I feel like I have tried everything. The error I get is:

[b]Output: 400: Bad Request args: [] kwargs: {'hostname': u'testserver'}"}*[/b]

Can anyone help? This is brutal 🙂

May 19, 2015 at 10:20 am

In this case, you'd probably be fine with just changing the double-quotation marks around the -d string to single quotes. That way all of the JSON code inside that parameter will have its double-quotes treated as literal parts of the string.

curl.exe -vvvv -XPUT –negotiate -u UserName:Password -k -H "Content-Type: application/json" -d '{ "hosttype": "server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "worker": "seattle", "notes": "this is a test", "url": "http://www.google.com"}' http://hostname/v1/host/testserver

May 19, 2015 at 10:40 am

Thanks, I have tried that previously ... 🙁

May 20, 2015 at 11:35 am

ok, I am almost there!

$raw = '{"hosttype":"server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "umiworker": "gent", "notes": "this is a test", "url": "http://www.google.com"}'
$form = $raw | ConvertTo-JSON

$type = '"Content-Type: application/json"'

$url = " http://hostname/v1/host/testserver"

$mycommand = "mycurl.exe -vvvv -XPUT -g -H '$type' -d $form $url"

# $mycommand value is now: mycurl.exe -vvvv -XPUT -g -H '"Content-Type: application/json"' -d "{\"hosttype\":\"server\", \"address\": [\"127.0.0.1\"], \"parents\": [], \"tag\": [\"demo\"], \"umiwo
rker\": \"gent\", \"notes\": \"this is a test\", \"url\": \"http://www.google.com\"}" http://hostname/v1/host/testserver

I tried running &$mycommand because clearly PS is trying to parse my crazy quotes but that is not working – it is parsing the json section.

Can someone tell me how to execute this long command without it being parsed by PS?

Thanks!

May 26, 2015 at 8:59 am

Ok, I got it, thanks you all for your ideas!

Two issues:
– running curl.exe invokes invoke-webrequest, so I renamed my curl to curltt.exe so that the Kerberos negotiation works.
-The brackets were difficult to handle in this long string.-Triple quotes did not work for the brackets.

I was able to get the exact syntax required by setting a variable to my text and converting to json. I will copy the contents of $json and use that string permanently, substituting some data with future variables:

$json = '{"hosttype":"server", "address": ["127.0.0.1"], "parents": [], "tag": ["demo"], "umiworker": "gent", "notes": "this is a test", "url": "http://www.google.com"}' | ConvertTo-JSON

Now the variable $json shows the correct format required for the json data to be used with curl on Windows put to a Linux box:

"{\"hosttype\":\"server\", \"address\": [\"127.0.0.1\"], \"parents\": [], \"tag\": [\"demo\"], \"umiworker\": \"gent\", \"notes\": \"this is a test\", \"url\": \"http://
http://www.google.com\"}"

🙂 🙂 🙂

To put this all together:

Create a command variable:

$cmd = @"

curltt.exe -XPUT -H "Content-Type: application/json" -d '"{\"hosttype\":\"server\", \"address\": [\"127.0.0.1\"], \"parents\": [], \"tag\": [\"demo\"], \"umiworker\": \"gent\", \"notes\": \"this is a test\", \"url\": \"http://
http://www.google.com\"}"' https://someserver/someAPI/v1/name

"@

invoke-expression $cmd

I hope this isn't too ugly, but I'm a beginner, and it works 🙂