Monday, July 12, 2010

powershell: my 3 favorite functions (1 of 2)


i have every intention of continuing our lessons in x86 assembler and nasm, but right now there is something else on my mind. let's pause our look at assembler for a little while and discuss powershell.

powershell is a very powerful scripting language for managing windows hosts. in fact, somewhere else i've gone on record saying, “if you administer a windows network, you must learn powershell.” i meant that.

powershell is very flexible and much easier to customize than dos batch files of yore. the powershell profile, reminiscent of the .profile used in the unix bourne shell, allows you to collect your personal preferences and code where they will always be available when you run a script. to that end there are three functions that, for me and my profile, are “must-haves.” i'll share them with you here.

pause

the first is the simple dos pause command. i'm not sure why powershell doesn't have such a useful command. it has proven its worth through decades of batch files. nevertheless, the powershell team has left it out. no matter. we can write our own with hardly any effort at all.

5
6
7 # pause:
8 # accepts a string
9 # Prints a message on the screen, then waits until a key is pressed before
10 # proceeding. If a string is not passed to the function the default string
11 # will be used. Simulates the "pause" command in DOS.
12
13 function pause ($msg = "Press any key to continue...") {
14 $msg
15 $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown") | Out-Null
16 } # end function pause
17

this function will accept a string to print as your pause message, but will default to the old dos standard “press any key” in absence of one. it uses the console's ui.rawui.readkey method to capture a raw key press, and redirects the output to the out-null cmdlet to avoid echoing the key's character on screen. a sample run of this function should look like this:

C:\> pause
Press any key to continue...
C:\> pause "I can't do that, Dave."
I can't do that, Dave.
C:\>

test

the second function is test, borrowed from the linux bourne-again shell, or bash. the test command takes an expression and determines whether the result is true or false. now, the bash command isn't really intended to be user-friendly: the result of the test is stored in the program's exit code. this is useful in a shell script, but i like to use test to help me debug scripts and check that my expressions are giving me the results i expect, so i've expanded it a little for powershell.

18
19 # test:
20 # expects an expression
21 # Accepts any valid expression and evalutes it for truth. Prints a message
22 # indicating that the expression evaluated true or false.
23
24 function test ($expr) {
25 if ($expr) {
26 Write-Host -ForegroundColor Green "True"
27 }
28 else {
29 Write-Host -ForegroundColor Red "False"
30 }
31 } # end function test
32

we can use this function from the interactive prompt to determine if some logical expression does what we think it does. a sample run of the function would look like this:

C:\> test (3 -gt 4)
False
C:\> test ("Bill" -is [string])
True
C:\> test (42 - 6 -lt 30 -or 56 + 9 -gt 70)
False
C:\>

now we can easily test even a complex expression to see whether it should be true or false when we use it in a script.

be sure to check back next time for the coolest of my three favorite functions.

No comments:

Post a Comment