Passing the Parameters to Function in PowerShell Script getting Failed

san1989
Giga Guru

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"

<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="name" value="Powershell.exe C:\Test\Sample.ps1 'TEST1' ' Test2'"/><parameter name="skip_sensor" value="true"/></parameters>

 

Received response from Mid Server as "Input" as below to snow

<result command="Powershell.exe C:\Test\Sample.ps1 'TEST1' ' Test2'">
<stdout>Display name output: TEST1 Test2</stdout>
 
But the same logic, when I am trying with function inside the PowerShell script, is not working
PowerShell Script 2:

function My-Function {
param (
[string]$arg1,
[string]$arg2 )

Write-Host "Argument 1: $arg1"
Write-Host "Argument 2: $arg2"}

 

From ECC Queue Payload

<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="name" value="Powershell.exe C:\Test\Sample1.ps1 My-Function 'TEST1' ' Test2'"/><parameter name="skip_sensor" value="true"/></parameters>
 
Below is the result
<result command="Powershell.exe C:\Test\Sample1.ps1 My-Function 'TEST1' ' Test2'">
<stdout/>
 
Can you please help me on above  parameter (highlighted in blue) to call the function in powershell script and to send the parameters.
 
1 ACCEPTED SOLUTION

Community Alums
Not applicable

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

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

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

san1989
Giga Guru

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