- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2024 03:20 PM
Hello All,
I have been trying to send the parameters to function in PowerShell script. When I tried to send without function and by just parameters, I have succeeded, below is the example,
PowerShell Script 1:
$arg1 = $args[0]
$arg2 = $args[1]
#Do some processing
$DisplayName = $arg1+$arg2
Write-Host "Display name output:" $DisplayName
In ServiceNow:
Send payload from ECC Queue as "output"
Received response from Mid Server as "Input" as below to snow
function My-Function {
param (
[string]$arg1,
[string]$arg2 )
Write-Host "Argument 1: $arg1"
Write-Host "Argument 2: $arg2"}
From ECC Queue Payload
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2024 07:15 PM
Hi @san1989 ,
Please follow the below steps-
Functions should be defined in the below manner-
Sample1.ps1
function My-Function {
param (
[string]$arg1,
[string]$arg2
)
Write-Host "Argument 1: $arg1"
Write-Host "Argument 2: $arg2"
}
# Call the function with arguments
My-Function -arg1 $args[0] -arg2 $args[1]
Correct ECC payload should look like this-
<?xml version="1.0" encoding="UTF-8"?>
<parameters>
<parameter name="name" value="Powershell.exe C:\Test\Sample1.ps1 'TEST1' 'Test2'"/>
<parameter name="skip_sensor" value="true"/>
</parameters>
then run the script locally with parameters
Powershell.exe C:\Test\Sample1.ps1 'TEST1' 'Test2'
You should get the below output-
Argument 1: TEST1
Argument 2: Test2
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2024 07:15 PM
Hi @san1989 ,
Please follow the below steps-
Functions should be defined in the below manner-
Sample1.ps1
function My-Function {
param (
[string]$arg1,
[string]$arg2
)
Write-Host "Argument 1: $arg1"
Write-Host "Argument 2: $arg2"
}
# Call the function with arguments
My-Function -arg1 $args[0] -arg2 $args[1]
Correct ECC payload should look like this-
<?xml version="1.0" encoding="UTF-8"?>
<parameters>
<parameter name="name" value="Powershell.exe C:\Test\Sample1.ps1 'TEST1' 'Test2'"/>
<parameter name="skip_sensor" value="true"/>
</parameters>
then run the script locally with parameters
Powershell.exe C:\Test\Sample1.ps1 'TEST1' 'Test2'
You should get the below output-
Argument 1: TEST1
Argument 2: Test2
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2024 09:36 PM
I have only issue is how to pass values as strings in 'TEST1' and 'TEST2'.
lets say gr.name need to be passed to payload, how can get this value into ''.
'gr.name' will be take the value as it is. instead of 'TEST1', if I pass gr.name its not working.
Thanks