Handling a million quotes in a string

Welcome Forums General PowerShell Q&A Handling a million quotes in a string

This topic contains 11 replies, has 4 voices, and was last updated by

 
Participant
3 years, 6 months ago.

  • Author
    Posts
  • #25183

    Participant
    Points: 31
    Rank: Member

    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!

  • #25187

    Participant
    Points: 0
    Rank: Member

    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'
    
  • #25189

    Participant
    Points: 303
    Helping Hand
    Rank: Contributor

    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
    
  • #25190

    Participant
    Points: 0
    Rank: Member

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

  • #25195

    Member
    Points: 0
    Rank: Member

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

    plink --% -load session # etc
    
  • #25196

    Member
    Points: 0
    Rank: Member

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

  • #25197

    Participant
    Points: 0
    Rank: Member

    Oh yeah, I always forget about that one.

  • #25404

    Participant
    Points: 31
    Rank: Member

    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 🙂

  • #25409

    Member
    Points: 0
    Rank: Member

    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
    
  • #25411

    Participant
    Points: 31
    Rank: Member

    Thanks, I have tried that previously ... 🙁

  • #25478

    Participant
    Points: 31
    Rank: Member

    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!

  • #25627

    Participant
    Points: 31
    Rank: Member

    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 🙂

The topic ‘Handling a million quotes in a string’ is closed to new replies.