Handling a million quotes in a string

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!

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'

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

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

In v3 or later, just use the “stop parsing” operator, --%:

plink --% -load session # etc

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.)

Oh yeah, I always forget about that one.

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:

Output: 400: Bad Request args: [] kwargs: {‘hostname’: u’testserver’}"}*

Can anyone help? This is brutal :slight_smile:

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

Thanks, I have tried that previously … :frowning:

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!

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://
www.google.com”}”

:slight_smile: :slight_smile: :slight_smile:

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://
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 :slight_smile: