The secret of success with Invoke-Command is to pay particular attention to these
three parameters:
-ComputerName to make that remote connection, (-Computer is
unambiguous and works just as well).
-ScriptBlock place the payload of your command is
inside the {Braces}.
-FilePath store the PowerShell instructions in
a file, then run against multiple machines.
Get-Help When investigating the scope and parameters
for any new command, I call for Get-Help followed by the name of the cmdlet,
this is how I learned about the above parameters.
{"C:\Program files" | GCI or Get-ChildItem} is just for illustration, a more common usage
would be PowerShell cmdlets such as Get-Service or Get-Eventlog.
# 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 3: If the ScriptBlock does not do what you
want, and you wish to append string commands, try
Invoke-Expression
instead.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Network Performance Monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
What I like best is the way NPM suggests solutions to network
problems. Its also has the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating
network maps, then I recommend that you try NPM now.
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.
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.
Guy Recommends: SolarWinds Engineer's Toolset v10
This
Engineer's Toolset v10 provides a comprehensive console of 50 utilities
for troubleshooting computer problems. Guy says it helps me
monitor what's occurring on the network, and each tool teaches me more about how the
underlying system operates.
There are so many good gadgets; it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring,
network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer's Toolset now!
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.
Here is handy remoting technique for running PowerShell commands on a batch
of network machines. Learn more about the syntax and parameters 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
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.