PowerShell for Windows Admins


January 27, 2017  4:48 AM

wmic deprecated

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

I saw a forum post today where the question involved the use of the wmi command line tool wmic.

Wmic was deprecated in Windows Server 2012 – https://technet.microsoft.com/en-us/library/hh831568(v=ws.11).aspx. It will eventually be removed.

You should use the CIM cmdlets instead of wmic. The syntax is much easier and the resultant code is easier to understand.

A little known fact – the PowerShell project was originally started as a replacement for wmic.

January 24, 2017  3:10 PM

Community Lightning Demos

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

We are continually evolving the content we present at the PowerShell Summit. This year we’re bringing back something that was a feature of the early PowerShell Deep Dives and Summits – the Community Lightning Demos. We have a session set aside on Wednesday afternoon for this. Timescales will depend on the number of people wanting to show something.

In the words of PowerShell MVP Warren Frame who’s organising this for us:

Ever wanted to present at Summit but were unsure if you could? This is your opportunity to present something you’ve discovered to your peers in the PowerShell community. A code trick, or tip, a new module you’ve created, an open source module or a feature of a cmdlet that’s relatively unknown.. The list goes on and on. Anything PowerShell, or DevOps related that you think is cool and that will interest other people is a suitable topic. We’re looking for 5-10 minute demos. Something you’ve done, discovered, solved or run up against. This is your opportunity to “give back” to our community by sharing your knowledge. Make sure its something you can present from your laptop and that you don’t need extensive Internet access. A sign up sheet will be available Sunday, Monday and Tuesday. We just need your name and topic. Who knows you may be asked to present a full session at the following Summit. Some of our best speakers started in the Lightning Demos sessions of past events.

This is your opportunity to start presenting to a knowledgeable and appreciative audience. In past events we’ve had some amazing things come to light – things the PowerShell team didn’t realise about PowerShell. If you have something to share please consider signing up for this.


January 24, 2017  2:37 PM

WMF 5.1 now available

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The download for WMF 5.1 for down level operating systems is now available:

https://blogs.msdn.microsoft.com/powershell/2017/01/19/windows-management-framework-wmf-5-1-released/

WMF 5.1 can be installed on Windows 7 and 8.1 plus Windows Server 2008 R2, 2012, 2012 R2

Windows 10 and Server 2016 already have PowerShell 5.1 and don’t need this install.

if installing on Windows 7 or Server 2008 R2 the installation process has changed – READ THE RELEASE NOTES OR BE PREPARED FOR A LOT OF EXTRA EFFORT


January 12, 2017  10:48 AM

Using $_

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The $_ symbol seems to be causing confusion from some recent forum questions I’ve seen.

$_ represents the current object on the pipeline – if you want to know why $_ was chosen you’ll have to read PowerShell in Action!

You can use $_ in a number of situations – in commands that perform an action on every object (or selected objects) on the pipeline. Here’s some of the commoner usages:

In the early versions of PowerShell you used it in the filter script of Where-Object

Get-Process | Where-Object -FilterScript {$_.CPU -gt 50}

This is more usually written as

Get-Process | Where {$_.CPU -gt 50}

As of PowerShell v3 if you are filtering on ONE property you can simplify the syntax

Get-Process | Where CPU -gt 50

which is a truncated version of

Get-Process | Where -Property CPU -gt -Value 50

When you write it like this its obvious what is happening. Get in the habit of thinking of the syntax in this manner even if you write in the shortened form

If you need to filter on TWO or more properties you have to use the old style syntax

Get-Process | Where {$_.CPU -gt 50 -AND $_.Handles -gt 1000}

In all of these cases you’re comparing a property of the current pipeline object against a value. If the comparison is true the object is passed onto the next step of the pipeline. If its false the object is discarded.

You can use $psitem in place of $_ if you prefer

Get-Process | Where {$psitem.CPU -gt 50}

In the Foreach-Object cmdlet you can use $_ to refer to the current object

Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true" |
ForEach-Object {
$ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($_.DeviceId)"
$props = @{
Name = $_.NetConnectionId
Product = $_.ProductName
DHCP = $ip.DHCPEnabled
IP   = $ip.IPAddress
}
New-Object -TypeName PSObject -Property $props
}

$psitem also works in this situation

