Converting Output to CSV and Out-Grid

130 views Asked by At

I have a file as below. I want it to convert it to CSV and want to have the out grid view of it for items Drives,Drive Type,Total Space, Current allocation and Remaining space only.

PS C:\> echo $fileSys
Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection

1:    Name               = Extreme Performance
      Drives             = 46 x 3.8T SAS Flash 4
      Drive type         = SAS Flash
      RAID level         = 5
      Stripe length      = 13
      Total space        = 149464056594432 (135.9T)
      Current allocation = 108824270733312 (98.9T)
      Remaining space    = 40639785861120 (36.9T)

I am new to Powershell but I have tried below code for two of things but it's not even getting me desired output.

$filesys | ForEach-Object {
    if ($_ -match '^.+?(?<Total space>[0-9A-F]{4}\.[0-9A-F]{4}\.[0-9A-F]{4}).+?(?<Current allocation>\d+)$') {
        [PsCustomObject]@{
            'Total space'  = $matches['Total space']
            'Current allocation' = $matches['Current allocation']
        }
    }
}
2

There are 2 answers

6
Doug Maurer On BEST ANSWER

First and foremost, the named capture groups cannot contain spaces.

From the documentation

Named Matched Subexpressions

where name is a valid group name, and subexpression is any valid regular expression pattern. name must not contain any punctuation characters and cannot begin with a number.

Assuming this is a single string since your pattern attempts to grab info from multiple lines, you can forego the loop. However, even with that corrected, your pattern does not appear to match the data. It's not clear to me what you are trying to match or your desired output. Hopefully this will get you on the right track.

$filesys = @'
Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection

1:    Name               = Extreme Performance
      Drives             = 46 x 3.8T SAS Flash 4
      Drive type         = SAS Flash
      RAID level         = 5
      Stripe length      = 13
      Total space        = 149464056594432 (135.9T)
      Current allocation = 108824270733312 (98.9T)
      Remaining space    = 40639785861120 (36.9T)
'@

if($filesys -match '(?s).+total space\s+=\s(?<totalspace>.+?)(?=\r?\n).+allocation\s+=\s(?<currentallocation>.+?)(?=\r?\n)')
{
    [PsCustomObject]@{
        'Total space'  = $matches['totalspace']
        'Current allocation' = $matches['currentallocation']
    }
}

Total space              Current allocation     
-----------              ------------------     
149464056594432 (135.9T) 108824270733312 (98.9T)

Edit

If you just want the values in the parenthesis, modifying to this will achieve it.

if($filesys -match '(?s).+total space.+\((?<totalspace>.+?)(?=\)).+allocation.+\((?<currentallocation>.+?)(?=\))')
{
    [PsCustomObject]@{
        'Total space'  = $matches['totalspace']
        'Current allocation' = $matches['currentallocation']
    }
}

Total space Current allocation
----------- ------------------
135.9T      36.9T  
0
GURU SINGH On
$unity=[Regex]::Matches($filesys, "\(([^)]*)\)") -replace '[(\)]',''  -replace "T",""

$UnityCapacity = [pscustomobject][ordered] @{

Name = "$Display"

"Total" =$unity[0]

"Used" = $unity[1]

"Free" = $unity[2]

'Used %' = [math]::Round(($unity[1] / $unity[0])*100,2)
}``