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

Posted in Powershell | Leave a comment

msmvps.com is back

My blog on msmvps.com is back at http://blogs.msmvps.com/richardsiddaway/

Posted in General | Leave a comment

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/

Posted in Books, Powershell | 2 Comments

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.

Posted in Powershell | Leave a comment

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

Posted in Powershell | Leave a comment

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

Posted in Powershell | Leave a comment

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

Posted in General | Leave a comment