4

i have output from external command, like as:

Status  Pool name                 Media Type        MS   # of media   Free [MB]
===============================================================================
Good    pool1           LTO-Ultrium       No         303   298066893
Good    pool2           LTO-Ultrium       No           1     1525878
Good    pool3           LTO-Ultrium       No         282   348735361
Good    pool4           LTO-Ultrium       No         473   588150645

i need parse in PowerShell "pool1", "pool2", "pool3" ... and run command in foreach... any idea for this, without exporting output to xml, csv & etc ? thanks

and, Q2, is here same, or similar way for parsing these:

Good   [QGHL5MMA] QGHL5MMA              [pool1:   322]            No   None              
Good   [QGHL5N2Y] QGHL5N2Y              [pool1:   922]            No   None              
Good   [QGHL5MUL] QGHL5MUL              [pool1:   621]            No   None              

From this output, i need only text from column 5 ( 322, 922, 621... ). thanks again

0

3 Answers 3

4

I put the content into a file test.txt and did a try as below:

gc .\test.txt | select-string '.*pool\d+.*' | foreach { $pool = ($_  -split '\s+')[1];write-host $pool;}

And the output:

pool1
pool2
pool3
pool4

In the command line I search the lines which has the pattern as 'pool' followed by digits, and then split it using whitespace as delimiter, then you can get the name 'pool1' etc. to do the things you want.

Sign up to request clarification or add additional context in comments.

Comments

1

If you create objects, you can do all kinds of Powershell things with them when you're done. Powershell is all about the objects.

Using your sample data and Powershell V3

$CommandData = 
(@'
Status  Pool name                 Media Type        MS   # of media   Free [MB]
===============================================================================
Good    pool1           LTO-Ultrium       No         303   298066893
Good    pool2           LTO-Ultrium       No           1     1525878
Good    pool3           LTO-Ultrium       No         282   348735361
Good    pool4           LTO-Ultrium       No         473   588150645
'@).split("`n") |
foreach {$_.trim()}

$CommandData | 
 foreach {
  $Props = &{$args} Status Pool Media Type MS No Free[MB]
  if ($_ -match '\d+\s*$')
   {$Parts = $_ -split '\s+'
    $Hash = [ordered]@{}
    for ($i=0; $i -le 5; $i++)
     {$Hash[$Props[$i]] = $Parts[$i]}
    [PSCustomObject]$Hash
    }
   } | | Format-Table -AutoSize


Status Pool  MediaType   MS No  Free[MB] 
------ ----  ---------   -- --  -------- 
Good   pool1 LTO-Ultrium No 303 298066893
Good   pool2 LTO-Ultrium No 1   1525878  
Good   pool3 LTO-Ultrium No 282 348735361
Good   pool4 LTO-Ultrium No 473 588150645

1 Comment

i need study your example - i am new in arrays,hash & etc. But i think, i dont have another way for another part of script...
0

A simple regex will capture the result.

gc .\table.txt | sls '\s+(pool\d+)\s+' | % {$_.Matches.Groups[1].Value}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.