Ezine 182 PowerShell -confirm
Ezine 182 PowerShell -confirm
Which ever version of PowerShell you are using, take the time to learn about
-confirm and -whatIf. The benefit is that you can test new commands
before you action them, this is particularly useful when contemplating
changing settings or deleting objects.
♣
I feel like an expectant father waiting for the birth of PowerShell v
2.0. Life is on hold, at least as far as this ezine is concerned,
because I dare not encourage people to use the CTP (Community Technology
Preview) of PowerShell 2.0 on live systems.
The reason I have chosen to feature -confirm this week is because it's
important to get into the habit of testing before you issue complex
PowerShell commands. The danger is that when
you are under pressure, perhaps we are performing an unfamiliar task and we
change
something that we later regret. If only we had tested the unfamiliar
syntax with -confirm then we could have seen the error in the trial run and
thus prevented a disaster.
In a nutshell think of -confirm as putting PowerShell into testing mode.
Actually, -confirm has a sister command called -whatIf, this switch truly
simulates the command but without making any changes.
This week I have three disparate tasks, however, once I reveal the
elements then perhaps you can see how they are linked. What I want to
show you is how to delete temporary files, to list the Environmental
Variables, and to learn about the -confirm parameter. Naturally, these
techniques will work equally well in PowerShell v 2.0.
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
For the sake of safety, and for the sake of a smooth learning progression, let us start
with a script which merely lists the operating system's built-in environmental variables.
# PowerShell script to list Environmental variables clear-host
get-ChildItem -path Env: |sort-Object name
Note 1: In PowerShell's the get-ChildItem
cmdlet usually involves a drive letter and folder, however, this example accesses the env:
namespace.
Note 2: '| sort-Object name' is an optional refinement
to order the output of get-ChildItem.
Note 3: Naturally, you should see Tmp and Temp
amongst the list of 30+ environmental variables.
Before we issue a command to delete files, let us merely list the
temporary files. For this task we employ two additional parameters: -include,
which filters the file extension, and -recurse which loops through the files and seeks out
any sub-directories.
# PowerShell script to list .tmp files clear-host $Location = "$Env:Temp"
get-ChildItem $Location -include *.tmp -recurse
Note 1: If you don't get any results, substitute *.* for *.tmp.
If you still don't get any results try a different value for the path controlled
by the $Location variable.
Note 2: Confession, when I first ran this script I
forgot the dollar sign. Whereas in the previous example -path Env: correctly listed the variables,
here $Env:Temp needs the dollar sign to access a specific environmental variable,
plain Env:Temp does not work.
Now we are ready to build on the success of the first two scripts. Here is
where we unleash remove-Item, which is the PowerShell command to zap stuff.
Incidentally there is no delete verb PowerShell consistently uses the pattern 'remove-Noun'. Now
the whole point of this example is to master the safety catch in the form of -confirm.
When the script runs, if you are in anyway unsure choose 'No' meaning don't delete anything.
# This script will delete all files in the user's Temp folder
$Location ="$Env:Temp" get-ChildItem $Location -recurse | remove-Item
-confirm
Expected result: PowerShell asks you if you want to
remove a whole bunch of temporary files. You could respond with 'No',
or 'No to All'. Next, you could run the same script again and this
time respond with 'Yes', or even, 'Yes to All'.
Challenge 1: Substitute -whatIf for -confirm.
Challenge 2: Research the remove verb with: get-Command
-verb remove
Note: -confirm and -whatIf only work with pure PowerShell
commands, I cannot get them to work with WMI syntax. Actually, this is
not such a big loss as when scripting WMI one is usually retrieving information
and not removing stuff.
Summary of -confirm
If you are coming from scripting languages such as VBScript, you may not be
aware that PowerShell has two handy commands for testing before you finally
unleash a command. All that you do is append -confirm or -whatIf to the
intended code.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
• PowerShell Tutorials •
Methods • Cmdlets
• PS Snapin •
Profile.ps1 • Exchange 2007
•
Command & Expression Mode •
PowerShell pipeline (|) •
PowerShell 'where' •
PowerShell 'Sort'
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
|