◷ Reading Time: 3 minutes
Sample Project Details
In this tutorial, we will explain how you can run a PowerShell script on a Remote Machine from a Flow in FlexRule. FlexRule calls PowerShell.exe on a local machine and subsequently calls a script on a remote machine.
- The commands below need to be run in PowerShell on both the Local and Remote Machines:
Enable-PSRemoting -Force Set-ExecutionPolicy Unrestricted
- The “test.ps1” script needs to be copied into the “c:\scripts\test.ps1” file path on the remote machine.
- The “TestScriptLog.log” log file needs to be created using the “C:\logs\TestScriptLog.log” file path on the remote machine.
- Find your Remote Machine Computer’s Name.
- In this example, we have used a Local Windows Credential.
PowerShell Script details
The sample script we run returns the ‘host’, ‘service’, ‘user’, and ‘password’ from the remote machine. This script name is “test.ps1” and it has been saved in the “c:\scripts\test.ps1” path on our remote machine. Also, it will generate a log file called “TestScriptLog.log” under the “C:\logs\TestScriptLog.log” file path.
param($HostName,$ServiceName,$UserName, $Password) "INFO: Connecting to $ServiceName"| Out-File C:\logs\TestScriptLog.log -Append Write-Host 'host' $HostName Write-Host 'service' $ServiceName Write-Host 'user' $UserName Write-Host 'pw' $Password "INFO: done!"| Out-File C:\logs\TestScriptLog.log -Append
- Flow has three steps; Start, processRun and End.
- An output parameter called “resultPowerShell” is defined to show the output after execution.
- The Properties shown below need to be set for the processRun node:
- The result of the processRun will be copied into our output parameter called “resultPowerShell”.
- The complete Command Arguments is as shown below:
Invoke-Command -ComputerName DESKTOP-K23QIOF -FilePath c:\scripts\test.ps1 -Credential FlexRuleUser
Running the Flow
- Flow can be run in debug mode (because no Input Parameter has been set).
- By clicking on the next button, processRun will run and ask for User Credentials:
- After inserting the credential (password), the script will run and the result will be found in the Parameters Window:
ProcessRun returns three properties:
- output: success output of the command
- error: error output of the command
- hasError: A boolean value indicates if something has gone wrong during the execution
- By selecting the output, you can click on the Data Viewer (magnifier icon) and see the output result as shown below: