Powershell split

“One Two Three”.split(” “)
One
Two
Three

$Result = “One Two Three”.split(” “)
$Result[1]
Two

“abcde” -match ‘(…)’ ; $matches
True

Name Value
—- —–
1 abc
0 abc

“abcde” -match ‘(^.*d)’ ; $matches
True

Name Value
—- —–
1 abcd
0 abcd

Transforming a string of text into an array

To continue on with the error report code which I started in the last post, I have now figured out how to break up my errpt.txt (get the text file) file into an array where each error is one element:

# This must current be run after
# $errorArray is initialized
# in the parent shell like this:
#
# PS> $errorArray = @();
# PS> type errpt.txt | . ./readerrpt.ps1
# PS> $errorArray[43]

$Line = ""
foreach ($element in $input)
{
    if($element -Match "-----------*")
    {
	$errorArray += , $Line
	    $errorArray.Count
	    $Line = ""
    }
    else
    {
	$Line += "$element`n"
    }
}

Change the Failure Groups on a GPFS disk

./mmchdisk icash change -d "gpfs13nsd::::10"
./mmdf icash

disk                disk size  failure holds    holds             free KB             free KB
name                    in KB    group metadata data       in full blocks        in fragments
--------------- ------------- -------- -------- ----- ------------------- -------------------
gpfs13nsd           104857600       -1 yes      yes         9514496 ( 9%)         82304 ( 0%)
gpfs12nsd           104857600       -1 yes      yes         9519616 ( 9%)         79368 ( 0%)
gpfs14nsd           104857600       -1 yes      yes         9346304 ( 9%)         72480 ( 0%)
gpfs15nsd           104857600       -1 yes      yes         9345536 ( 9%)         71344 ( 0%)

PowerShell versus Korn Shell example

As I dig deeper into PowerShell from an Administrator perspective, I try to use it as much as possible in my daily work as a true shell. In my opinion, you open up a shell at the beginning of the day and basically ‘live it in’. You do most of your work from it, and slowly surround yourself with quick little functions and tools. You then can create tiny ‘throw-away’ scripts over and over again to solve whatever small problem you face during the day. This has been my approach using the korn and bash unix shells and I think it is a great starting point for PS. This is very different from programming in a language like C# or even VB.

A shell seeks to solve very different problems from traditional compiled languages and can even help you to figure out what you are trying to figure out if you don’t initially know. Working in a shell can be a liberating and elegantly simply REPL experience. This is a stark contrast to using graphical interfaces to solve problems. While initial data extraction may be easier and faster, all of the pieces of your solution need to be reinvented and rewritten for a new application or tool and data must be extracted from one and placed into the next. Even if you could have a swiss-army knife graphical tool (this is probably excel), you still are limited to how its authors imagined that you could use it. By writing new scripts or functions or simply downloading new tools from the internet, the power of a shell is endless.

Since most of the problems I have to solve with a shell are unix problems, it might seem that powershell couldn’t help me. This isn’t true. Because both have a strong base of using the pipe ( | ) to cascade text from one command to another, I simply pipe my unix problem into my powershell and solve it in Windows rather than AIX. In this way I can get PowerShell ‘under my fingers’ so I can turn to it quickly.

In this case, I need to pull out MAC addresses from an error report, get rid of duplicates, and email the results to some one else, with one result per line. I have a cool command that can get what my initial error reporting command returns from unix into powershell on the fly, but for this comparison, my data always starts in a file called ‘errpt.txt’.

The data in the file mostly looks like this over and over again:

----------------------------------------------------
LABEL:          AIXIF_ARP_DUP_ADDR
Date/Time:       Fri Jun 19 22:41:54 CDT 2009
Node Id:         nortameraix14
Class:           S
Type:            PERM

Detail Data
DUPLICATE IP ADDRESS
0534 0622
MAC ADDRESS
0015 1763 E07B   <<<---- This is the line I need
----------------------------------------------------

In Korn Shell, I would run:

cat errpt.txt | grep "^00"
(which would return)
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
...

Since I noticed that every line I needed started with '00', I just piped into a unix grep command and matched to the regular expression for "line starts with 00". At this point I turn to my PowerShell pocket reference, which is sadly useless. Next I simply go to google and type 'powershell grep' and one of the top examples tells me about a command in powershell called select-string. So instead of wading through a big tutorial about powershell, I just do a strategic strike and find someone else's blog where they already make the comparison for me:

PS> type errpt.txt | select-string "^00"
(which would return)
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
0015 1763 E07B
0015 1763 DE95
0015 1763 E3AB
...

So the commands were almost identical:

unix: cat errpt.txt | grep "^00" 
PS:   type errpt.txt  | select-string "^00"

The only problem with my example is that in order to learn about 'select-string' I had to know to tell google that it needed to work like 'grep'. If I didn't know that I wouldn't have been able to leverage the knowledge of other unix guys who made the switch. All I can say is that once you learn about 'select-string', you have to potiential to type 'select-string unix' into google and you would learn about grep if you ever needed to. Maybe in ten years you will type 'select-string fooshell' into some search engine to learn about the next hot thing that would be similar to what is now 'select-string' in PowerShell.

At any rate, the next thing I need to do is show each unique line only one time, in unix the command is 'sort -u' which sorts the line first and then removes duplicate lines. I can just bring up my old command, go to the end of the line, add another pipe, and then put my sort in place:

