0

我正在使用 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"

}

我现在没主意了。我应该把它做成数组吗?

请指教。

谢谢

4

2 回答 2

1

问题是您要在循环中导出到 csv,因此最后一项将是唯一显示的项。尝试这样的事情。

循环前:

# Create empty array
$report = @()

在循环结束时,代替您当前导出到 CSV 的位置:

#Add item to report
$report += $OutputObj

最后,在循环结束后:

$report | Export-CSV -NoTypeInformation "c:\powershell\report.txt"
于 2012-12-01T19:30:42.123 回答
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 toExport-CSV`:

Get-Content -Path C:\powershell\List.txt | Foreach-Object{
    ...
} | Export-Csv ...
于 2012-12-02T06:50:40.297 回答