Showing posts with label profile. Show all posts
Showing posts with label profile. Show all posts

Wednesday, July 14, 2010

powershell: my 3 favorite functions (2 of 2)


you may have noticed that when you print a really, really long string in powershell, the text is wrapped at the edge of the console. if your screen is 80 characters wide, your string gets wrapped at 80 characters. you may also have noticed that this wrapping isn't very intelligent. your string will be wrapped at exactly 80 characters, even if that means that the line gets broken in the middle of a word. just look through the powershell help to see what i mean.

textwrap

python has a nifty little module called textwrap that wraps a string the smart way. i don't know a lot about it—i only use the parts i need. it is not my intent to re-write the python module for powershell, only to borrow the concept with my own textwrap function. the idea is to take a long string and a maximum width in characters and return a string formatted so that each line does not exceed the width. whole words should be preserved at the end of each line so that the text is readable. because this is a more complex function, we will build it incrementally and talk about it along the way.


C:\> function textwrap ($longstring, $width) {
>>> $word_list = -split $longstring
>>> $out_strings = @("")
>>> $i = 0
>>>

the simple way to begin is to take the long string and convert into an array of words using the string split() method or, if you're using powershell 2, the split operator as i did here. we're going to convert the string to an array of strings, each no longer than the desired width. we'll store this in $out_strings. notice we initialized the array with an empty string. that way we can write to $out_strings[0] without getting errors about non-existent array elements. we'll start indexing our array at zero with our good buddy $i.


>>> foreach ($word in $word_list) {
>>> if ($out_strings[$i].Length + $word.Length -gt $width) {
>>> $i++
>>> $out_strings += ""
>>> }
>>> $out_strings[$i] += $word
>>>

what we want to do is concatenate each word to the end of the string in our array as long as that wouldn't make the string longer than our maximum width. we use the foreach loop to fetch each word from the list. in the loop we test to see if we can safely add the word to the end of the string in the first array element. once we've added as many words to the string as we can, we increment the array index and append a new empty string to the array. then we add the next word to the end of the new string.


>>> if ($out_strings[$i].Length + 1 -le $width) {
>>> $out_strings[$i] += " "
>>> }
>>> }
>>> return ($out_strings -join "`n")
>>> }
>>>

before we leave the foreach loop we need to add a space to the end of the word we just concatenated onto our string. that is, as long as it doesn't put us above our width. the if statement above adds a space if there is room to do so. finally, we return our array of strings as a single string with the join operator. we separate them with a newline character so that the new string prints on multiple lines. given a long string stored in $ls a sample run of this function might look like this:


C:\> "-" * 25
-------------------------
C:\> textwrap $ls 25
This is a really long
string. It is wrapped in
a way that doesn't chop
up words.
C:\>

the finished function appears below. i've added code so that it can even deal with hyphenated words and do the right thing. i'll let you examine that to see how it works. until next time.


33
34 # textwrap:
35 # expects a string and an int
36 # returns a string
37 # Intelligently wraps a long string to a maximum width.
38
39 function textwrap ($long_string, $width) {
40 $word_list = -split $long_string
41 $out_strings = @("")
42 $i = 0
43
44 foreach ($word in $word_list) {
45 # If adding the word would make the final string too long, start a
46 # new string:
47 if ($out_strings[$i].Length + $word.Length -gt $width) {
48 # If the word is hyphenated, try just the first part:
49 if (([regex] "\w+\-\w+").IsMatch($word)) {
50 if ($out_strings[$i].Length + 1 +
51 $word.Split("-")[0].Length -le $width) {
52 $out_strings[$i] += $word.Split("-")[0]
53 $out_strings[$i] += "-"
54 $word = $word.SubString($word.IndexOf("-") + 1)
55 }
56 }
57 $i++
58 $out_strings += ""
59 }
60 $out_strings[$i] += $word
61
62 # Add a space if it doesn't make the string too long.
63 if ($out_strings[$i].Length + 1 -le $width) {
64 $out_strings[$i] += " "
65 }
66 }
67
68 return ($out_strings -join "`n")
69 } # end function textwrap
70

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.