Being close to the RES Wisdom fire means I know which new features are going to be in the next release, and which requests didn’t make it. One of the very cool things that is going to be in RES Wisdom 2009 is support for Microsoft Windows PowerShell. You can run PowerShell scripts as a resource, or you can paste a script directly onto the script tab (for example a script you’ve copied from Microsoft http://www.microsoft.com/technet/scriptcenter/scripts/msh/default.mspx?mfr=true).

 


 

The way PowerShell is implemented is what makes it really cool. All the information that would normally be sent to the PowerShell console is captured and logged in the job results. Let’s say you want to know which services are running and can be stopped. The command you can use to get that is:
get-Service * | where {$_.Status –EQ “Running” –AND $_.CanStop} |Sort-Object -property ServiceType | format-Table name, ServiceType, status, CanStop, -auto

Now if you schedule this task on an agent you’ll get the results back on the “Console Output” tab.

 

Set IP Address

OK, so PowerShell is cool and is going to be in the next release. Like I said, being close to the fire, I know what’s going to be in there, but also what’s not. It’s just not possible to get every enhancement request, however cool, into the next release of a product. One request I made some time ago is the ability to set the IP address of a network interface card (NIC). Well, it just didn’t make it. After struggling at the bottom of the list it just fell off in favor of other, even cooler, features (like PowerShell, for instance). So, I decided to build my own task for this, using the PowerShell task. he first bump I met on the road to my first PowerShell script was how to determine which NIC was the one to set the IP Address on. A lot of PCs out there today have more than one NIC. Most notebooks have an Ethernet and Wireless connection for instance, most server hardware have double NICs, etc, etc. I came up with three ways to do this:

When thinking of how to do this, t

  • The name of the NIC
  • The current (DHCP) IP Address
  • The MAC Address

This is done in this part of the script:

  1. $nics | foreach {
  2. $pos =$_.Caption.IndexOf(“] “)
  3. $nicname=$_.Caption.SubString($pos+2)
  4. if ($_.IPAddress -like “$[0-SearchText]” -or $_.MACAddress -like “$[0-SearchText]” -or $nicname -like “$[0-SearchText]”)
  5. { if ($tempmac -NE $_.MACAddress)
  6. { $niccount =$niccount + 1
  7. $tempmac =$_.MACAddress
  8. }
  9. $nic = $_
  10. $Index = $nic.Index
  11. }
  12. }

Line 4 checks if either the IP Address, the MAC Address or the NIC Name resemble the SearchText that is entered as a parameter from RES Wisdom. The script will only run successfully if the text to search for returns only 1 NIC. While I tested it on several different VMs I found that some NICs appear more than once in the WMI list. All lines had the same IP address AND the same MAC Address. I decided to treat those as one NIC aswell. This is done on line number 5. I found out that setting the last referenced NIC (the last time the WMI list refers to the same NIC) is the one you have to change to set the IP Address. The next step was to determine what to do with the NIC. I decided to build in three different actions to perform on the NIC.

  • Set the NIC to DHCP
  • Copy the DHCP Address to a static address
  • Set a manually provided static IP Address

So you can enter an IP Address (or part thereof) and select DHCP2STATIC. If the IP Address is found the IP Properties provided by DHCP are set as static properties:

 

Because the PC already knows the right IP settings you don’t need to provide anything else. Same goes for setting a NIC back to DHCP. If you think you can use this script, please feel free to download the Building Block. Of course the script is provided as is: download Building Block