PowerShell for Windows Admins


September 29, 2015  1:42 PM

Win32_ReliabilityRecords Class

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

The Win32_ReliabilityRecords class was introduced with Windows 7. It contains information from the Windows Event Log related to system reliability.

The most interesting properties are the Message and the TimeGenerated

£> Get-WmiObject -class win32_reliabilityRecords | select -First 1 | fl Message, TimeGenerated
Message       : Installation Successful: Windows successfully installed the following update: Definition Update for  Windows Defender – KB2267602 (Definition 1.207.1367.0)
TimeGenerated : 20150929170914.620000-000

Notice the usual delightful date format on TimeGenerated

Easiest way to resolve this and get sensibly formatted date is to use the CIM cmdlets

£> Get-CimInstance -ClassName win32_reliabilityRecords | select -First 1 | fl Message, TimeGenerated
Message       : Installation Successful: Windows successfully installed the following update: Definition Update for  Windows Defender – KB2267602 (Definition 1.207.1367.0)
TimeGenerated : 29/09/2015 18:09:14

Now you have a date you can read easily.

September 29, 2015  8:47 AM

Everything is recycled

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DevOps

I was just thinking this morning about the DevOps phenomenon. When I started in IT it wasn’t unusual for people to do development work and operations – especially in smaller organisations.

Over the years this changed and specialisation became the name of the game – you were a C# developer or a java developer or a Windows admin or a network guy…

|Now with DevOps being the flavour of the month the admin guy, or developer, who can at least have an intelligent conversation about the other guys issue’s is in big demand again.

The cynical side wonders how long before someone has the bright idea that separating developers to concentrate on development and ops staff to concentrate on admin stuff is more efficient  🙂


September 29, 2015  5:32 AM

Error handling for DNS lookups

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DNS, Powershell

Interesting question on the forum regarding the Resolve-DNSname cmdlet. This is part of the DNSclient module introduced with Windows 8.

If the DNS record is found everything is good

£> Resolve-DnsName W12R2DSC -Server server02 | ft -a

Name                   Type TTL  Section IPAddress
—-                   —- —  ——- ———
W12R2DSC.Manticore.org A    1200 Answer  10.10.54.204

If the record isn’t found you get an error

