我正在使用PowerShell进行一些测试,以列出我域中每台计算机的硬件信息。我的脚本将从我使用PowerShell创建的文本文件中获取计算机列表。
$arrComputers = get-Content -Path "C:\powershell\List.txt"
foreach ($strComputer in $arrComputers)
{
$colItemsOs = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsOs)
{
Write-host "Computer Name: " $strComputer
Write-host "OsName: " $objItem.Name
Write-host "Version: " $objItem.Version
}
$colItemsCompsys = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsCompsys)
{
Write-host "Memory: " $objItem.TotalPhysicalMemory
Write-host "Manufacturer: " $objItem.Manufacturer
Write-host "Model: " $objItem.Model
Write-host "Domain: " $objItem.Domain
}
$colItemsSysEnc = get-wmiobject -class "Win32_SystemEnclosure" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsSysEnc)
{
Write-host "SerialNumber: " $objItem.SerialNumber
}
$colItemsProcessor = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsProcessor)
{
Write-host "Processor: " $objItem.Name
}
$colItemsIP = get-wmiobject -class "Win32_NetworkAdapterConfiguration " -namespace "root\CIMV2" `
-computername $strComputer -Filter "IpEnabled = TRUE"
foreach ($objItem in $colItemsIP)
{
Write-host "IPAddress: " $objItem.IpAddress
}
Write-host | Out-File c:\powershell\reportbeta.csv
}
但是,我无法这样做。我如何在csv文件中生成结果,以后可以使用sqlcmd将其上传到我的数据库。
我错过了什么?
谢谢
我设法在文本文件中显示输出。但是,只显示1条记录。
$arrComputers = get-Content -Path "C:\powershell\List.txt"
foreach ($strComputer in $arrComputers)
{
$colItemsOs = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsOs)
{
$strComputer
$Name =$objItem.Name
$Version = $objItem.Version
}
$colItemsCompsys = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsCompsys)
{
$TotalMemory = $objItem.TotalPhysicalMemory
$Manufacturer = $objItem.Manufacturer
$Model = $objItem.Model
$Domain = $objItem.Domain
}
$colItemsSysEnc = get-wmiobject -class "Win32_SystemEnclosure" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsSysEnc)
{
$SerialNUmber = $objItem.SerialNumber
}
$colItemsProcessor = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsProcessor)
{
$Processor = $objItem.Name
}
$colItemsIP = get-wmiobject -class "Win32_NetworkAdapterConfiguration " -namespace "root\CIMV2" `
-computername $strComputer -Filter "IpEnabled = TRUE"
foreach ($objItem in $colItemsIP)
{
$IPV4 = $objItem.IpAddress
}
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $strComputer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Name
$OutputObj | Add-Member -MemberType NoteProperty -Name Version -Value $Version
$OutputObj | Add-Member -MemberType NoteProperty -Name TotalPhysicalMemory -Value $TotalMemory
$OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Manufacturer
$OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Model
$OutputObj | Add-Member -MemberType NoteProperty -Name Processor -Value $ProcessorSpeed
$OutputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $SerialNo
$OutputObj | Add-Member -MemberType NoteProperty -Name Domain -Value $Domain
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPV4
$OutputObj | Export-CSV -NoTypeInformation "c:\powershell\report.txt"
}
我现在没想法了。我应该把它变成数组吗?
请告知。
谢谢
答案 0 :(得分:1)
问题是您在循环中导出到csv,因此最后一项将是唯一显示的项目。尝试这样的事情。
循环之前:
# Create empty array
$report = @()
在循环结束时,代替您当前导出为CSV的位置:
#Add item to report
$report += $OutputObj
最后,在循环结束后:
$report | Export-CSV -NoTypeInformation "c:\powershell\report.txt"
答案 1 :(得分:0)
我建议您将foreach结果分配给变量,然后将其传递给Export-Csv
。它应该执行得更快,并且比在每次循环迭代中重新创建和更新数组更有效。
$result = foreach($strComputer in $arrComputers)...
$result | Export-CSV ...
如果您使用了Foreach-Object' cmdlet to loop over computers you could skip the whole assignment or array creation, processing one object at a time (streamlined) and pipe directly to
导出CSV文件:
Get-Content -Path C:\powershell\List.txt | Foreach-Object{
...
} | Export-Csv ...