0

I have a script to check active directory for windows 7 computers, ping them and report back the ip address.

The export-csv function writes the correct fields and data, except the IP address. The IP address shows as "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"

Copy of Script

Function Get-Win7 {
        $Win7list = get-adcomputer -filter {(operatingsystem -like "Windows 7 Professional") -and (enabled -eq "True")} -properties operatingsystem, lastlogondate 
        foreach ($pc in $Win7list){
            $pingtest = test-connection $pc.name -erroraction silentlycontinue -errorvariable pingfail
            if (get-variable -name pingfail -erroraction silentlycontinue) {
                if ($pingfail.exception -match "failed"){
                 $IP3 = "No IP"
                 $pingfall = $null
                }       
            }
            $pc.IP = New-Object -TypeName PSCustomObject -Property @{IPAddress = $pingtest.IPV4Address.ToString | select-object -first 1}
            $PC |select-object name,operatingsystem, lastlogondate, IP |sort-object lastlogondate | export-csv -path c:\users\{user}\desktop\Win7-GSN.csv -notypeinfo -append
        }
}

Any help is appreciated.

1
  • Still learning and figuring this PS thing out :) I read that new-object would allow me to pass the string into export-csv. Can you be so kind to type the syntax that would work? Commented Feb 6, 2020 at 0:55

1 Answer 1

1

You can use a calculated property in Select-Object, which allows you to add a custom property and give it a custom value. Here we can add a custom property IP and give it the IPV4Address.

$PC | Select-Object name,operatingsystem, lastlogondate, @{n='IP';e={$pingtest[0].IPV4Address}}

As a result, you can remove the $pc.IP = ... line entirely.

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

1 Comment

Thank you very much. Appreciate the syntax and explanation equally :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.