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

问题描述 投票:0回答:1

我阅读了包含很多信息的文本文件。我只需要提取2个值:1- ComputerName和ProductName。我写了下面的脚本,最终结果是2个标题,其余为空白。

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
powershell file text extract
1个回答
0
投票
  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

© www.soinside.com 2019 - 2024. All rights reserved.