As you’ve maybe noticed, default cmdlets for PVS are returning text array and not regular .NET objects – since Powershell is object-oriented scripting language, it can be quite complicate to manipulate with the output if you want to process it automatically (and not just display on the screen).
As a temporary workaround, I wrote a function that can convert output from MCLI.exe to regular objects with properties – this should make it much easier to use it in the code.
The usage is very simple – just execute MCLI.exe with any arguments and pipe the output to the function called ConvertFrom-MCLI – you will automatically get array of objects in your output.
Function ConvertFrom-MCLI {
Begin {
[array]$PvsLines = @()
}
Process {
$PvsLines += $_
}
End {
[array]$ResultArray = @()
:NextLine ForEach ($Line in $PvsLines) {
If ($Line.Length -eq 0) {Continue}
If ($Line[0] -eq " " -or $Line.StartsWith("Record #")) {
# New object reference
If ($Line.StartsWith("Record #")) {
[Object]$Script:PvsObject = New-Object PSObject
$ResultArray += $Script:PvsObject
Continue NextLine
} ElseIf ($Script:PvsObject -is [Object]) {
$ItemName = $([System.Text.RegularExpressions.Regex]::Replace($Line.Substring(0, $Line.IndexOf(":")),"[^1-9a-zA-Z_]",""))
$ItemValue = $($Line.Substring($Line.IndexOf(":") + 2))
$Script:PvsObject | Add-Member -MemberType NoteProperty -Name $ItemName -Value $ItemValue
}
}
}
Write-Host "Retrieved $($ResultArray.Count) objects"
Return $ResultArray
}
}
Examples
1.) Retrieve device name & MAC address:
& 'C:\Program Files\Citrix\Provisioning Services\MCLI.exe' get DeviceInfo | ConvertFrom-Mcli | Select deviceName, DeviceMac
2.) Retrieve all target devices with more than 0 retries:
& 'C:\Program Files\Citrix\Provisioning Services\MCLI.exe' get DeviceInfo | ConvertFrom-Mcli | Where {$_.Status -gt0}
3.) Display number of days since domain account was created:
$Devices = & 'C:\Program Files\Citrix\Provisioning Services\MCLI.exe' get DeviceInfo | ConvertFrom-MCLI
ForEach ($Devicein$Devices) { Write-Host"$($Device.deviceName): "-NoNewlineWrite-Host $([Math]::Round($($(Get-Date) - [datetime]$Device.domainTimeCreated).TotalDays)) }
If you are looking for a specific example, feel free to ask in comments.
Each returning object will have automatically all properties assigned – the only downside at the moment is the fact that each property is [String] type – but you can always override this default type if needed (see example no. 3 – I’m specifying that domainTimeCreated is [DateTime]).
You can download the function here
Martin Zugec
9 Comments