question

jansiranikrishnan-1796 avatar image
0 Votes"
jansiranikrishnan-1796 asked AndreasBaumgarten commented

Creating a variable in PowerShell with complex value

Hi Experts,

I would like to create a variable in PowerShell with value as below.

![105951-image.png][1]


Is it possible to create a variable which holds the value as below in PowerShell? Please suggests. If yes, Please show the syntax.
![105952-image.png][2]

Regards,
Jansi

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Experts,

I would like to create a variable in PowerShell with value as below.
105886-image.png


Is it possible to create a variable which holds the value as below in PowerShell? Please suggests. If yes, Please show the syntax.

105933-image.png

Regards,
Jansi

0 Votes 0 ·
image.png (10.4 KiB)
image.png (9.9 KiB)
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten commented

Hi @jansiranikrishnan-1796 ,

could you please post the code as code and not as a screenshot.
It's really time consuming to write all the stuff from the screenshot. Copy&Paste is much easier.
Please try this:

  $a = '"RunbookId":"11212313",
  "Parameters":"<Data><Parameter><Name>Urgency</Name><ID>{121242342342}</ID><Value>High</Value></Parameter></Data>"'

(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Andreas,

Thank you for the response. It is working. Sorry for the inconvenience caused. Actually I am working on the SCSM & Orch Servers remotely and I could not copy anything from the remote servers. I will try my best to copy the code next time but not as a screenshot.


One question here.. Instead of using the hardcode values, how to use the variable which actually replaces its value while compiling the $a.

Example:
$RunbookID="11212313"
$a = '"RunbookId":"$($RunbookID)",
"Parameters":"<Data><Parameter><Name>Urgency</Name><ID>{121242342342}</ID><Value>High</Value></Parameter></Data>"'

I tried the above code and not getting the exact output which I expected. Any inputs is highly appreciated.

Regards,
Jansi

0 Votes 0 ·

The values of the variables must be defined/set by ServiceNow. How ever they are doing this ;-) I have no clue about SNOW.

Than the code needs to be executed to trigger the runbook in Orchestrator.
Or did I miss something?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·

Hi Andreas,

Yes, You are correct. With respect to the SCSM-SNOW integration, these values must be sent by ServiceNow to the SCSM. However I would like to do some testing from my end (SCSM). So thought of using variables instead of hardcoded values. Is there any chance that we can include variable in the above format.. so that it can replaces its value while executing to frame the exact JSON format request.

Regards,
Jansi

0 Votes 0 ·
Show more comments
cooldadtx avatar image
0 Votes"
cooldadtx answered jansiranikrishnan-1796 commented

Yes. Depends on what you intend to do with it though as to how you do it. In most cases just use a hashtable in PS.

$JSONBody = @{ RunbookId = '1234'; Parameters = '<xml></xml>' }   
$JSONBody | Get-Member


However if you are really working with JSON then PS supports conversion to and from already. Normally you get JSON as a string from some API. PS can convert that string to a PS object.

# From some API
$json = '{ "RunbookId": "1234", "Parameters": "<xml></xml>" }'
$data = ConvertFrom-Json $json   
$data | Get-Member
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response cooldadtx. It is working.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered jansiranikrishnan-1796 commented

Hi,

Please post your code using the Ctrl-K tool. You can try the ConvertFrom-Json cmdlet to convert the JSON string to a variable.

 $JsonBody='
 {
 "ID": "e8d-a0c",
 "Parameter": "<Data><Name>{226-91c}</Name></Data>"
 }'
 $var = $JsonBody | ConvertFrom-Json

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response Lan Xue. It is working.

0 Votes 0 ·