Winter Scripting Games (2013) Event 1

Teresa Wilson, the Scripting Wife, talks about the first event in the 2013 Winter Scripting Games and how she solved the scenario

Here’s the one-liner I submitted:

Get-Volume -CimSession (            
New-CimSession -Credential (Get-Credential) -ComputerName my.WS.2012.FQDN -Authentication Kerberos            
) | ? DriveType -eq "Fixed" |            
Select -Property DriveLetter,FileSystemLabel,            
@{l="Percentage of free disk space";e={'{0:N2}'-f($_.SizeRemaining*100/$_.Size)}},            
@{l="Free space (GB)";e={'{0:N2}'-f($_.SizeRemaining/1GB)}},            
@{l='Total Size (GB)';e={'{0:N2}'-f($_.Size/1GB)}}

My appproach was different. The event description stated that we could use WMI or CIM. I prefered the latter for two reasons.
First they are new and shipped with Powershell 3:

The scenario stated that:

In order to support future abilities to connect to other computers,
you have been asked to use WMI or CIM to retrieve this information.

I’ve a Windows 8 computer member of a workgroup and a Windows 2012 server joined to an Active Directory domain. I knew that ‘Windows Remote Management’ is enabled by default on 2012 box, whereas WMI isn’t (mainly because of firewall rules). In other words, it was a great opportunity to use CIM cmdlets.

To solve the scenario, I also knew that Windows 8 has also a brand new built-in storage module. But, as my Windows 8 computer isn’t domain joined, I’ve had to create a CIM session with the New-CimSession cmdlet to be able to connect to the remote server.

Then the scenario mentioned that I’ve to display the amount of free space on each fixed-disk volume. The Get-Volume cmdlet returns an array of Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume objects as output that has a ‘DriveType’ property. To filter the output, I chose to use the new Powershell 3.0 syntax. Surrounding curly braces can be omitted when using simple filters with the Where-Object cmdlet that has ? for alias.

The last step was about formatting. Teresa Wilson used the format-table cmdlet that has the ability to rename properties and change the display by calculating an expression. I prefered the Select-Object cmdlet to achieve this formatting as this cmdlet can actually create a new calculated property using the same syntax the Format-Table uses. I also used Select-Object as its output can be piped into any formatting cmdlet.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s