如何获取内容与system.array类型数据的文件并使用PowerShell对其进行排序?

我想获取文件的内容。

文件格式如下

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,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 

......

我要读取包含NVMe和Sata的Disk0MapForUefiBootOrder的值,

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

PciRoot(0x0)/Pci(0x1B,00-00-00-00-00-00-00-00)

PciRoot(0x0)/Pci(0x17,0x0)

,并将其传递到输出文件。然后我必须按等级对其进行排序。

我尝试过,但是我只能阅读内容。但是我无法排名。

有人可以帮我吗?

$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
adsaad 回答:如何获取内容与system.array类型数据的文件并使用PowerShell对其进行排序?

如果我们假设所需的数据已缩进,则可以执行以下操作:

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

您可以将Select-String与-Pattern一起使用。

$FileContents= Get-Content $FilePath | Select-String -Pattern "NVMe|Sata"
本文链接:https://www.f2er.com/3168024.html

大家都在问