0

How to handle array with split and substring and foreach to output below information.

I have tried with split and substring. But still not work. Thanks.

$item=$item.ToString() 
$driveID=$item.Substring(0,26) 
$ID=($driveID.Split(":")[-1]).trim()
$status=$item.Substring(27,$item.Length-1-27)
$stauts1=($status.split(",")[-1]).trim()

input【array】:

physicaldrive 1I:1:9 (port 1I:box 1:bay 9, SAS HDD, 900 GB, OK)
physicaldrive 1I:1:10 (port 1I:box 1:bay 10, SAS HDD, 900 GB, OK)
physicaldrive 1I:1:11 (port 1I:box 1:bay 11, SAS HDD, 2.4 TB, OK, spare)
physicaldrive 1I:1:12 (port 1I:box 1:bay 12, SAS HDD, 900 GB, OK, shared spare)

Expected output:

DriveID DriveStatus
9  OK
10 OK
11 OK
12 OK
3
  • [1] is the drive id the same as the bay number? [2] PLEASE format your code & data properly. there is link to the instructions on the page that you used to create your question. Commented Dec 29, 2019 at 16:45
  • @Lee_Dailey, thank you very much. I've learned a lot from you Commented Dec 30, 2019 at 3:36
  • you are very welcome! glad to have helped a little ... [grin] Commented Dec 30, 2019 at 7:06

1 Answer 1

1

the following presumes the DriveID is the number after the word bay. the code grabs named capture groups and uses them to make a PSCustomObject. that is then sent to the $DriveInfo collection, and finally displayed on screen. at that point you can easily export it. [grin]

#region >>> fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
physicaldrive 1I:1:9 (port 1I:box 1:bay 9, SAS HDD, 900 GB, OK)
physicaldrive 1I:1:10 (port 1I:box 1:bay 10, SAS HDD, 900 GB, OK)
physicaldrive 1I:1:11 (port 1I:box 1:bay 11, SAS HDD, 2.4 TB, OK, spare)
physicaldrive 1I:1:12 (port 1I:box 1:bay 12, SAS HDD, 900 GB, OK, shared spare)
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a text file


$DriveInfo = $InStuff.ForEach({
    $Null = $_ -match '^.+:bay (?<DriveID>\d+), .+b, (?<Status>.+).+$'
    [PSCustomObject]@{
        DriveID = $Matches.DriveID
        Status = $Matches.Status.Split(',')[0]
        }
    })

$DriveInfo

output ...

DriveID Status
------- ------
9       OK    
10      OK    
11      OK    
12      OK    
Sign up to request clarification or add additional context in comments.

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.