Tag Archives: PowerShell

PowerShell loves enumerations

One of the things that make PowerShell oh-so-great is its tight integration with .NET Framework. As a simple example, in the framework there are many pre-define values, often stored as enumerated types. Enums are great because they allow us to define a set of values and then let the user (or program) choose among these values.

Another great thing with PowerShell is that it makes command arguments ‘pickable’ on the command line. So if I’m running a command and I am unsure of which arguments can be used I can simply press tab and I will se the first valid parameter value.
Let me show you the simple beauty of Enums together with PowerShell scripts.

function Get-SpecialFolderPath
{
  param ( [System.Environment+SpecialFolder]$SpecialFolder )
  return [System.Environment]::GetFolderPath($SpecialFolder)
}

This function takes one argument, the alias of a special folder, and returns the physical location of the special folder. The special folders that Windows knows about are define in the enumeration Environment.SpecialFolder. You can use the function like this:

Get-SpecialFolderPath -SpecialFolder Cookies
C:\Users\Joakim\AppData\Local\Microsoft\Windows\INetCookies

This in itself may not impress you (me neither:-)). But when you use this function toghether with your own function or other PowerShell cmdlets it becomes a great help for the user. Let’s say you want to quickly check what is in the users Cookie folder. How would you know where the cookies are stored? With this function you don’t, it is enough to type:

Get-SpecialFolderPath *tab*

As this is a static blogpost I can’t show you what happens when I press the <tab> key. but what does happen is that you will get the first value in the SpecialFolder enumeration, “Admintools”. Press tab again and you’ll get the second value which is “ApplicationData”. Another press of the tab key gies you “CDBurning” and so on.  You can of course avoid pressing tab many times by providing part of the name of the folder your are looking for:

Get-SpecialFolderPath coo *tab*

When you press tab this will expand to:

Get-SpecialFolderPath Cookies

A more relevant example: If you want to list all items on the users desktop you can combine Get-ChildItem with Get-SpecialFolder as this:

Get-ChildItem (Get-SpecialFolderPath Desktop)

This will give you a listing of all items in your dekstop folder. Again, the beauty is that you do not need to know the exact path to the Desktop folder.

Using enumerations to get input can really make your PowerShell scripts a lot easier to use and safer. So I hope you take some time to play around with using enumerations with your functions and cmdlets!

$env:HOME doesn’t work in PowerShell $PROFILE

Today I wanted to setup my PowerShell $PROFILE to make it easier to work with all the modules I learned about at the PowerShell Summit. I wanted to add an additional path to the environment variable $PSModulePath. Something that should be a picece of cake.

I store additional modules in the folder ‘Documents\WindowsPowerShell\Modules’ under my userdirectory (C:\Users\Joakim). So simply joining the path using Join-Path and add the result to $ENV:PSModulePath should do the trick:

$env:PSModulePath += ';'+ (Join-Path -Path ${env:HOME} -ChildPath 'Documents\WindowsPowerShell\Modules')

