Print volume name from each array value

1

I've an array with values as follow - $VolErrChk[0] has :

 VOLUME #2
I:
\Device\HarddiskVolume8
\\?\Volume{3559b156-d159-11e4-80c1-0050569cf1fd}
5117 MB.
0x000000304f65e304.
\\.\Volume{3559b156-d159-11e4-80c1-0050569cf1fd}
>      **** WARNING: DATA NOT AVAILABLE [0x00000100] ****

$VolErrChk[1] has :

     VOLUME #4
B:
\Device\HarddiskVolume20
\\?\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
81917 MB.
0x000000304f65e304.
UNKNOWN.
>      **** WARNING: DATA NOT AVAILABLE [0x00000100] ****

I'm doing as follow -

 foreach ($disk in $VolErrChk)
    {
    $disk = ($VolErrChk-split "`n" | where {$_ -like "*?\Volume{*"})
    "type=ERROR;disk=$disk;"
    }

Can I get this output from 2 array values -

type=ERROR;disk=\\?\Volume{3559b156-d159-11e4-80c1-0050569cf1fd}
type=ERROR;disk=\\?\Volume{3bf4ee0a-d050-11e4-80be-0050569cf1fd}
powershell
asked on Stack Overflow Oct 12, 2016 by jes • edited Oct 12, 2016 by Martin Brandl

2 Answers

2

You were close you just needed to split $disk and not $VolErrChk, and then pipe the output it into a foreach cmdlet (%) to output a volume on each line.

foreach ($disk in $VolErrChk){
    $disk -split "`n" | where {$_ -match "\\\\\?\\Volume{*" } | %{ "type=ERROR;disk=$_;" } 
}
answered on Stack Overflow Oct 12, 2016 by Richard • edited Oct 12, 2016 by Richard
1

You don't have to split the string, you can just use a to capture the volume:

foreach ($disk in $VolErrChk)
{
    $disk = [regex]::Match($disk, '([\\]{2}\?\\Volume{[^}]+})').Groups[0].Value
    "type=ERROR;disk=$disk;"
}
answered on Stack Overflow Oct 12, 2016 by Martin Brandl

User contributions licensed under CC BY-SA 3.0