将数组导出到 CSV 文件时出现问题

Issue in export Array to CSV file

我在文本文件中有机器列表,我正在尝试获取物理驱动器、OS 体系结构和物理内存的详细信息。在 Matt(SO 用户)的帮助下,这里是 powershell 脚本。

$server = Get-Content .\Server.txt
#$infoObject11 = @{}
$infoObject11 = @{}
foreach ($server in $servers) {
    # Gather all wmi drives query at once
    $alldisksInfo = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" -ComputerName $server -ErrorAction SilentlyContinue | Group-Object __Server

    # Figure out the maximum number of disks
    $MaximumDrives = $alldisksInfo | Measure-Object -Property Count -Maximum | Select-Object -ExpandProperty Maximum

    # Build the objects, making empty properties for the drives that dont exist for each server where need be. 
    $server | ForEach-Object {
        # Clean the hashtable
        $infoObject1 = @{}
        # Populate Server
        $infoObject1.Server = $server 
        $HOSTNAME = Get-WMIObject -Query "Select * from Win32_OperatingSystem" -ComputerName $infoObject1.Server
        # Add other simple properties here
        $infoObject1.PhysicalMemory = (Get-WmiObject Win32_PhysicalMemory -ComputerName $infoObject1.Server | Measure-Object Capacity -Sum).Sum/1gb
        $infoObject1.OSarchitecture =$HOSTNAME.osarchitecture

        # Add the disks information from the $diskInfo Array
        $serverDisksWMI = $alldisksInfo | Where-Object{$_.Name -eq $infoObject1.Server} | Select-Object -ExpandProperty Group

        for ($diskIndex =0; $diskIndex -lt $MaximumDrives;$diskIndex++) {
            $infoObject1."PhysicalDisk$diskIndex" = [Math]::Round(($serverDisksWMI | Where-Object{($_.DeviceID -replace "^\D*") -eq $diskIndex} | Select -Expand Size)/1GB)
        }


    }
    # Create the custom object now.
    New-Object -TypeName psobject -Property $infoObject1  | Export-Csv -path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation 
}

问题出在 CSV 文件中我得到的是单台机器的详细信息,但在 server.txt 文件中有不止一台机器。如果我在 New-Object 之前打印 $infoObject1 那么我可以看到多台机器的详细信息。数组似乎有问题,我无法将其导出为 CSV。

任何人都可以对此提出建议。

foreach ($server in $servers) {
   ...
   New-Object -TypeName PSObject -Property $infoObject1 |
     Export-Csv -Path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation 
}

您正在使用参数 -Append(在 PowerShell v3 和更新版本中可用)在 inside 循环 without 中导出。每次迭代都会覆盖您的输出文件,只留下最后一个服务器的数据。

使用参数 -Append(如果您有 PowerShell v3 或更新版本):

foreach ($server in $servers) {
   ...
   New-Object -TypeName PSObject -Property $infoObject1 |
     Export-Csv -Append -Path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation
}

或将 Export-Csv 移到循环外(适用于所有 PowerShell 版本):

(foreach ($server in $servers) {
   ...
   New-Object -TypeName PSObject -Property $infoObject1
}) | Export-Csv -Path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation

请注意,您需要 运行 括号中的循环才能正常工作,因为 foreach 循环不会输出到管道。

如果您想直接向管道输送,您也可以将 foreach 循环替换为 ForEach-Object

Get-Content .\Server.txt | ForEach-Object {
   $server = $_
   ...
   New-Object -TypeName PSObject -Property $infoObject1
} | Export-Csv -Path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation

看来您在集成我的代码时遇到了问题。您添加了第二个不应该存在的循环。同样正如其他用户指出的那样,您没有在 外部 循环中创建每个服务器对象。 。我什至告诉过你把 Export-CSV.

放在哪里
$servers = Get-Content .\Server.txt

# Gather all wmi drives query at once
$alldisksInfo = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" -ComputerName $servers -ErrorAction SilentlyContinue | Group-Object __Server

# Figure out the maximum number of disks
$MaximumDrives = $alldisksInfo | Measure-Object -Property Count -Maximum | Select-Object -ExpandProperty Maximum

# Build the objects, making empty properties for the drives that dont exist for each server where need be. 
$servers | ForEach-Object {
    # Clean the hashtable
    $infoObject1 = @{}
    # Populate Server
    $infoObject1.Server = $_ 
    # Add other simple properties here
    $infoObject1.PhysicalMemory = (Get-WmiObject Win32_PhysicalMemory -ComputerName $infoObject1.Server | Measure-Object Capacity -Sum | Select-Object -ExpandProperty Sum)/1GB
    $infoObject1.OSarchitecture = Get-WMIObject -Query "Select * from Win32_OperatingSystem" -ComputerName $infoObject1.Server | Select-Object -ExpandProperty OSArchitecture

    # Add the disks information from the $diskInfo Array
    $serverDisksWMI = $alldisksInfo | Where-Object{$_.Name -eq $infoObject1.Server} | Select-Object -ExpandProperty Group

    for ($diskIndex =0; $diskIndex -lt $MaximumDrives;$diskIndex++) {
        $infoObject1."PhysicalDisk$diskIndex" = [Math]::Round(($serverDisksWMI | Where-Object{($_.DeviceID -replace "^\D*") -eq $diskIndex} | Select-Object -ExpandProperty Size)/1GB)
    }

    # Create the custom object now for this pass in the loop.
    New-Object -TypeName psobject -Property $infoObject1  
} | Export-Csv -path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation