Wednesday, June 23, 2010

Powershell Quick Hit - Text in quotes

I'm beginning to piece together the intricacies of text pattern matching using regex wildcards. It's tricky stuff, but that's not the point of this Quick Hit, it's just something interesting that I picked up along the way.

Today's Powershell quick Hit has to do with text in single or double quotation marks. Powershell treats each of them very differently, especially when variables and wildcards are being used.

Consider these 2 variable strings:

PS:\ $date = get-date
PS:\ $single = 'Today is $date, and my Windows Directory is $env:windir.'
PS:\ $double = "Today is $date, and my Windows directory is $env:windir."
PS:\ $single
Today is $date, and my Windows Directory is $env:windir.
PS:\ $double
Today is 06/23/2010 22:51:00, and my Windows directory is C:\WINDOWS.

Friday, June 18, 2010

Powershell Quick Hit - Doing it X times, again....

Continuing with the theme of using an easy array (1..20) with a forEach to do a code block a defined number of times, I cobbled this together yesterday. I can't figure out how to get the output on a single line yet, but it works; will ping entire subnet and report which ones respond.

(1..255) | % {
"192.168.0.$($_)"; Test-Connection "192.168.0.$($_)" -quiet -count 1 | % {
IF($_ -eq $True){"IP above pings"}
}
}

The % is an alias for ForEach

Thursday, June 17, 2010

Powershell Quick Hit - Doing it X times

I learned a cool and easy trick today for running a block of code a defined number of times.

It started with a simple commandlet called get-random, which, as you might expect, is a random number generator. You can feed it -min and -max values.

So "get-random -min 1 -max 10" generates a random number between 1 and 10. I wondered how I could do that 20 times.

There's a "for" looping contruct that works like this:

FOR ($i = starting value; $i -le X (less than or equal to) (max value); $i++ (increment by 1)
{do code here}

PS>for ($i = 0; $i -le 10; $i++) {$i}
0
1
2
3
4
5
6
7
8
9
10

Combining those 2 concepts together works fine:

for ($i = 0; $i -lt 20; $i++) {Get-Random get-random -min 1 -max 10}
generates 20 random numbers between 1 and 10.

But here's the cool Powershell quick hit - look how easy Powershell makes doing this using forEach and a range (1..20)

PS>1..20 | ForEach {get-random -min 1 -max 10}
2
5
7
8
4
2
7
4
9
7
1
7
9
2
1
9
6
2
4
4

Powershell Quick Hit - Out Gridview

Bring up a PS prompt, and type $a = $env:windir

You just assigned $a to your Windows directory.

You could just as easily have done $a = C:\Windows, but I thought I'd throw 2 tips in for the price of one. :)

Just to verify, do:

get-childitem $a (or dir $a, or ls $a, or gci $a...)

and you should see the contents of your windows directory. Now here comes the fun part. Are ya ready?

pipe that to "out-gridview"

get-childitem $a | out-gridview

And watch what happens.

See the "Add Criteria" button at the top? Press it, select all the options. Cool eh??

Are you in favor of irradating food?