£> Resolve-DnsName W12R2DSC3 -Server server02
Resolve-DnsName : W12R2DSC3 : DNS name does not exist
At line:1 char:1
+ Resolve-DnsName W12R2DSC3 -Server server02
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (W12R2DSC3:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName

If you want to gracefully handle that error you use try-catch

$computer = ‘W12R2DSC3’
try {
Resolve-DnsName $computer -Server server02 -ErrorAction Stop
}
catch {
Write-Warning -Message “Record not found for $computer”
}


September 28, 2015  1:28 PM

PowerShell and DevOps Global Summit–call for topics extended

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DevOps, Powershell

The call for topics for the PowerShell and DevOps Global Summit (renamed PowerShell Summit) has been extended.

If you’re thinking of submitting please do so.

If you want some ideas fro topics we’ll consider – see http://powershell.org/wp/2015/09/16/speaking-at-powershell-summit-2016-topic-ideas-for-aspiring-speakers/


September 28, 2015  12:28 PM

Updating help problem

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A question on the forum posed this question – why does update-help fail on Windows 10 in this case:

£> Update-Help -Module PSDesiredStateConfiguration -Force
Update-Help : Failed to update Help for the module(s) ‘PSDesiredStateConfiguration’ with UI culture(s) {en-GB} :
Unable to retrieve the HelpInfo XML file for UI culture en-GB. Make sure the HelpInfoUri property in the module
manifest is valid or check your network connection and then try the command again.
At line:1 char:1
+ Update-Help -Module PSDesiredStateConfiguration -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand

The usual reason is that Update-Help can’t find the help file. This is either because the URL in the module manifest is wrong or the help file isn’t available yet. In this case I suspect the help file isn’t available yet as DSC may still change as Server 2016 hasn’t shipped yet.


September 27, 2015  1:04 PM

Where next?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

With the demise of the European PowerShell Summit organised by powershell.org what should happen next in Europe as far as a PowerShell conferences are concerned?

The probability is that there will be a number of regional conferences. Tobias already has a German language conference and people at the Stockholm Summit were talking about an event based in Scandinavia.

What about the UK?

I been talking to a number people and ideas are beginning to coalesce.

Suggestions so far include a one day PowerShell Saturday and a PowerShell conference modelled on the recent Summits.

What do you want to see?

Its no use putting events together if its not what the UK PowerShell community actually wants.

Leave a comment if you have views for or against either of the two ideas and if you have another option please let me know


September 27, 2015  11:47 AM

Bad habits when creating objects

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I’m seeing a lot of code recently that looks like this

$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$os = Get-CimInstance -ClassName Win32_OperatingSystem

$obj  = New-Object -TypeName PSObject

$obj | Add-Member -MemberType NoteProperty -Name Name -Value $comp.Name
$obj | Add-Member -MemberType NoteProperty -Name  Manufacturer -Value $comp.Manufacturer
$obj | Add-Member -MemberType NoteProperty -Name Model -Value $comp.Model
$obj | Add-Member -MemberType NoteProperty -Name OS -Value $os.Caption
$obj | Add-Member -MemberType NoteProperty -Name SP -Value $os.ServicePackMajorVersion
$obj | Add-Member -MemberType NoteProperty -Name Architecture -Value $os.OSArchitecture

$obj

A couple of calls to CIM classes then a long laborious way to create an output object.  Way, way, way back in the day you had to create objects like this. Life and PowerShell have moved on.

There are much simpler ways to get the job done that involve far less typing. The more you type the greater the chance of introducing an error. Debugging a mass of Add-member calls is not something you ever want to do.

So easy way number 1.

$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$os = Get-CimInstance -ClassName Win32_OperatingSystem

$props = @{
‘Name’ = $comp.Name
‘Manufacturer’ = $comp.Manufacturer
‘Model’ = $comp.Model
‘OS’ = $os.Caption
‘SP’ = $os.ServicePackMajorVersion
‘Architecture’ = $os.OSArchitecture

}
$obj  = New-Object -TypeName PSObject -Property $props

$obj

Create a hash table of the properties you want your object to have and pass that to New-Object. I prefer to create the hash table as a separate step as I find it easier to read.

This is far, far quicker than multiple calls to Add-Member. Its also safer as there’s a lot less to type.

You can make the process a bit simple – easy way number 2:

$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$os = Get-CimInstance -ClassName Win32_OperatingSystem

$obj = [PSCustomObject]@{
‘Name’ = $comp.Name
‘Manufacturer’ = $comp.Manufacturer
‘Model’ = $comp.Model
‘OS’ = $os.Caption
‘SP’ = $os.ServicePackMajorVersion
‘Architecture’ = $os.OSArchitecture

}

$obj

Create the object directly from the hash table – note that you have to make the type PSCustomObject not PSObject.

There’s no excuse for creating objects with multiple calls to Add-Member


September 9, 2015  12:58 PM

Scripting Games–August 2015

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The August 2015 Scripting Games puzzle was a simple ‘get data from a web service’ request. You had to retrieve data from a given web service

£> Invoke-RestMethod -Uri ‘http://www.telize.com/geoip’ |
Select-Object -Property  longitude, latitude, continent_code, timezone
longitude latitude continent_code timezone
——— ——– ————– ——–
-0.13     51.5 EU             Europe/London

Apart from the inaccuracy in the continent information – England isn’t part of Europe Smile – its pretty straight forward. The trick is remembering that Invoke-RestMethod exists and selecting out the properties you need.

If you wanted to productionise this you end up with the follwoing function as a minimum

function Get-GeoInformation
{
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias(‘ip’, ‘address’)]
[ValidateScript({([System.Net.IPAddress]::TryParse($_, [ref]’127.0.0.1′)) -eq $true})]
[string[]]$ipaddress
)

