Invoke-Expression is a brilliant PowerShell cmdlet, which mimics us typing
instructions into a 'DOS box'. A typical scenario is where you are
expert with cmd.exe, and now you want to execute the same commands, but
using PowerShell. This is a job for the Invoke family of cmdlets,
specifically invoke-Expression.
Topics for Invoke-Expression Cmdlet
I have two 'Vehicles' for you to test invoke-Expression, firstly,
WOL Wake-on-Lan and secondly test-Connection.
I have chosen Wake-on-Lan (WOL) as a vehicle to show how to combine
PowerShell and cmd. Thanks to the use
of variables, it is easy to modify the invoke-Expression instructions to run your particular
command-line program instead of wolcmd.exe.
Invoke-Expression Example
While PowerShell has aliases for dir (list-ChildItem) and cd
(set-Location), there is no direct equivalent for cmd.exe. However,
the invoke-Expression cmdlet allows you to launch an executable and
crucially,
append command-line instructions. The result is that invoke-Expression executes
the string as if you had typed it in the 'DOS box'.
How to Bring the Command-Line into PowerShell - WOL Example
Take the scenario where you want to mimic cmd.exe, see screenshot above, my idea is to
execute Wake-On-Lan with a PowerShell
script containing two variables. Firstly, control the path to the executable, secondly,
append
the command-line instructions that you want to pass to that program.
Summary of WOL Command In DOS: Wolcmd 00248C1F9023
192.168.1.66 255.255.255.0 7
In PowerShell: Invoke-Expression Wolcmd.exe 00248C1F9023 192.168.1.66
255.255.255.0 7
Encouraging computers to sleep when not in use is a great idea -
until you are away from your desk and need a file on that remote sleeping machine!
Wake-On-LAN really will save you that long walk to awaken a hibernating
machine; however my reason for encouraging you to download this utility is
just because it's so much fun sending those 'Magic Packets'. As Wake-On-LAN (WOL) is free, see
if I am right, and you get a kick from arousing those sleeping machines.
WOL also has business uses for example, wakening machines so that they can have
their patches applied.
Vital preliminary step: download
wolcmd. I strongly recommend that you 'play' with wolcmd in the
DOS box before you use it in a PowerShell script.
In the script below I have added two variables because I
wanted to be sure that we control the path to the executable, and also I
wanted to
direct you to making your own version of my example.
Note 1: You probably want to change the values of $ExePath
and of $CLine
Note 2: Observe the double quotes
surrounding both variables. "$ExePath $CLine"
Note 3: Experiment with and without |
out-Null. This command merely suppresses any output.
Troubleshooting
If you are following my actual PowerShell Wake-on-Lan example, then
download
wolcmd here.
If you have changed the values of $ExePath and $CLine, but your script
still does not work try a manual walk-through in the cmd dos box. See if
you can get ipconfig to work with the command-line switch /all.
My idea in this PowerShell invoke-Expression example is to store the
string values in a text file, and then call that file and run those
instructions. The scenario is that you wish to measure the server's response
time with PowerShell's test-Connection, this is the equivalent of ping in
cmd.exe.
Concept 1: My instructions ping a website using PowerShell's
test-Connection cmdlet Concept 2: These instructions are installed in a text file with a
.ps1 file extension. Concept 3: Invoke-Expression then 'calls' this text file with its
string instructions.
Save this into a file, let us call it c:\PingWeb.ps1
Now let us use Invoke-Expression to execute these instructions.
# PowerShell invoke-Expression example from .ps1 file $Path =
"C:\PingWeb.ps1" Invoke-Expression "$Path"
As with many of my scripts, this example contains extra code which
aids learning, and
directs you to making changes to suit your circumstances.
However, you could simplify the expression to:
Invoke-Expression "C:\PingWeb.ps1"
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion 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.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is 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 take advantage of Solarwinds' offer.
The two most promising cmdlets are Invoke-Command and Invoke-Item.
Problem with Invoke-Expression -computername .... -command "xyz"
If it's any consolation commands like this did not work for me:
invoke-Expression -computername localhost, BigServer -command
"Get-process powershell*"
Invoke-Expression is a wonderful way of executing a string of DOS instructions,
but in
PowerShell. I chose Wake-on-lan (WOL) as a vehicle to give you a grounding in
this cmdlet's syntax. The ideas is that Invoke-Expression executes the string as if
you typed it at the cmd.exe command-line. I hope that you will be
able to modify my examples to suit
your task.
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.