如何用PowerShell优化MegaCLI的LDInfo输出并提取指定字段?
Filtering MegaCLI LDInfo Output to Specific Fields with PowerShell
No problem—let's cut through that noisy MegaCLI output and get just the data you need. Below is a PowerShell script that parses the raw output from your MegaCliKL command and extracts only the Name, RAID Level, Size, Number Of Drives, State, and Span Depth fields, organized by each virtual drive:
# Capture the raw output from MegaCLI $megaCliResults = & "C:\Users\Administrator\Downloads\8-04-07_MegaCLI\Win_CliKL_8.04.07\MegaCliKL.exe" -LDInfo -Lall -aAll # Define the exact fields we want to retain $desiredFields = @("Name", "RAID Level", "Size", "Number Of Drives", "State", "Span Depth") # Process output to extract and structure the data $currentDriveData = @{} $megaCliResults | ForEach-Object { $cleanLine = $_.Trim() # Detect the start of a new virtual drive's info if ($cleanLine -match "^Adapter\s+\d+:\s+Virtual Drive\s+(\d+)\s+Info") { # If we have data from a previous drive, output it first if ($currentDriveData.Count -gt 0) { [PSCustomObject]$currentDriveData $currentDriveData = @{} } # Track which virtual drive we're processing $currentDriveData["Virtual Drive ID"] = $matches[1] } # Check if the line matches any of our target fields foreach ($field in $desiredFields) { if ($cleanLine -match "^$field\s*:\s*(.*)$") { $currentDriveData[$field] = $matches[1].Trim() break } } } # Output the last virtual drive's data (since the loop won't catch it) if ($currentDriveData.Count -gt 0) { [PSCustomObject]$currentDriveData }
How this works:
- Capture Output: Runs your MegaCLI command and stores the raw text output in a variable.
- Target Fields: Lists exactly the fields you want to keep, so we only process relevant lines.
- Parse Line-by-Line: Scans each line to detect when a new virtual drive's info starts, then extracts values for your target fields and stores them in a hash table.
- Structured Output: Converts the hash table into a PowerShell custom object for clean, readable output—you can even pipe this to
Export-Csvif you want a spreadsheet-friendly version:# Add this to the end of the script to export to CSV | Export-Csv -Path "Clean_LD_Info.csv" -NoTypeInformation
内容的提问来源于stack exchange,提问作者ZackUnity
相关产品推荐
相关产品推荐

