1

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
4
  • Where's the rest of your script? You want it to say "Subnet: c:\t2\AD Computer Scripts" ? Commented Aug 16, 2016 at 19:02
  • 1
    Remove -PassThru from the Add-Member statement Commented Aug 16, 2016 at 19:11
  • Mathias as always thank you for your help. That solved it. Commented Aug 16, 2016 at 19:16
  • @JRN Added a proper answer Commented Aug 16, 2016 at 20:58

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.