The third common usage of $_ is in select-object in calculated fields (you can do the same in Format-Table)

Get-CimInstance -ClassName Win32_LOgicalDisk -Filter "DriveType=3" |
select DeviceId, VolumeName,
@{N='Size(GB)'; E={[math]::Round($_.Size / 1GB, 2)}},
@{N='Used(GB)'; E={[math]::Round(($_.Size - $_.FreeSpace) / 1GB, 2)}},
@{N='Free(%)'; E={[math]::Round(($_.FreeSpace / $_.Size) * 100, 2 )}}

In this example we’re changing the size to GB from bytes and calculating the used space and the % free space. Again you could use $psitem instead of $_

ONE PLACE YOU CAN’T USE $_  IS IN A FOREACH LOOP

This fails

$nics = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true"
$data = foreach ($nic in $nics) {
$ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($_.DeviceId)"
$props = @{
Name = $_.NetConnectionId
Product = $_.ProductName
DHCP = $ip.DHCPEnabled
IP   = $ip.IPAddress
}
New-Object -TypeName PSObject -Property $props
}
$data |Format-List

To recap $_ (or $psitem) is used to represent the current object on the pipeline. You can us it in commands that are performing an action on every object on the pipeline.


January 9, 2017  1:49 PM

Foreach confusion: Part two

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

My post on foreach seems to have generated a bit of confusion. I was trying to use the calculations as examples not a statement that you should do things that way – there were statements to that effect in the post but they seem to have been missed.

Any way, to try and resolve the confusion here’s another couple of examples. This time getting network adapters and finding the matching IP address.

First using Foreach-Object on a pipeline

Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true" |
ForEach-Object {
  $ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($psitem.DeviceId)"
  $props = @{
    Name = $psitem.NetConnectionId
    Product = $psitem.ProductName
    DHCP = $ip.DHCPEnabled
    IP   = $ip.IPAddress 
  }
  New-Object -TypeName PSObject -Property $props
} | Format-List

You can’t pipe Get-CimInstance into another call to the same cmdlet so you need to solve the problem using foreach.

Now the same problem solved using a foreach loop

$nics = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "NetEnabled = $true"
$data = foreach ($nic in $nics) {
  $ip = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index = $($nic.DeviceId)"
  $props = @{
    Name = $nic.NetConnectionId
    Product = $nic.ProductName
    DHCP = $ip.DHCPEnabled
    IP   = $ip.IPAddress 
  }
  New-Object -TypeName PSObject -Property $props
} 
$data |Format-List


January 6, 2017  3:55 PM

Is PowerShell just for administration?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Opinion, Powershell

Two related questions were left on my blog recently.

Is PowerShell just for administration?

Should I learn PowerShell, VBScript or cmd tools?

PowerShell was introduced as Microsoft’s automation engine for the Windows platform. It includes a scripting language, a shell and  the Integrated Scripting Environment (ISE). Other additions over the years have included Powershell workflows and Desired State Configuration.

The extension of PowerShell to Linux and Mac extends the administrative capabilities.

I would say that PowerShell  is primarily about the administration of Windows machines. This will be extended in time but at the moment its a Windows based administration tool.

Does that mean that its all you can do with it – Very definitely Not for example I recently wrote a script that would look at a large set of photos and move them into the correct folder based on the month they were created. Doing this by hand for a few photos is easy – for hundreds you need a script.

Over the years I’ve seen PowerShell code to do many non-administrator based things – for instance:

– manage and modify photos and videos

– play space invaders

– calculate the cooking time of a chicken given its weight

– work out the apparent temperature using air temperature, wind speed and altitude

– create web sites

PowerShell is .NET based which means you can work with just about the whole of the .NET framework in your PowerShell code. You can even write GUI applications in PowerShell if you want.

If you have a task that you perform on a Windows machine its very probable that a script could be written to perform that task. Creating the script may be very easy or very difficult but it should be possible.

If you want to do this sort of thing you need to:

– work out how to do the task manually

– break that down into a number of steps

– create the code to perform each step

– join it all together and test

If any one has suggestions for tasks they’d like to do leave a comment and I’ll see if I can suggest how you could do it

PowerShell or VBScript or cmd tools is fairly easy decision for me.  VBScript will receive no further development as far as I’m aware. The cmd tools are OK for specific jobs but don’t have the breadth that PowerShell does. CMD batch files are no where as caapble asa PowerShell script.

