ConvertFrom-MCLI for PVS 7.6 POSH module

06 Apr 2016

Martin Zugec made a POSH script to convert output from mcli.exe to a PowerShell object, but his script fails with the new PVS 7.6 POSH module. From looking at his script he is trying to key on ‘spaces’ to set the properties of the objects. MCLI.exe outputs the properties with spaces. PVS 7.6 POSH module fails because it doesn’t format it’s output as spaces.

MCLI.EXE output:

Executing: Get DISKVERSION
Get succeeded. 2 record(s) returned.
Record #1
    access: 0
 
Record #2
    access: 

POSH module output:

Executing: Get diskversion
Get succeeded. 2 record(s) returned.
Record #1
access: 0
 
Record #2
access: 

Notice the subtle difference? No spaces.

To get Martin’s original script working we need to change the script to look for another character we can key on. Fortunately(?) it *appears* that the PVS POSH module outputs properties to start with a lower case letter. If we modify his script here:

If ($Line[0] -eq " " -or $Line.StartsWith("Record #")) 

To this:

If ($Line[0] -cmatch  "[a-z]" -or $Line.StartsWith("Record #")) 

It now outputs properties correctly from the PVS POSH module:

PS C:\swinst\VMTools_and_TargetDeviceSoftwareUpdate> mcli-get diskversion -p diskLocatorName=XenApp65tn02,siteName=BDC,storeName=& -f access | ConvertFrom-MCLI
Retrieved 2 objects
 
access                                                                                                                     
------                                                                                                                     
0                                                                                                                          

The full modified script is here:

Function ConvertFrom-MCLI {
 
 Begin {
  [array]$PvsLines = @()
 }
  
 Process {
  $PvsLines += $_
 }
  
 End {
  [array]$ResultArray = @()
  :ParseLines ForEach ($Line in $PvsLines) {
   If ($Line.Length -eq 0) {
                Continue
                }
   If ($Line[0] -cmatch  "[a-z]" -or $Line.StartsWith("Record #")) {
    # New object reference
    If ($Line.StartsWith("Record #")) {
     [Object]$Script:PvsObject = New-Object PSObject
     $ResultArray += $Script:PvsObject
                    Continue ParseLines
    } ElseIf ($Script:PvsObject -is [Object]) {
     $Script:PvsObject | Add-Member -MemberType NoteProperty -Name  $([System.Text.RegularExpressions.Regex]::Replace($Line.Substring(0, $Line.IndexOf(":")),"[^1-9a-zA-Z_]","")) -Value $($Line.Substring($Line.IndexOf(":") + 2))
    }
   }
  }
  Write-Host "Retrieved $($ResultArray.Count) objects"
  Return $ResultArray
 }