VMware Cloud Community
bvi1006
Enthusiast
Enthusiast
Jump to solution

How to I execute a command string which has too many escape characters and such

Hi,

I know this is not a PowerShell forum, but perhaps it will eventually help someone else!

I need to execute the following command, all one one line:

curl -vvvv -XPUT --negotiate -u : --insecure -XPUT -g -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.tomtom.com\"}" https://someservert/v1/host/test

However, no matter how many times I set a variable to this, it still fails.

Can anyone help me wrap this thing up and execute it in PS?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
bvi1006
Enthusiast
Enthusiast
Jump to solution

Hi,

I had found that triple quoting worked, but not for the brackets. Therefore, I used the Convertto-JSON with double quotes to give me the correct ouput. I did this only once because I will be using the string over and over again using variables in place of some required parameters. NOTE: the resultant command will run one line, which is necessary.

$json = '""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.tomtom.com""} https://someservert/v1/host/test'| ConvertTo-JSON

$json contains this:

"\"\"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.tomtom.com\"\"} https://someservert/v1/host/test"

Then I set the entire command/parameters. I realize I could use the $json variable here, possibly, but for me there is no need.

$cmd = @"

curl.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.tomtom.com\"\"} https://someservert/v1/host/test"

"@

Hope this helps someone else!

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Did you already try something like this ?

Just double the quote where a quote is needed inside the string

Those are 2 lines.

$cmd = "curl -vvvv -XPUT --negotiate -u : --insecure -XPUT -g -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.tomtom.com""} https://someservert/v1/host/test"

Invoke-Command -ScriptBlock {&$cmd}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

bvi1006
Enthusiast
Enthusiast
Jump to solution

Hi,

I had found that triple quoting worked, but not for the brackets. Therefore, I used the Convertto-JSON with double quotes to give me the correct ouput. I did this only once because I will be using the string over and over again using variables in place of some required parameters. NOTE: the resultant command will run one line, which is necessary.

$json = '""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.tomtom.com""} https://someservert/v1/host/test'| ConvertTo-JSON

$json contains this:

"\"\"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.tomtom.com\"\"} https://someservert/v1/host/test"

Then I set the entire command/parameters. I realize I could use the $json variable here, possibly, but for me there is no need.

$cmd = @"

curl.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.tomtom.com\"\"} https://someservert/v1/host/test"

"@

Hope this helps someone else!

0 Kudos