从数组循环中导出多行,因为数组不起作用

时间:2017-11-22 18:43:21

标签: arrays powershell loops csv wmi

我正在努力将多类WMI查询调整为相同的东西,但是在txt文件中滚动多个PC,然后将循环中的每一行附加到CSV

它仍然可以在一台机器上正常工作,但只要列表中有多个机器名称,CSV中的行似乎不会作为数组附加,每个单元格读取" System.Object []&#34 ;

这是我改编的代码。我觉得错误就像我的循环格式一样简单但不幸的是,我的知识有限,我不太确定如何解决它。这是我的代码:

#Start


$computers = @(Get-Content -path "C:\users\kyle.ray5\desktop\TestHosts.txt")

$disk = Get-WmiObject Win32_DiskDrive -ComputerName $computers
$bios = Get-WmiObject Win32_BIOS -ComputerName $computers
$physicalMemory = Get-WmiObject Win32_PhysicalMemory -ComputerName $computers
$processor = Get-WmiObject Win32_Processor -ComputerName $computers
$video = Get-WmiObject Win32_VideoController -ComputerName $computers
$volume = Get-WmiObject Win32_Volume -ComputerName $computers
$os = Get-WmiObject Win32_OperatingSystem -ComputerName $computers
$computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $computers

foreach($computer in $computers){
    $line = @()
        $o = [PSCustomObject]@{
        'ComputerName'      = $computerSystem.Name
        'Manufacturer'      = $bios.Manufacturer
        'Serial Number'     = $bios.SerialNumber
        'Version'           = $bios.Version
        'Operating System'  = $os.Name
        'Service Pack'      = $os.ServicePackMajorVersion
        'CPU Manufacturer'  = $processor.Manufacturer
        'Processor Arch'    = $processor.Architecture
        'Family'            = $processor.Family
        'CPU Name'          = $processor.NumberOfCores
        'Mem Capacity'      = $physicalMemory.Capacity
        'Volume Label'      = $volume.Label
        'Volume Name'       = $volume.Name
        'Total Capacity'    = $volume.Capacity
        'Available Space'   = $volume.Availability
        'Disk Part'         = $disk.Partitions
        'Disk Size'         = $disk.Size
        'Disk Availability' = $disk.Availability
        'Video Card'        = $video.Name
        'GPU Desc.'         = $video.Description
        'GPU'               = $video.VideoProcessor  
    }

    $line += $o

    $line | Export-CSV -Append -Path "C:\users\kyle.ray5\desktop\TestQuery.CSV"
}

#end

This is the output I get when I Put anymore than 1 Hostname in my list

我在每个循环中添加PSCustomObject数组$line时都尝试了这个,但仍然会收到此结果。

任何帮助都会受到赞赏,就像我说我的知识仍然有限!谢谢!

2 个答案:

答案 0 :(得分:0)

尝试以下方法:



$computers = @("Computer1", "Computer2")


$csvLines = @()

foreach($computer in $computers){

    # IF you don't need any credentials, you can omit the -Credential parameter
    $info = Invoke-Command -ComputerName $computer -Credential (Get-Credential) -ScriptBlock {

     
        $disk = Get-WmiObject Win32_DiskDrive 
        $bios = Get-WmiObject Win32_BIOS 
        $physicalMemory = Get-WmiObject Win32_PhysicalMemory 
        $processor = Get-WmiObject Win32_Processor 
        $video = Get-WmiObject Win32_VideoController 
        $volume = Get-WmiObject Win32_Volume 
        $os = Get-WmiObject Win32_OperatingSystem 
        $computerSystem = Get-WmiObject Win32_ComputerSystem 

        
        $o = [PSCustomObject]@{
            'ComputerName'      = $computerSystem.Name
            'Manufacturer'      = $bios.Manufacturer
            'Serial Number'     = $bios.SerialNumber
            'Version'           = $bios.Version
            'Operating System'  = $os.Name
            'Service Pack'      = $os.ServicePackMajorVersion
            'CPU Manufacturer'  = $processor.Manufacturer
            'Processor Arch'    = $processor.Architecture
            'Family'            = $processor.Family
            'CPU Name'          = $processor.NumberOfCores
            'Mem Capacity'      = $physicalMemory.Capacity
            'Volume Label'      = $volume.Label
            'Volume Name'       = $volume.Name
            'Total Capacity'    = $volume.Capacity
            'Available Space'   = $volume.Availability
            'Disk Part'         = $disk.Partitions
            'Disk Size'         = $disk.Size
            'Disk Availability' = $disk.Availability
            'Video Card'        = $video.Name
            'GPU Desc.'         = $video.Description
            'GPU'               = $video.VideoProcessor  
        }

        # return content of $o
        $o

    }

    $csvLines += $info

    
}

$csvLines | Export-Csv -LiteralPath C:\temp\test.csv




我使用Powershell远程处理(about remoting),因为它使用了更多标准化的"从远程机器获取信息的方法。远程处理是通过Invoke-Command -ComputerName $computer完成的。 -ScriptBlock的内容在远程计算机上执行。远程处理允许隐藏ScriptBlock的值,这可以通过调用$o - >来完成。因为它是未写入任何管道的最后一个语句,所以远程Powershell实例通过网络返回$o的内容。 确保您已在远程计算机上启用了PsRemoting - >见enable PS remoting

希望有所帮助。

答案 1 :(得分:0)

随着Vincents的帮助,我能够让这个工作。问题是我的循环格式化导致多个属性被放在一个单元格中。

全部谢谢!