Monthly Archives: July 2014

History cmdlets

There are 4 cmdlets for working with the history in a PowerShell session:

Add-History – adds an entry to the history
Clear-History – clears the history
Get-History -enables you see the current history
Invoke-History – enables you to run a line in your history

Get-History is the one I use the most.  You can use the UP arrow key to cycle back to an old command or use Invoke-History.

I’d only use Clear-History if I had a long running session and I was changing what I was working on.

Add-History is the odd one. I’ve never used it but it could be useful if you have a command you run often and you want to push it into the history when you start a session.

Be aware of  a  variable that controls history:

$MaximumHistoryCount  defaults to  4096 and is the maximum number of commands saved in the command history.

You can increase this up to 32768 if you desire but unless you put the command in your profile your next session will drop back to the default.

Its mandatory Jim but not as we know it

A uestion was asked on the forum about whether a parameter could be made mandatory sometimes and nor other – in particular if a parameter could be made mandatory if another parameter was used.

My initial thought was that it couldn’t but a little experimentation with parameter sets produced this:

function test {

[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$Param1,

[Parameter(Mandatory)]
[string]$Param2,

[Parameter(ParameterSetName = 'Set1')]
[switch]$Param3,

[Parameter(ParameterSetName = 'Set1',Mandatory)]
[Parameter(ParameterSetName = 'Set2')]
[string]$Param4,

[Parameter(ParameterSetName = 'Set2')]
[switch]$param5
)

"parameters accepted"

}

The function doesn’t do anything – its just to demonstrate the parameters

So this works:

£> test -Param1 x -Param2 y
parameters accepted

These work

£> test -Param1 x -Param2 y -param5
parameters accepted

£> test -Param1 x -Param2 y -param5 -Param4 z
parameters accepted

So you see that param4 when used in parameter set set2 is not mandatory

However,

£> test -Param1 x -Param2 y -param3
cmdlet test at command pipeline position 1
Supply values for the following parameters:
Param4: a
parameters accepted

So when you use parameter 3 you’re in parameter set set1 and param4 becomes mandatory.

Bit messy with the parameter decorators but it gets the job done

Welcome back

This has taken over from the old blog at http://msmvps.com/blogs/richardsiddaway

Not sure if I'll be able to get the historical posts back.  You'll find most of them at

http://richardspowershellblog.wordpress.com/

I'd recommend making that your primary view of my blogs

July 22 2014–deal of the day

Half off my book PowerShell and WMI. Use code dotd072214au at www.manning.com/siddaway2/ Also Half off PowerShell in Depth, Second Edition. Use code dotd072214au at www.manning.com/jones6/

Bad practice – – creating objects

Another in my occassional series on bad practices I’ve seen and recommend you avoid.  This time I want to look at creating objects. I recently saw some code that looked a bit like this:

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

$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name OperatingSystem -Value $os.Caption
$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 Bootup -Value $os.LastBootUpTime
$obj | Add-Member -MemberType NoteProperty -Name BiosType -Value $bios.BIOSVersion
$obj

This involves a lot more typing and effort than is required.

A much, much  simpler way is available:

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

$obj = New-Object -TypeName PSObject -Property @{
OperatingSystem = $os.Caption
Manufacturer = $comp.Manufacturer
Model = $comp.Model
Bootup = $os.LastBootUpTime
BiosType = $bios.BIOSVersion
}
$obj

The Property parameter takes a hash table of property names and values and populates the object. Much less typing and more obvious, to my mind.

Some people complain that use the Property parameter means that you lose the order of the properties.

Who cares – its an object. Access the properties as you need them.

If for some reason you need to be able to dictate the order of the properties then use an ordered hash table:

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

$props = [ordered]@{
OperatingSystem = $os.Caption
Manufacturer = $comp.Manufacturer
Model = $comp.Model
Bootup = $os.LastBootUpTime
BiosType = $bios.BIOSVersion
}

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

But however you use the hash table use it in preference to Add-Member for these scenarios.

Does Add-Member have a place. Yes. Use when you want to add one or two properties to an existing object.

DSC Resource Kit Wave 5

The next wave of the DSC resource kit has arrived – see http://blogs.msdn.com/b/powershell/archive/2014/07/17/powershell-dsc-resource-kit-wave-5-arrives.aspx for details

Docker and DSC for Linux

Docker is a way to “virtualise” applications on Linux machines. With DSC for Linux you can manage Docker instances http://blogs.technet.com/b/privatecloud/archive/2014/07/17/configuring-docker-on-azure-with-powershell-dsc.aspx

msmvps.com is down

My mirror blog on msmvps.com is down – as is the rest of the site.  It is being worked on. In the meantime transfer here

PowerShell Summit Europe 2014 – – update 5

Registration is now open. Access through Events menu at powershell.org

Formatting disks – – the new way

Last time I showed how to format disks using the Win32_Volume CIM class. If you need to perform this activity on a Windows Server 2012/Windows 8 or later system you can use a couple of cmdlets from the Storage module

Get-Volume | where DriveLetter -ne 'C' | Format-Volume -FileSystem NTFS -Confirm:$false –WhatIf

 

If you’ve not looked at the Storage module before there is a lot of useful cmdlets.