Return output from Powershell script to UIPath usi

2019-07-27 09:04发布

I am trying to get a value from an input box from a Powershell script back to a UI Path Sequence. I have created a simple sequence as an example. Yes, I know I could do this all in UI Path, I am just using an easy example as a way to try and test this for future use cases. Here is my sequence:

sequence

My text file from "Read text file" is:

$test = "Desktop/PassingArgs2of2.ps1 -Message foo"
Invoke-Expression -Command $test

The activity in UiPath looks like so:

Read Text File

The psCmd that I am running in the Invoke power shell activity looks like this:

Param(
[parameter(Mandatory=$true)]
[string]
$Message)

try{

    $Global:fooVar = $null

    function Test-InputBox(){
        [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
        $msg = "fooMsg"
        $title = "fooTitle"
        $localtest = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
        $Global:fooVar = $localtest.ToString()
    }
    Test-InputBox
}
catch{}

I tried setting fooVar equal to testLocal in the PowerShellVariables within Invoke power shell and then writing it, but that did not work.

fooVar

Basically I want to get fooVar back into UI Path. Any help would be greatly appreciated. Thank you!

1条回答
Deceive 欺骗
2楼-- · 2019-07-27 09:47

You're almost there. First, your Powershell script has to return a value. Take this for example:

[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$title = 'Your title goes here'
$msg   = 'Your favorite color:'

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
return $text

Here's the script in action (note that I called it twice and provided "red" the first time:

powershell messagebox

Then, just use this script directly in the Invoke Powershell activity. Note that the most important part here is the Output property - here, I decided to go for an array of strings. Naturally, as we only return a single value, you can just access the text provided by the user by accessing output(0).ToString().

invoke ps in UiPath

查看更多
登录 后发表回答