BEGIN {} #end begin
PROCESS {
try {
if ($ipaddress) {
foreach ($ip in $ipaddress){
if ($psCmdlet.ShouldProcess(“$ip”, ‘Retreiving geo-location data’)) {
Write-Verbose -Message “Retrieving data for $ip”
Invoke-RestMethod -Method GET -Uri “http://www.telize.com/geoip/$ip” -ErrorAction Stop
}
} # end foreach ($ip in $ipaddress)
} # end if ($ipaddress)
else {
Write-Verbose -Message ‘Retrieving data’
if ($psCmdlet.ShouldProcess(‘default ip address’, ‘Retreiving geo-location data’)) {
Invoke-RestMethod -Uri ‘http://www.telize.com/geoip’-ErrorAction Stop
}
} #end else
} # end try
catch [System.Net.WebCmdletWebResponseException] {
throw ‘Error connecting to web service’
}
catch {
throw ‘Unresolved error’

} # end catch

} #end process
END {} # end end

<#
.SYNOPSIS
Get-GeoInformation returns data from the geoip web service
at http://www.telize.com/

.DESCRIPTION
Get-GeoInformation returns data from the geoip web service
at http://www.telize.com/

If an IP address is presented to the function that will be sent to
the web service otherwise the current public IP address of the user,
or their ISP, is used.

.PARAMETER  ipaddress
IP address to be tested. The IP address will be validated on input
and an error thrown if invalid

.EXAMPLE
£> Get-GeoInformation
longitude      : -0.13
latitude       : 51.5
asn            : AS9105
offset         : 1
ip             : 88.108.92.247
area_code      : 0
continent_code : EU
dma_code       : 0
timezone       : Europe/London
country_code   : GB
isp            : Tiscali UK
country        : United Kingdom
country_code3  : GBR
No IP address is presented so the user’s public (ISP address) is used

.EXAMPLE
£> Get-GeoInformation -ipaddress 88.108.92.247
longitude      : -0.13
latitude       : 51.5
asn            : AS9105
offset         : 1
ip             : 88.108.92.247
area_code      : 0
continent_code : EU
dma_code       : 0
timezone       : Europe/London
country_code   : GB
isp            : Tiscali UK
country        : United Kingdom
country_code3  : GBR

An IP address is used

.NOTES
Written in response to the August 2015 Scripting Games puzzle
from PowerShell.org

.LINK
http://www.telize.com
http://powershell.org/wp/2015/08/01/august-2015-scripting-games-puzzle/
#>

}

The function takes one, or more, IP addresses as an optional parameter and after testing its a valid IP address proceeds to call the web service for each IP address.

The –Confirm and –WhatIf parameters are available on the function via the  CmdletBinding:

[CmdletBinding(SupportsShouldProcess=$true)]

I’ve also added comment based help.

Its interesting that a single PowerShell pipeline goes to over 100 lines of code when you add in the validation, error handling and other aspects of producing production code


September 8, 2015  4:37 AM

WMF 5.0 Production Preview

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The PowerShell team recently announced the availability of the WMF 5.0 Production Preview. This is available for:

Windows Server 2012 R2
Windows Server 2012
Windows 2008 R2 SP1
Windows 8.1
Windows 7 SP1

You’ll need .NET Framework 4.5 installed to run WMF 5.0

see – http://blogs.msdn.com/b/powershell/archive/2015/08/31/windows-management-framework-5-0-production-preview-is-now-available.aspx

NOTE  WMF 5.0 PP will NOT load on Windows 10 – as WMF 5.0 PP is effectively the version on Windows 10 plus some bug fixes.

Windows 10 will have an update mechanism when WMF 5.0 is RTM – see http://blogs.msdn.com/b/powershell/archive/2015/08/06/windows-management-framework-wmf-5-0-roadmap.aspx


August 30, 2015  10:50 AM

Manning Deal of the Day–31 August 2015

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

Deal of the Day August 31: Half off my book Learn Active Directory Management in a Month of Lunches. Use code dotd083115au at https://manning.com/books/learn-active-directory-management-in-a-month-of-lunches

Learn ConfigMgr 2012 in a Month of Lunches and Learn SQL Server Administration in a Month of Lunches also included

 


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: