Powershell读取文本文件并提取2个值ComputerName&;产品名称
我读了包含很多信息的文本文件。 我只需要提取2个值:1-ComputerName&ProductName。 我已经写了下面的脚本,最终结果是2个标题,其余的是空白Powershell读取文本文件并提取2个值ComputerName&;产品名称,powershell,file,text,extract,Powershell,File,Text,Extract,我读了包含很多信息的文本文件。 我只需要提取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\A
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
谢谢如何将此脚本的结果导出到文件(例如toto.txt)中?我试过了,但结果是循环。谢谢您的帮助。@user270488请参阅更新的帖子,其中包括如何导出它(json/csv)。您好,结果如下图所示,但在“|”之间有数百个值。“SERVER1”“USB音频设备| USB音频设备| A | B | C |……| PowerMicII NS |”如果服务器没有值“PowerMicII NS”,我需要得到如下结果:“SERVER1”“PowerMicII NS”,对不起,我不需要结果文件中的服务器,但我找不到获得最终结果的方法,因为值太多且变化很大。谢谢您的帮助。@user270488您的原始帖子要求提供所有usb设备,我唯一能想到的生成数组csv的方法是使用不同的分隔符。如果要查找特定设备,请更改查找描述的过滤器
-Filter“Description LIKE'PowerMicII'”
我尝试了这个-Filter“Description LIKE'PowerMicII'”,但是整个文件的“PropValue”现在为空。谢谢你的帮助。
$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