cat errpt.txt | grep "^00" | sort -u
000C CAC4 8E4E
0014 2135 807C
0015 1763 DE95
0015 1763 E07B
0015 1763 E3AB
0015 C5FF 49D2

This is basically all I need to do. I now have a list of unique MAC addresses, one to a line.
In powershell, I now just have to understand how to do a 'sort -u', hit up-arrow to bring back my last command, and add it in.

I eventually find that I have to put two commands together. Here are the results sided by side:

ksh:   cat errpt.txt | grep "^00" | sort -u
PS:  type errpt.txt | select-string "^00" | sort | get-unique

So you might say that Korn shell wins since I have to type a little less to get to my result. I am going to give PowerShell a free pass this time because I know that its main goal in life is to manipulate rich .NET data in ways that Korn shell can't and that is where is ends its similarity with unix and really shines.

Regular Expression Usage (from get-help)

TOPIC
Regular expressions

SHORT DESCRIPTION
Using regular expressions in Cmdlet parameters in the Windows PowerShell

LONG DESCRIPTION

PowerShell supports a number of the regular expression characters:

Format Logic Example
——– ——————————- ———————–
value Matches exact characters “book” -match “oo”
anywhere in the original value
. Matches any single character “copy” -match “c..y”
[value] Matches at least one of the “big” -match “b[iou]g”
characters in the brackets
[range] Matches at least one of the “and” -match “[a-e]nd”
characters within the range.
The Use of a hyphen (–) allows
specification of contiguous
character.
[^] Matches any character except “and” -match “[^brt]nd”
those in brackets
^ Matches the beginning “book” -match “^bo”
characters
$ Matches the end characters “book” -match “ok$”
* Matches zero or more instances “baggy” -match “g*”
of the preceding character
? Matches zero or one instance “baggy” -match “g?”
of the preceding character
\ Matches the character that “Try$” -match “Try\$”
follows as an escaped character

PowerShell supports the character classes available in .NET regular
expressions
Format Logic Example
——– ——————————- ———————–
\p{name} Matches any character in the “abcd defg” -match “\p{Ll}+”
named character class specified
by {name}. Supported names are
Unicode groups and block
ranges. For example, Ll, Nd,
Z, IsGreek, IsBoxDrawing.
\P{name} Matches text not included in 1234 -match “\P{Ll}+”
groups and block ranges
specified in {name}.
\w Matches any word character. “abcd defg” -match “\w+”
Equivalent to the Unicode (this matches abcd)
character categories [\p{Ll}
\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}].
If ECMAScript-compliant behavior
is specified with the ECMAScript
option, \w is equivalent to
[a-zA-Z_0-9].
\W Matches any nonword character. “abcd defg” -match “\W+”
Equivalent to the Unicode (This matches the space)
categories [^\p{Ll}\p{Lu}\p{Lt}
\p{Lo}\p{Nd}\p{Pc}].
\s Matches any white-space “abcd defg” -match “\s+”
character. Equivalent to the
Unicode character categories
[\f\n\r\t\v\x85\p{Z}].
\S Matches any non-white-space “abcd defg” -match “\S+”
character. Equivalent to the
Unicode character categories
[^\f\n\r\t\v\x85\p{Z}].
\d Matches any decimal digit. 12345 -match “\d+”
Equivalent to \p{Nd} for
Unicode and [0-9] for non-
Unicode behavior.
\D Matches any nondigit. “abcd” -match “\D+”
Equivalent to \P{Nd} for
Unicode and [^0-9] for non-
Unicode behavior.

PowerShell supports the quantifiers available in .NET regular expressions,
the following are some examples of quantifiers

Format Logic Example
——– ——————————- ———————–
* Specifies zero or more matches; “abc” -match “\w*”
for example, \w* or (abc)*.
Equivalent to {0,}.
+ Matches repeating instances of “xyxyxy” -match “xy+”
the preceding characters
? Specifies zero or one matches; “abc” -match “\w?”
for example, \w? or (abc)?.
Equivalent to {0,1}.
{n} Specifies exactly n matches; “abc” -match “\w{2}”
for example, (pizza){2}.
{n,} Specifies at least n matches; “abc” -match “\w{2,}”
for example, (abc){2,}.
{n,m} Specifies at least n, but no “abc” -match “\w{2,3}”
more than m, matches.

All of the comparisons shown in the above examples in the table
above evaluate to true.

Note that the escape character for regular expressions is different
than that of the PowerShell. The character escape for regular
expressions is the backslash “\”.

SEE ALSO
For information about the match comparison operator, enter the
following command at the PowerShell command prompt:

help about_comparison_operators

See the MSDN Documentation on Regular Expression Language Elements

Note to Staff: Watermelon in the ice machine

I love this stuff, its hard to explain why.  I hope that one day as historians sift through what remains of culture, they don’t over look the little touches.  Notes about cleaning the coffee pot and not sitting on the counter are as much a part of what we are as anything we like to believe about ourselves:

To whomever put the watermelon in the ice machine:Please remove it immediately as well as the ice that it has been sitting in. Just as a helpful hint, the shell of a watermelon is not the cleanest in the world as it is handled by shoppers in the grocery store, gets transported in your vehicle and as well gets carried by you in to the building. Would you want to use the ice that is contaminated by all of that to put in your drink? Please be more considerate of others in the building and if you decide to bring an entire watermelon to work, please bring and use a cooler of ice to store it in, not the company ice machine.

If there are any questions, please feel free to contact me.

Thank you in advance for your cooperation.