Learn PowerShell and use it as you want to make your computer use easier


January 3, 2017  11:47 AM

foreach confusion

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

One of the biggest sources of confusion to people learning PowerShell is foreach.

Don’t worry – it is confusing. Having just read the help file about_foreach its doubly confusing. That help file desperately needs a re-write

Foreach is used in two separate ways with two separate meanings

Firstly there is the foreach language statement or foreach loop. This is used to iterate over a collection of items

$disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" 
foreach ($disk in $disks){
  $fp = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($disk.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($disk.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

In this case the Win32_LogicalDisk CIM class is used to create a collection of disk information. The foreach language statement (loop) is used to work through that collection. For each disk in the disks collection we calculate the percentage free space. If that percentage is less than 60 a warnign message is displayed otherwise an information message is displayed – for versions of PowerShell before 5.0 use Write-Host instead of Write-Information.

PLEASE DON’T COMMENT THAT YOU WOULD DO THIS ANOTHER WAY – SO WOULD I. I’M USING IT TO ILLUSTRATE FOREACH!

Alternatively you could do this

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |
foreach {
  $fp = [math]::Round(($psitem.FreeSpace / $psitem.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($psitem.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($psitem.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

Get the disk information but this time pass it into foreach on the pipeline

AGAIN PLEASE DON’T COMMENT THAT YOU WOULD DO THIS ANOTHER WAY – SO WOULD I. I’M USING IT TO ILLUSTRATE FOREACH!

This is where the confusion arises – foreach is used in two different ways.

In the second set of code – foreach is actually an alias for Foreach-object so the code could be written

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |
ForEach-Object -Process {
  $fp = [math]::Round(($psitem.FreeSpace / $psitem.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($psitem.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($psitem.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

which makes more sense and is less confusing

Couple of tips:

Foreach in the pipeleine is an alias for foreach object and foreach on its own is the language statement

When writing code use Foreach-Object rather than foreach to reduce confusion


January 2, 2017  3:44 AM

New Year book offer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
book, Powershell

The New Year starts very nicely with POwerShell in Action, 3e being Mannings deal of the day for 3 January 2017

Half off my book Windows PowerShell in Action, Third Edition. Use code dotd010317au at http://bit.ly/2iw7MD2

Links should be directed to the DOTD’s page at https://www.manning.com/dotd


December 31, 2016  5:35 PM

PowerShell in 2017

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

As 2016 winds down what do we have to look forward to from PowerShell?

In January WMF 5.1 should become available for down level clients. This brings PowerShell 5.1 to Windows 7 SP1, 8.1, 2008 R2 SP1, 2012, 2012 R2

Notice that Windows 8 and 2008 aren’t on that list

If you’re using DSC I’d recommend bringing all your DSC servers and target machines up to 5.1

April brings the PowerShell Summit in Bellevue Washington USA and the PowerShell conference in Hannover Germany

Hopefully, Bruce Payette and I will get PowerShell in Action third edition  finished and published

PowerShell 6.0 will continue to be developed and the current alpha builds may turn into betas

Beyond that – who knows – watch this space…


December 31, 2016  1:36 PM

2016–a PowerShell perspective

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

2016 was important as the 10th anniversary of PowerShell being released as a web download – announced November 2006 at TechEd/IT Forum in Barcelona. The PowerShell team organised a 8 hour web cast on Channel 9 that I was privileged to be part of.

We also saw PowerShell 5.1 become available on Windows 10 and server 2016 (but no PowerShell for managing containers!!!)

April saw the PowerShell Summit which returned to Bellevue. The sell out event went very well with some great sessions and participation from the community and especially from the PowerShell team who went above and beyond. Already looking forward to next years event.

I also attended the PowerShell conference in Hannover, Germany and had a great time.

May saw the one day WinOps event in London – hope that becomes an annual event

The big – OK gigantic –event was the move of PowerShell core to the open source and the release of alpha versions of the code for Windows, Linux and Mac. You can mow remote between Windows and Linux!! How this develops is going to be interesting as it is a potentially breaking event for PowerShell or it could be the greatest thing ever only time will tell.

2016 was an interesting year and a fitting 10th anniversary for one of the best things to come out of Microsoft


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: