输出多个值的问题

Issue with outputting multiple values

我正在编写一个脚本来收集有关域计算机的数据,并且我试图将其输出到一个 csv 文件。然而它并没有按照我想要的方式出现。

这是我的代码:

Set-ExecutionPolicy Unrestricted -force
Import-Module ActiveDirectory

$CSVPath = $ScriptFolderPath + "\" + $ScriptName + ".csv" 
$Import = Get-Content "c:\T2\AD Computers Scripts\ComputersTest.csv"

foreach ($Member in $Import)
{
    $ComputerName = Get-ADComputer $Member -Properties Name, CanonicalName | Select Name, CanonicalName

    $ConnectionStatus = Test-Connection $Member -Quiet
    IF ($ConnectionStatus -eq $TRUE)
    {
      $IPAddress = Test-Connection $Member -Count 1  | Select -ExpandProperty IPV4Address
    }
    ELSE
    {
      $IPAddress = "Not able to contact server"
    }

    $CSVPath

    $Report1 = $ComputerName | ForEach -Process {$_ | Add-Member -Name IPAddress -Value $IPAddress -MemberType NoteProperty -PassThru}
    $Report1 | Add-Member -Name Subnet -Value $CSVPath -MemberType NoteProperty -PassThru
    $Report1
    "`n`n`n"

输出看起来像这样:

Name                          CanonicalName                 IPAddress                     Subnet
----                          -------------                 ---------                     ------
CEN-RVS                       abc.local/Servers/Corpora... 10.19.95.2                    C:\t2\AD Computers Scripts
CEN-RVS                       abc.local/Servers/Corpora... 10.19.95.2                    C:\t2\AD Computers Scripts

我只需要一行。它应该是这样的:

Name                          CanonicalName                 IPAddress                     Subnet
----                          -------------                 ---------                     ------
CEN-RVS                       abc.local/Servers/Corpora... 10.19.95.2                    C:\t2\AD Computers Scripts

Add-Member-PassThru 参数表示 "output the modified object"。

因此,这两个语句(除了将 NoteProperty 添加到 $Report1 的副作用之外)是多余的:

$Report1 | Add-Member -Name Subnet -Value $CSVPath -MemberType NoteProperty -PassThru
$Report1

要么删除末尾的 $Report1 语句,要么从 Add-Member 语句中删除 -PassThru 开关