- 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 pass arguments in Invoke-Command in PowerShell?
To pass the argument in the Invoke-command, you need to use -ArgumentList parameter. For example, we need to get the notepad process information on the remote server.
Example
Invoke-Command -ComputerName Test1-Win2k12 - ScriptBlock{param($proc) Get-Process -Name $proc} - ArgumentList "Notepad"
Output
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName ------- ------ ----- ----- ------ -- -- ----------- -------------- 67 8 1348 7488 0.08 104 notepad Test1-Win2k12
In the above example, we are passing "Notepad" name as the argument to the command and the same has been caught by the $proc variable inside Param().
If you have the multiple, check the below command to pass the multiple parameters.
Example
Invoke-Command -ComputerName Test1-Win2k12 -ScriptBlock{param($proc,$proc2) Get-Process -Name $proc,$proc2} -ArgumentList "Notepad","Calc"
Output
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName ------- ------ ----- ----- ------ -- -- ----------- -------------- 96 20 5980 11392 0.19 288 calc Test1-Win2k12 67 8 1344 7556 0.08 104 notepad Test1-Win2k12
Same can be achieved with the Session variable.
Example
$sess = New-PSSession -ComputerName Test1-win2k12 Invoke-Command -Session $sess -ScriptBlock{param($proc) Get-Process $proc} -ArgumentList "Notepad"
Output
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName ------- ------ ----- ----- ------ -- -- ----------- -------------- 67 8 1348 7488 0.08 104 notepad Test1-Win2k12
- Related Articles
- How to run Invoke-Command in PowerShell Workflow?
- How to pass arguments to a Button command in Tkinter?
- How to exclude PSComputerName property from Invoke-Command output in PowerShell?
- How to exclude the RunSpaceID property from the Invoke-Command output in PowerShell?
- How can I pass arguments to Tkinter button's callback command?
- How to pass arguments to animation.FuncAnimation() in Matplotlib?
- How to download images using Invoke-Webrequest in PowerShell?
- How to invoke a function with partials prepended arguments in JavaScript?
- How to invoke a function with its arguments transformed in JavaScript?
- How to work with Invoke-Command Scriptblock output?
- How to Parse Command Line Arguments in C++?
- How to add command line arguments in Python?
- How to pass arguments to anonymous functions in JavaScript?
- How to get website links using Invoke-WebRequest in PowerShell?
- How to pass arrays as function arguments in JavaScript?

Advertisements