Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Windows PowerShell Invoke-Command

Windows PowerShell Invoke-Command

PowerShell's invoke-Command is ideal for running a quick command on a remote computer.  Alternative techniques would be to use invoke-Expression for string commands or else create a PSSession.

Remoting in Microsoft PowerShell v 2.0 with Invoke-Command

 ♣

Three Key Parameters for Invoke-Command

The secret of success with Invoke-Command is to pay particular attention to these three parameters:

  1. -computerName to make that remote connection, (-computer is unambiguous and works just as well). 
  2. -scriptBlock place the payload of your command is inside the {Braces}.
  3. -filePath store the PowerShell instructions in a file, then run against multiple machines.

When investigating the scope and parameters for any new command, I call for Get-help followed by the name of the cmdlet, thus:

Get-help invoke-Command -full

Simple Example of Invoke-Command with -scriptBlock

# PowerShell Invoke-Command Example
Clear-Host
Invoke-Command -computer Victim1 -scriptBlock {"C:\Program files" |dir}

{"C:\Program files" |dir } is just for illustration, a more common usage would be PowerShell cmdlets such as Get-Service or Get-Eventlog.

Invoke-Command -computer Test1, Test2 -scriptBlock {Get-Eventlog}

Invoke-Command -computer Test1, Test2 -scriptBlock {Get-Eventlog}

Get-Process Example of Invoke-Command

# Invoke-Command -scriptBlock example in PowerShell 2.0
Invoke-Command -computerName BigServer, LittleServer `
-scriptBlock {Get-Process | Where-Object {$_.name -like 'W*'}}

Note 1:  The backtick ` tells PowerShell to word-wrap to the second line.

Note 2:  You could also store a list of computer names in a file and call the file with:
Invoke-Command -ComputerName (Get-Content "E:\PowerShell\com\ServerList.txt")

Note: If the scriptBlock does not do what you want and you wish to append string commands, try Invoke-Expression instead.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Invoke-Command -filePath

Using the -filePath parameter is an alternative technique to employing -scriptBlock.  The idea is that you store all the PowerShell instructions in a .ps1 file.  Then instead of looking for a scriptBlock, you point invoke-Command at the -filePath.  The benefit is that you don't have to keep a copy of the file on all the remote servers for which you want to run the script.

# PowerShell Invoke-Command -filePath example
invoke-Command -computerName BigServer `
-filePath "C:\Services.ps1"

Note 1:  Naturally for this to work you need suitable PowerShell instructions in the file that I have called Services.ps1.

Note 2:  I find that you always need the name of a server, even when testing on the local machine.

Note 3: Talking of files, you could also store a list the servers, which you want to run the instructions, in a file and then call that file.

# Invoke-Command -filePath example in PowerShell 2.0
-ComputerName (Get-Content "C:\ServerList.txt")  `
-filePath "C:\Services.ps1"

Other Useful Techniques

Jobs and -asJob
For scripts that take a long time to complete you could consider appending the -asJob parameter.  This has the pleasant effect of running the commands in the background.  However, to actually see the results, you need to master the cmdlet receive-Job.

As an alternative to -asJob you could be to employ the -session parameter then run Start-job.  However from a learning perspective, I normally like to concentrate on one technique at a time and combining invoke-Command with start-Job is a tricky task for a beginner.

Invoke-Expression

Invoke-Expression is a sister PowerShell cmdlet useful for mimicking the CMD dos box.  A typical scenario is where you can typing command-line instructions, but now you want to execute the same string using PowerShell. 

With invoke-Expression you can either save the string values in a text file, then execute them, else append the string directly to invoke-Expression.  See more about invoke-Expression here.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Introduction to the PowerShell Invoke Family

To investigate PowerShell's 'invoke' verb, let us start with the trusty Get-command, thus:

Get-command -verb invoke

  • Invoke-Command
  • Invoke-Expression
  • Invoke-History
  • Invoke-Item
  • Invoke-WmiMethod
  • Invoke-WsManAction

Summary of PowerShell Invoke-Command

Here is handy remoting technique for running PowerShell commands on a batch of network machines.  Learn more about the command with help invoke-Command, the key to this cmdlet is what you put between the -scriptBlock {Braces}.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials:

PowerShell Home   •Shell Application  • New-Object   • PowerShell create shortcut

PowerShell Logon Script   • Map Network Drive   • PowerShell add printer   • PowerShell -com

Invoke-Expression   • Invoke-Command 

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: