JRN
JRN

Reputation: 279

Issue with outputting multiple values

I am writing a script to gather data about domain computers and I ma trying to output this to a csv file. However its not coming out the way I want it to.

Here is my code:

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"

The out put looks like this:

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

All I need is a single line. It should look like this:

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

Upvotes: 1

Views: 35

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174865

The -PassThru parameter to Add-Member means "output the modified object".

Thus, these two statements are (apart from the side-effect of adding a NoteProperty to $Report1) redundant:

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

Either remove the $Report1 statement at the end or remove -PassThru switch from the Add-Member statement

Upvotes: 2

Related Questions