- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run Invoke-Command in PowerShell Workflow?
To run Invoke-Command in PowerShell Workflow we need to use the InlineScript block because Invoke-Command is not supported directly in the workflow. The below example is without using the InlineScript block we get an error.
Example
Workflow TestInvokeCommand{ Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{ Get-Service WINRM } }
Output −
At line:2 char:5 + Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Cannot call the 'Invoke-Command' command. Other commands from this module have been packaged as workflow activities, but this command was specifically excluded. This is likely because the command requires an interactive Windows PowerShell session, or has behavior not suited for workflows. To run this command anyway, place it within an inline-script (InlineScript { Invoke-Command }) where it will be invoked in isolation. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : CommandActivityExcluded
The above error indicates that the Invoke-Command can’t be directly used with the PowerShell and we need to use it inside the InlineScript block as shown below.
Workflow TestInvokeCommand{ InlineScript{ Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{ Get-Service WINRM } } } TestInvokeCommand
To use the outside variable in the scriptblock inside InlineScript block, we need to use $Using:VariableName. For example,
Example
Workflow TestInvokeCommand{ $server = "Labmachine2k16" InlineScript{ Invoke-Command -ComputerName $using:Server -ScriptBlock{ Get-Service WINRM } } } TestInvokeCommand
To store the output variable,
Output
Workflow TestInvokeCommand{ $server = "Labmachine2k16" $Status = InlineScript{ Invoke-Command -ComputerName $using:Server -ScriptBlock{ return( Get-Service WINRM).Status } } Write-Output "WinRM Status: $status" } TestInvokeCommand
- Related Articles
- How to pass arguments in Invoke-Command in PowerShell?
- How to exclude PSComputerName property from Invoke-Command output in PowerShell?
- How to run PowerShell commands from the command prompt?
- How to exclude the RunSpaceID property from the Invoke-Command output in PowerShell?
- How to run a PowerShell script from the command prompt?
- How to work with Invoke-Command Scriptblock output?
- How to download images using Invoke-Webrequest in PowerShell?
- How to run PowerShell commands in Background?
- What is the PowerShell Workflow?
- How to get website links using Invoke-WebRequest in PowerShell?
- How to run TestNG from command line?
- How Parallel and Sequence execution works in PowerShell Workflow?
- How to run Python functions in Eclipse command line?
- How to Run a Command Multiple Times in Linux?
- How to run Python functions from command line?

Advertisements