我阅读了包含很多信息的文本文件。我只需要提取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
$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