Powershell and SMS Part 2 – parsing bad MIF files

I’ve been working on my SMS site this weekend, and had a couple issues that would have taken a bit of time to deal with. However, given a couple lines of Powershell, I turned minutes of work (which turn into hours when scaled) into instant gratification.

Example 2:

In the course of rebuilding my SMS site last week, I managed to encounter some documented bugs, and long story short, I had a slew of bad MIF files that I needed to contend with. The gist of what needed to be done was to open each MIF file in a text editor and find the computer name associated with the bad MIF. When dozens of these start showing up, it takes a lot of time to open each file – (OK, not THAT much time, but again, as the problem scales, it does become significant).

Enter Powershell:

With Powershell’s ability to use WHERE-OBJECT(?) and FOREACH-OBJECT(%),
one can parse the whole directory in a couple of seconds and end up with only the computernames:

gc F:\SMS\inboxes\auth\dataldr.box\BADMIFS\*.mif | ? {$_ -like “*netbios*”} | % {$_.split(“<> “)} | ? {$_ -like”*BL-*”} | sort -unique

So how does it work? First, let’s break down the commands:

gc – this is an alias for Get-Content, which basically just
reads a file

? – this is an alias for Where-Object – in other words, a filter

$_ –  this is the way Powershell
interprets “the current object” in the pipeline

% – this is an alias for Foreach-Object – in other words, a loop

sort – I’m using sort here because it has a –unique parameter, which I use to
return a single instance

So the English version of the command above is:

1) Read all files with a MIF extension in this directory

2) Only return lines which contain the word netbios

3) Split those lines up anywhere there’s a space, a <, or a >.

4) Only return lines which contain “BL-“ (all my computer names contain these letters).

5) Return only unique instances of that computername.

Most people probably won’t be able to use THAT particular code, but I just wanted to illustrate how powerful Powershell is when it comes to trudging through hundreds of lines of text to give you just what you’re looking for.

Powershell and SMS Part 1 – WINS

I’ve been working on my SMS site this weekend, and had a couple issues that would have taken a bit of time to deal with. However, given a couple lines of Powershell, I turned minutes of work (which turn into hours when scaled) into instant gratification.

Example1: WINS

Since I work in an Active Directory environment in which the schema is not extended for SMS, clients MUST have WINS entries setup in order to communicate with the SMS Management Point. This is generally not an issue, since DHCP gives out the necessary WINS entries. However, sometimes the techs
need to set static IPs, and they often forget to set the WINS since it doesn’t SEEM to have any relevance any more, and the WINS entries are on another tab of
the TCP/IP settings. In fact, the one thing that can go wrong (that I know of) in our environment is that the SMS management point can’t contact the client. The error, of course, doesn’t say that. It just says “verify your client is online”, or something like that. Well, I don’t want to have to RDP to the client to check and/or set the WINS entries. Enter Powershell.

function check-wins ([string]$comp = “localhost”)

{

$IP = gwmi win32_networkadapterconfiguration -comp $comp | ? {$_.IPAddress -ne $null}
$pri = $IP.WinsPrimaryServer
$sec = $IP.WinsSecondaryServer
write-host “Primary WINS Server is $pri”
write-host “Secondary WINS Server is $sec”
}

function set-wins ([string]$comp = “localhost”)

{
$IP = gwmi win32_networkadapterconfiguration -comp $comp | ? {$_.IPAddress -ne $null}
$IP.SetWinsServer(“129.79.1.200″,”129.79.5.200”)
}

You can type check-wins computername to get the results of a given computer. Just typing check-wins will give you the local computer’s settings.

You can type set-wins computername to set the value to IU settings. Just typing set-wins will set the local computer’s WINS.

Very fast and effective!