Cheries
Cheries

Reputation: 892

How to get content a file with system.array type data and sort it using PowerShell?

I want to get the contents of a file.

The format of file is like this

Disk0MapForUefiBootOrder
    PciRoot(0x0)/Pci(0x14,0x0)

    PciRoot(0x0)/Pci(0x1B,0x0)/Pci(0x0,0x0)/NVMe(0x1,7D-F0-B6-71-B7-38-25-00)

    PciRoot(0x0)/Pci(0x1B,0x4)/Pci(0x0,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)

    PciRoot(0x0)/Pci(0x17,0x0)/Sata(0x1,0xFFFF,0x0)

    BBS(0xFFFF,,0x0)/PciRoot(0x0)/Pci(0x14,0x0)

Fast Charge
    Disable
    *Enable 

......

I want to read the value of this Disk0MapForUefiBootOrder which contains NVMe and Sata, which are:

PciRoot(0x0)/Pci(0x1B,0x0)/Pci(0x0,0x0)/NVMe(0x1,7D-F0-B6-71-B7-38-25-00)

PciRoot(0x0)/Pci(0x1B,0x4)/Pci(0x0,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)

PciRoot(0x0)/Pci(0x17,0x0)/Sata(0x1,0xFFFF,0x0)

and pass it to the output file. Then I have to sort it with a rank.

I tried this, but I can only read the content. But I can not rank it.

Can anyone help me please?

$FileContents = Get-Content "D:\Boot\file.txt" | Select-String -Pattern "PciRoot" | Out-File D:\Boot\Out2 -Force
    $FileContents
    $Rank = @{
        'NVMe' = 1
        'Sata' = 2
    }
    $FileContents |
        Where-Object { $Rank.Contains("NVMe" -and "Sata") } | Sort-Object {[int64]$_.Size} |
        Sort-Object { $Rank["NVMe" -and "Sata"] }|
        Export-Csv 'Output.csv' -NoType

Upvotes: 0

Views: 102

Answers (2)

AdminOfThings
AdminOfThings

Reputation: 25001

You can do the following if we assume that the data you want is indented:

$found = $false
switch -regex -file file.txt {
    'Disk0MapForUefiBootOrder' { $found = $true; continue }
    'nvme|sata' { if ($found) { $_.Trim() }}
    '^\S' { if ($found) { return }}
}

Upvotes: 1

Dany
Dany

Reputation: 126

You could use Select-String with -Pattern.

$FileContents= Get-Content $FilePath | Select-String -Pattern "NVMe|Sata"

Upvotes: 0

Related Questions