What is Invoke-Command Cmdlet
Powershell Invoke-Command usage has many useful implementations.
You can run a command, a set of commands, on local, remote, or several computers. For remoting, you also can check the Cmdlet of “New-PSSession.”
As of Powershell 6.0, you also can connect to a remote computer through a Secure Shell connection (SSH) with Invoke-Command.
Affiliate: Experience limitless no-code automation, streamline your workflows, and effortlessly transfer data between apps with Make.com.
Powershell Invoke-Command Usage Examples
Let’s say you want to run a file “*.ps1” with Powershell Invoke-Command usage on a local computer:
Invoke-Command -FilePath c:\scripts\test.ps1
If you want to run several Cmdlets in a script block:
Invoke-Command -Scriptblock {
Get-Process
Get-Host
}
If you want to save the script block as a variable and then run it:
$Command = {
Get-Process
Get-Host
}
Invoke-Command -Scriptblock $Command
Sometimes if you get errors, you can add Parentheses “()” to the variable:
$Command = ({
Get-Process
Get-Host
})
Invoke-Command -Scriptblock $Command
Executing Command on a remote computer with Powershell Invoke-Command usage (in most cases, you will need credentials to run a command on a remote computer):
$Computer = "KitchenComputer001"
$Credential = Get-Credential
Invoke-Command -ComputerName $Computer -Credential $Credential -ScriptBlock {
Get-Host
}
Invoking Commands and sending variables to the remote computer:
$Computer = "KitchenComputer001"
Invoke-Command -ComputerName $Computer -Credential $Credential -ArgumentList $Credential.UserName,$Admin -ScriptBlock {
Get-LocalUser -Name $args[0]
Get-LocalUser -Name $args[1]
}
The first argument always will be “$args[0]” (Arguments are an array). Second will be “$args[1]”, third will be “$args[2]”, etc.
You cannot use the argument with quotations (“”) inside the Script Block. If you need your argument as a string, use it in the argument itself or a variable. For example, you want to add Computer Account “RoomComputer001” to the Local Administrators Group of “KitchenComputer001”:
$Computer = "KitchenComputer001"
$ComputerAccount = "RoomComputer001"
Invoke-Command -ComputerName $Computer -Credential $Credential -ArgumentList $ComputerAccount -ScriptBlock {
Add-LocalGroupMember -Group "Administrators" -Member $args[0]
}
The action above will give you “Principal KitchenComputer001 was not found.” Error. The error means you must add “$” to the string so that it will be “RoomComputer001$”. You can create another variable and then use THAT variable instead, but it will be much simpler to add to the args[0] parameter with quotes, like
Add-LocalGroupMember -Group "Administrators" -Member "$args[0]$"
the action will give you “Principal KitchenComputer001[0]$ was not found.” Error. So, we’ll use the quotes in the argument itself:
$Computer = "KitchenComputer001"
$ComputerAccount = "RoomComputer001"
Invoke-Command -ComputerName $Computer -Credential $Credential -ArgumentList "$ComputerAccount$" -ScriptBlock {
Add-LocalGroupMember -Group "Administrators" -Member $args[0]
}
You can return the output of Powershell Invoke-Command usage to a variable or an array:
$Array = Invoke-Command -ComputerName (Get-Content c:\Computers.txt) -Credential $Credential -ScriptBlock { Get-Process -Name "WmiPrvSE" }
The switch “-ComputerName” gets all the computer names from another Cmdlet, “Get-Content,” which reads the file “c:\Computers.txt”. Instead of putting Get-Content from Computers.txt into a variable, we can set it inside parentheses like: (Get-Content c:\Computers.txt). Then in the script block, we want to know all instances of the process “WmiPrvSE” on all the computers. Now “$Array” will contain all the process instances on all the computers. A property “PSComputerName” will have the computer names
$Array.PSComputerName
Another Powershell Invoke-Command usage is with the correlation of New-PSSession:
$RemoteComputer = "KitchenComputer001"
$Credential = Get-Credential
$RemoteSession = New-PSSession -ComputerName $RemoteComputer -Credential $Credential
Invoke-Command -Session $RemoteSession -ScriptBlock {
Get-Host
}
Sometimes, you need to use a specific Powershell Module (like SCCM / Exchange), and you can execute only on a particular server, so Powershell Invoke-Command usage is very useful in this case.
…Sometimes if you get errors…. It is “try and error” game?
Most of the time.