Start Posting to – NOW !!!

Hal posted a comment a while back asking if I would consider putting some of the functions i had been blogging about up on I had heard of it and even used some of the scripts up there but for whatever reason I hadn't bothered to post anything. Hal finally convinced me to put some code up there. I have to say, its ridiculously easy.

My 0.02 is this should be the default shared online repository for Powershell scripts and functions. So I have one question for you.. Why are you still reading this and not posting your latest and greatest script up to PoshCode?

Go and do it.. now !




   1: PS C:\> Get-GreatestSwimmerEver
   4: NumberGoldMedalsIn2008    : 8
   5: NumberWorldRecordsIn2008  : 7
   6: LifetimeOlympicGoldMedals : 14
   7: FirstName                 : Michael
   8: LastName                  : Phelps
   9: GreatestSwimmerEver       : True
  13: PS C:\>

A thank you note from an intern

We had several high school students working for us this summer as interns. It was a great experience. I just had to share a section of a thank you note I received from one of them the day they completed their internship.

"I thought it would never be possible for me like coding again, after a year of programming class. But after a week of PowerShell, I liked coding again .. but only in PowerShell !!"

I love the fact that Powershell helped me get a high school student interested in scripting, and that it is quite literally "Changing the world one line at a time."

PowerShell Function Set-IPAddress

I recently came across the need to configure IP information on a Network interface using a script. This is definitely something that can be accomplished with NETSH,  but I was looking for a way to do it with native PowerShell using Get-WmiObject. If there is a choice, I tend to prefer to stick with native Powershell Cmdlets and functions rather than running cmd based tools, but that is just my personal opinion and you can take it or leave it.

My goal was to be able to specify a Network Interface by name, ie "Local Area Connection" and pass it an IP address, mask, gateway, and 1 or 2 DNS servers. In order to accomplish this there were two WMI classes that I needed. The first is Win32_NetworkAdapter. The second is Win32_NetworkAdapterConfiguration. The real key (pun intended) was to figure out a way to join the information from these two classes so that I knew for sure that I was working with the same network interface. It turns out that both classes have a property called InterfaceIndex.

Win32_NetworkAdapter has a property called netconnectionid which is equal to the name of the Adapter, ie "Local Area Connection".

With that information, here is a function that you can use to specify settings for a NIC based on the name of the NIC.

   1: function Set-IPAddress {
   2:         param(  [string]$networkinterface,
   3:                 [string]$ip,
   4:                 [string]$mask,
   5:                 [string]$gateway,
   6:                 [string]$dns1,
   7:                 [string]$dns2,
   8:                 [string]$registerDns = "TRUE"
   9:          )
  12:         #Start writing code here
  13:         $dns = $dns1
  14:         if($dns2){$dns ="$dns1,$dns2"}
  15:         $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
  16:         $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
  17:         $NetInterface.EnableStatic($ip, $mask)
  18:         $NetInterface.SetGateways($gateway)
  19:         $NetInterface.SetDNSServerSearchOrder($dns)
  20:         $NetInterface.SetDynamicDNSRegistration($registerDns)
  22: }

Hope you find this useful.


PowerShell Function Set-NetInterfaceName

Now that you are able to set an IP address given a name of a network interface, you may want to change that name.

Here's a quick little function to do that.

   1: function Set-InterfaceName {
   2:         param(  
   3:                 [string]$oldName,
   4:                 [string]$newName
   5:          )
   7:         $NicInterfaceName = gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $oldName}
   8:         $NicInterfaceName.netconnectionid = $newName
   9:         $NicInterfaceName.put()
  11: }