Unfortunately that didn’t work. And it confused me. Executing this on the PowerShell commandline worked fine, but when I put the line above in my $PROFILE it gave me this error:

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\Joakim\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:3 char:44
+ $env:PSModulePath += ';'+ (Join-Path -Path ${env:HOME} -ChildPath 'Do ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

I understand what the error says and apparently the environment variable HOME is not instantiated when the $PROFILE is executed. This really confuses me. I already have the following line in my profile and it workes like a charm:

[System.Environment]::SetEnvironmentVariable('PATH',"C:\Users\Joakim\SkyDrive\Util;$env:PATH")

So environment variables are available when $PROFILE is executing… After a bit of frustration I changed from using

$env:HOME

to using

$env:USERPROFILE

and then it works!

So a word of advise if you want to find the users homedirectory when in the context of $PROFILE. Do not use $env:HOME

If anyone can explan when the HOME variable is instatiated vs the USERPROFILE… I’m listening 😉

Basic inventory of HyperV virtual machines using PowerShell

Working at large scale with thousands of servers customers often asks for a list of machines with various properties for each machine.

Most of the time the customer want this information in an simple format (read CSV, the World isn’t as advanced as you like!) so that they can use it internally. In this blogpost I will show how you can get the information about memory, CPU count etc for a set of Hyper-V machines from Virtual Machine Manager via PowerShell.

Start a PowerShell command line and load the PowerShell Snap-In for Virtual Machine Manager.

Add-PSSnapin -Name Microsoft.SystemCenter.VirtualMachineManager

Now we can work with the commands made available to us by the Snap-In, if you want to find all the commands that are available issue:

Get-Command -Module Microsoft.SystemCenter.VirtualMachineManager

So let’s begin by loading information about all our machines from the VMM host into a variable named $VMs

$VMs = Get-VM -VMMServer hyperv-vmm01.sth.basefarm.net

What the above command does is to load all of the VMs on the host HYPER-V-01.mydomain.com into the variable $VMs. This means we will only do one call to the server which avoid generating unnecessary load.

Now let’s check how many machines we have:

$VMs.Count

And now that we know we have machines to query, let’s find out what attributes exists (things we can get into our output)

$VMs | Get-Member -MemberType Property

For example, to find all macines that are powered off:

 $VMs | where { $_.Status -eq 'PowerOff' } | select VMHost, name , Memory, CPUCount , Status

The above example adds some complexity to the command, but it is to filter so we only see machines that have the status is ‘PowerOff’.

Now let’s get what we wanted from the beginning, a list of machines for a specific customer. The list should include name of the VM host, VM name, memory, number of CPUs and current status.

 $VMs | where { $_.Name -Match 'CUST*' } | select VMHost, name , Memory, CPUCount , Status

This will list all machines who’s name begins with ‘CUST’. So we now have found what we wanted!

But instead of copying & pasting this we want to write the result to a CSV file so we can send that to the customer. Let’s make that easier by getting the output of the above command into a variable named $result

 $result = $VMs | where { $_.Name -Match 'LFO*' } | select VMHost, name , Memory, CPUCount , Status

Now our ‘report’ is stored in the $result variable and we can use standard PowerShell to export it to a CSV file:

$result | Export-Csv -NoTypeInformation -Delimiter ';' .\report.csv

Now our report is available in a CSV file on the file ‘report.csv’ (in the current directory)

A very basic way of getting your Hyper-V inventory out!

Quick way to name your NICs in Windows Servers

If you, like me, manage many servers, it’s essential to name network adapters in a way that makes it easy to troubleshoot issues when they arise.

In complex networks with thousands of servers and all servers connected using multiple paths a consistent naming standard is very important!

PowerShell and the cmdlets available in Windows Server makes naming adapters a breeze. The servers we usualy deploy have built in four (4) port network adapters. We like to name the Windows NICs the same as is the default in Linux; eth0, eth1, etc.

In the following example we name the adapters eth0, eth1, eth2 and eth3 in Windows. The NIC with the lowest MAC address gets the name eth0 etc. (If you prefer to to start naming adapters from eth1 change the variable $NICs to 0):

 $NICs = -1
 Get-NetAdapter Etherne* | Sort-Object MacAddress | % { Rename-NetAdapter -InterfaceAlias $_.InterfaceAlias -NewName eth$NICs }

PowerShell really makes life easy 😉

Enumerate UPNP devices

At home I have a Sonos system. It’s probably the best of any device that I have owned in terms of quality, usability and sheer happiness. The Sonos system has been working just fine for many years. It just works!

Recently I was fortunate enough to get a direct fiber into my house. Needless to say this triggered me to update my home network. Part of this upgrade was to install Ubiquiti long range access points for better performance and coverage as well as a new firewall. After this network upgrade Sonos system then started to behave strangely which brings me to the reason for this post!

I had to troubleshoot the Sonos system, a good start would be to see that all the devices were available and OK. A quick google search found a few PowerShell examples for how to use the UPnP.UPnPDeviceFinder COM object. Here’s one example that I found:

$finder = New-Object -ComObject UPnP.UPnPDeviceFinder;
$devices = $finder.FindByType("upnp:rootdevice", 0)
foreach($device in $devices)
{
    Write-Host ---------------------------------------------
    Write-Host Device Name: $device.FriendlyName
    Write-Host Unique Device Name: $device.UniqueDeviceName
    Write-Host Description: $device.Description
    Write-Host Model Name: $device.ModelName
    Write-Host Model Number: $device.ModelNumber
    Write-Host Serial Number: $device.SerialNumber
    Write-Host Manufacturer Name: $device.ManufacturerName
    Write-Host Manufacturer URL: $device.ManufacturerURL
    Write-Host Type: $device.Type
}

If you run this you get what you want, here’s a partial output:

---------------------------------------------
Device Name: BUBBA
Unique Device Name: uuid:4d696e69-444c-164e-6b41-101f743be078
Description: MiniDLNA on Debian
Model Name: Windows Media Connect compatible (MiniDLNA)
Model Number: 1
Serial Number: 12345678
Manufacturer Name: Justin Maggard
Manufacturer URL: http://www.debian.org/
Type: urn:schemas-upnp-org:device:MediaServer:1
---------------------------------------------
Device Name: 192.168.0.6 - Sonos CONNECT:AMP
Unique Device Name: uuid:RINCON_000E5726C94601400
Description: Sonos CONNECT:AMP
Model Name: Sonos CONNECT:AMP
Model Number: ZP120
Serial Number:
Manufacturer Name: Sonos, Inc.
Manufacturer URL: http://www.sonos.com/
Type: urn:schemas-upnp-org:device:ZonePlayer:1

This output is fine but it is not done in a PowerShell way. Newcomers to PowerShell often make things needlessly complex. While the above script works it is not the best way do list all UPnP devices. Let’s make the above code easier and more importantly, more useful:

$finder = New-Object -ComObject UPnP.UPnPDeviceFinder;
$devices = $finder.FindByType("upnp:rootdevice", 0)
$devices

This script will give the same output but much more useful. Line one instatiates the UPnP COM object, line two tells the oject to find all ‘root’ UPnP devices on our local network and store the result in the $devices variable. The third line is a bit of PowerShell magic. Since the variable $devices is really an object PowerShell understands that it contains many objects inside itself. So by simply stating the variable name $devices on a single line PowerShell will output the object properties like this:

IsRootDevice     : True
RootDevice       : System.__ComObject
ParentDevice     :
HasChildren      : False
Children         : System.__ComObject
UniqueDeviceName : uuid:4d696e69-444c-164e-9d41-101f743be078
FriendlyName     : BUBBA
Type             : urn:schemas-upnp-org:device:MediaServer:1
PresentationURL  : http://192.168.0.9:8200/
ManufacturerName : Justin Maggard
ManufacturerURL  : http://www.debian.org/
ModelName        : Windows Media Connect compatible (MiniDLNA)
ModelNumber      : 1
Description      : MiniDLNA on Debian
ModelURL         : http://www.debian.org/
UPC              :
SerialNumber     : 12345678
Services         : System.__ComObject

IsRootDevice     : True
RootDevice       : System.__ComObject
ParentDevice     :
HasChildren      : True
Children         : System.__ComObject
UniqueDeviceName : uuid:RINCON_000E5836C94601400
FriendlyName     : 192.168.0.6 - Sonos CONNECT:AMP
Type             : urn:schemas-upnp-org:device:ZonePlayer:1
PresentationURL  :
ManufacturerName : Sonos, Inc.
ManufacturerURL  : http://www.sonos.com/
ModelName        : Sonos CONNECT:AMP
ModelNumber      : ZP120
Description      : Sonos CONNECT:AMP
ModelURL         : http://www.sonos.com/products/zoneplayers/ZP120
UPC              :
SerialNumber     :
Services         : System.__ComObject

Admittedly this doesn’t look as pretty as the output from the first script. But, the result is a true objectand much more useful. In order to see what information is available, issue the most useful PowerShell command of all, Get-Member:

$devices | Get-Member

   TypeName: System.__ComObject#{3d44d0d1-98c9-4889-acd1-f9d674bf2221}

Name             MemberType Definition                            
----             ---------- ----------                            
IconURL          Method     string IconURL (string, int, int, int)
Children         Property   IUPnPDevices Children () {get}        
Description      Property   string Description () {get}          
FriendlyName     Property   string FriendlyName () {get}          
HasChildren      Property   bool HasChildren () {get}            
IsRootDevice     Property   bool IsRootDevice () {get}            
ManufacturerName Property   string ManufacturerName () {get}      
ManufacturerURL  Property   string ManufacturerURL () {get}      
ModelName        Property   string ModelName () {get}            
ModelNumber      Property   string ModelNumber () {get}          
ModelURL         Property   string ModelURL () {get}              
ParentDevice     Property   IUPnPDevice ParentDevice () {get}    
PresentationURL  Property   string PresentationURL () {get}      
RootDevice       Property   IUPnPDevice RootDevice () {get}      
SerialNumber     Property   string SerialNumber () {get}          
Services         Property   IUPnPServices Services () {get}      
Type             Property   string Type () {get}                  
UniqueDeviceName Property   string UniqueDeviceName () {get}      
UPC              Property   string UPC () {get}

As you can see in the above output we get a number of properties and one method, IconURL. Now you can pick and choose which properties you want and you can do magic with them. Let’s say you want the FriendlyName, ModelName and the ManufacturerName. Try this:

$devices | Select-Object FriendlyName, ModelName, ManufacturerName

The great thing here is that you can pick and choose exactly the output you want. In a later post I will show you what you can do with this.