Monthly Archive

Categories

Monthly Archives: September 2015

PowerShell DSC for Linux 1.1

PowerShell DSC for Linux 1.1 has been released.

 

http://blogs.msdn.com/b/powershell/archive/2015/09/30/powershell-dsc-for-linux-version-1-1-is-now-available-and-new-linux-resources.aspx

 

With bug fixes, support for the separation of configuration and node ids  and new resources this release makes it easier than ever to manage your Linux boxes through DSC

Controlling copies

I was recently asked about ‘forcing’ a copy so that only the files you want are copied

 

Consider a folder with lots of files. I want to copy those files that start with the letter t and have a txt extension

PS> Get-ChildItem -Path C:\test2\t*.txt

    Directory: C:\test2

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       08/08/2015     14:14              0 test.txt
-a----       08/08/2015     14:16              0 test1.txt
-a----       08/08/2015     14:17              0 test2.txt
-a----       08/08/2015     14:17              0 test3.txt
-a----       08/08/2015     14:17              0 test4.txt

 

I want to copy to c:\test1 which is empty

 

PS> Get-ChildItem -Path C:\test1\
PS>

 

Defining the copy is simply a matter of setting the path to files you want

PS> Copy-Item -Path C:\Test2\t*.txt -Destination C:\test1\ -Verbose
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test2\test.txt Destination: C:\test1\test.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test2\test1.txt Destination: C:\test1\test1.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test2\test2.txt Destination: C:\test1\test2.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test2\test3.txt Destination: C:\test1\test3.txt".
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Test2\test4.txt Destination: C:\test1\test4.txt".

PS> Get-ChildItem -Path C:\test1\

    Directory: C:\test1

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       08/08/2015     14:14              0 test.txt
-a----       08/08/2015     14:16              0 test1.txt
-a----       08/08/2015     14:17              0 test2.txt
-a----       08/08/2015     14:17              0 test3.txt
-a----       08/08/2015     14:17              0 test4.txt

WMF 5.0 production preview–installed

I’ve installed WMF 5.0 PP on a number of Windows 2012 R2 (full GUI and server core). Installation was over the top of a mixture of PowerShell 4.0 and various versions of PowerShell 5.0 previews.

 

Installation worked in all cases.

 

I’ve not noticed any problems with using powershell 5.0 in this environment

Win32_ReliabilityRecords Class

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.

Everything is recycled

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 Smile

Error handling for DNS lookups

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"
}

PowerShell and DevOps Global Summit–call for topics extended

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/

Updating help problem

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.

Where next?

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

Bad habits when creating objects

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