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
# 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!
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.
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.
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 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:
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
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 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.