Invoke Command - PowerShell


Overview

Invoke-Command is a powerful PowerShell command designed to execute commands remotely on multiple target computers. It enables centralized management and task automation across a distributed environment, allowing administrators to execute scripts, manage system settings, and gather information from remote machines.

Syntax

Invoke-Command [-ComputerName] <String[]> [-ScriptBlock] <ScriptBlock> [-ArgumentList] <Object[]> [-AsJob] [-Credential] <PSCredential> [-ErrorAction] <ActionPreference> [-ErrorVariable] <String> [-Force] [-JobName] <String> [-OutFile] <String> [-OutVariable] <String> [-ThrottleLimit] <Int32> [-UseSSL] [-SessionOption] <SessionOption> [-Authentication] <AuthenticationMechanism> [-Proxy] <Uri> [-ProxyCredential] <PSCredential> [-CertificateThumbprint] <String>

Options/Flags

-ComputerName: Specifies the target computer(s) where the command will be executed. Accepts both individual computer names and arrays of computer names.

-ScriptBlock: The script or command to be executed on the target computers. This can be a literal scriptblock or a script file path.

-ArgumentList: An array of arguments to be passed to the scriptblock.

-AsJob: Creates a background job that runs the command asynchronously.

-Credential: Specifies the credentials to use for authentication when connecting to the target computers.

-ErrorAction: Controls how errors are handled during execution.

-ErrorVariable: Stores any errors encountered during execution in a specified variable.

-Force: Suppresses confirmation prompts when executing the command.

-JobName: Assigns a custom name to the background job created by the -AsJob parameter.

-OutFile: Redirects the output of the command to a specified text file.

-OutVariable: Stores the output of the command in a specified variable.

-ThrottleLimit: Limits the number of concurrent connections to target computers.

-UseSSL: Enforces SSL encryption for the WinRM transport.

-SessionOption: Specifies advanced WinRM session options.

-Authentication: Determines the authentication mechanism used for connecting to the target computers.

-Proxy: Configures a proxy server for the WinRM transport.

-ProxyCredential: Specifies the credentials to use for the proxy server.

-CertificateThumbprint: Specifies the thumbprint of the SSL certificate to be used when connecting to the target computers.

Examples

Example 1: Run PowerShell script on remote computers

Invoke-Command -ComputerName server1, server2 -ScriptBlock { Get-Service }

Example 2: Execute command with arguments on target computers

Invoke-Command -ComputerName client1, client2 -ScriptBlock { Invoke-WebRequest -Uri https://google.com } -ArgumentList @(10)

Example 3: Create a background job and check its status

$job = Invoke-Command -AsJob -ComputerName remotehost -ScriptBlock { Get-Hotfix }
Get-Job | Format-Table Name, State

Common Issues

  • Failed connections: Ensure that WinRM is enabled on the target computers and that firewall rules allow for WinRM communication.
  • Authentication errors: Verify that the specified credentials have sufficient privileges on the target computers.
  • Timeout errors: Increase the -ThrottleLimit parameter to allow for more concurrent connections.

Integration

Invoke-Command can be combined with other PowerShell commands to perform more complex tasks:

  • Get-Credential: Prompt the user for credentials to use with the -Credential parameter.
  • Export-CSV: Export the output of the command to a CSV file.
  • Join-Object: Combine the output from multiple Invoke-Command calls.
  • Enter-PSSession: Establishes an interactive PowerShell session with a remote computer.
  • New-PSSession: Creates a persistent PowerShell session with a remote computer.
  • Remove-PSSession: Terminates a PowerShell session with a remote computer.