Tuesday, 19 April 2011

Doing a 'start /wait' in PowerShell

Countless times I’ve had to write a script that would start a process, normally some sort of installation routine, but have needed to ensure that the process had finished doing its thing before passing control back to the script.  Normally if you’re writing a batch/cmd file you’d do something like this..

start /wait notepad.exe  

..or in VBscript something like this..

Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.Run("notepad.exe"),1,True  

As part of our Windows 7 rollout here at the Uni I want to reduce the number of startup/shutdown & logon/logoff scripts and if we have to script something then it would be done in PowerShell.  As such I’ve needed to reproduce the ‘start /wait’ type functionality that I’ve used before in .cmd & .vbs scripts in a .ps1.  After a bit of research seems like the easiest way to do it is with something like this:

$process = [System.Diagnostics.Process]::Start("notepad.exe")
$process.WaitForExit()  

And passing parameters is easy as well, you just need something like:

$process = [System.Diagnostics.Process]::Start("notepad.exe","c:\temp\sometextfile.txt")
$process.WaitForExit()  

Photosynth on the iPhone

Discovered today that Microsoft have made an iOS version of Photosynth available, and best of all it’s free.  Have already tried it out and must admit that it works pretty well.  So okay, the 360ยบ panoramas of my office aren’t that exciting but it’s still a very cool app.  App details can be found here.

Friday, 15 April 2011

Another dose of reality…

Once again Dilbert seems a little too close to reality for comfort… Dilbert.com

Thursday, 14 April 2011

Less is not always more

In the past I’ve made various posts giving examples of how PowerShell can be perfect for the the lazy sysadmin through not having to type as much.  This can be done through using aliases instead of the full cmdlet and truncating parameters (only available with PS v2 remember, you old-school v1 users will have to upgrade).  For example, why type all of…

Get-ChildItem u:\scripts -Include *.ps1 –Recurse

…when you could simply type…

 gci U:\Scripts -i *.ps1 –r

For simply running stuff from within PS the shorter version is ideal.  Problem arises when you start to copy-and-paste your one-liners into a script, that then gets put into production, that then might have to be revised / debugged / fixed by someone else in the future?  Am guilty as charged as have done this myself on numerous occasions, in an attempt to affirm my alpha-geek status.  Trouble is it all falls apart when one of your team asks you what one of your scripts is doing and you struggle to come up with a reasonable explanation.

So what lessons am I going to try and learn from this?

  • If I’m going to insist on trying to make my typing as little as possible I’ll back up my code with enough comments (but then kind of defeats the object of reduced typing!)
  • Use full cmdlet and parameter names in scripts (and comments!) as if I don’t its only going to come back and bite me anyway.

Monday, 11 April 2011

Assigning multiple values in PowerShell

Some time ago I posted a PS one-liner to display the number in the Fibonacci sequence up to 10000000.  I learnt something new about PowerShell today regarding assigning values to variable so thought I’d use the same one-liner to demonstrate.  Originally I assigned a starting value to two variables separately, like this…

$1 = 0; $2 = 1;

The neat trick I found today was that you can do the same with…

$1,$2=0,1;

…which now makes the one-liner now look like…

$1,$2=0,1;$3=$1+$2;do{$3;$1=$2;$2=$3;$3=$1+$2} while ($3 -lt 10000000)

Makes for slightly less typing (well one less “=” anyway, but they all add up for the lazy sysadmin), but its just continues to prove that PowerShell is so flexible in how it lets you use it.

Wednesday, 8 December 2010

Send email using PowerShell

I like it when you come across something new and today’s nugget is the PowerShell cmdlet Send-MailMessage.  I’ve used mailing scripts with VBS in the past, for example when I wanted email notification when a script had finished but the beauty of the PowerShell method is that you can do it with a single line of code.  As a bare minimum all you need is something like:

image

Okay, so I did say that you only needed a single line of code but I used the back-tick (highlighted) just to make it more readable.

You can find the TechNet article on the Send-MailMessage cmdlet here.

Tuesday, 7 December 2010

Checking BADMIFS

Recently there has been a slight increase in the number of files appearing the BADMIFS folder on one of our SCCM servers.  Given that these files get assigned a seemingly random name the only way to identify which client machine they’ve come from to identify any pattern is to open each one.  If you’ve only got a couple of BADMIFs then this task isn’t too onerous but anymore than that you need to look to automate the checks.  What I came up with was a…wait, can you guess?…PowerShell script that checks if there are any BADMIFS on either server and if found then extracts the particular line that contains the computer name and outputs it all to a text file.  A sanitised version of the code I came up with is below:

image