Powershell 读取文本文件并提取 2 个值 ComputerName 和 ProductName

Powershell Read a text file and extract 2 values ComputerName & ProductName

我阅读了包含大量信息的文本文件。 我只需要提取 2 个值:1- ComputerName 和 ProductName。 我已经写了下面的脚本,最终结果是 2 headers 剩下的是空白。

SystemName=ABC123 (This is the ComputerName)
ProductName=USB Audio Device



$FileName0 = "C:\DATA\AUDIT\DRAGON\DRAGON.TXT"
foreach ($computer in (Get-Content C:\DATA\AUDIT\DRAGON\COMPUTERS11.TXT)) {
wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value  | Out-File "C:\DATA\AUDIT\DRAGON\DRAGON.TXT"}
Get-content $FileName0 | Select-Object $computer, ProductName | Out-File C:\DATA\AUDIT\DRAGON\DRAGON-USB.TXT
  1. 读入您的计算机列表
  2. 运行 每台计算机上的脚本使用 Get-WmiObject
  3. 编译你想要的数据。
$hash = @{}

foreach($server in Get-Content computers.txt) {
    $result = (Get-WmiObject CIM_LogicalDevice -Filter "Description like 'USB%'" -ComputerName $server | select Name).Name
    $hash[$server] = $result
}

# At this point, your hashtable has a list of all servers with all the products that have description starting with USB.

#Convert to Json and save it to file.
$hash | ConvertTo-Json | Out-File test.txt

# And to convert it to csv, use the following with GetEnumerator.
$hash.GetEnumerator() | Select-Object -Property @{N='Property';E={$_.Key}}, @{N='PropValue';E={$_.Value -Join "|"}}  | Export-Csv test.csv