0
votes

I did a search and I understand the issue that some of these are arrays. What I am not sure I understand is why I am getting this when I am specifically trying to return just one value.

I am using a ForEach loop and pulling from a text file the names of the computers I want to query. I want to verify that the IP address I am pinging is the same computer I want to connect to. We have many users on VPN and the DNS doesn't update fast enough. This means we get some incorrect information.

$NIC2 = Get-WmiObject win32_NetworkAdapterConfiguration -ComputerName $computer | Where-Object {$_.DNSDomain -eq "usms.contoso.com"} | Select-Object IPAddress 

This should give me only one IP address and not an array, but unfortunately I get the System.String[] in the CSV output. If I just type this command out in the PowerShell command line it works beautifully, but not when I put it into a CSV.

any ideas?

Thanks in advance.

Entire code below - I get the results for everything except the $NIC2 and doing $NIC2.IPAddress[0] doesn't seem to work.

Putting $NIC2[0] just give me this in that column dapterConfiguration -ComputerName $computer | Where-Object {$_.DNSDomain -eq "contoso.com"} | Select-Object IPAddress

#Definitions of variables
#************************
    #Defines the location and name of the text file from where computers will be pulled
    $Computers = Get-Content "c:\Temp\computers.txt"  

    #Defines the variable and format for Date
    $Date = Get-date -UFormat %h_%d_%y_%H%M

    #Define the name of the files to output
    $OutputFile = "C:\Temp\Verify-Computer-$Date.csv" #All Computers On-Line Status will be provided in this file

  
    #Defines the Array where to put the data and the $Ping variable to store the IP address
    $DataArray = @()
    $Resolution = New-Object System.Object.NetworkInformation.Ping

#Define A Function that will test which computers are on-line and create files to use in the other function.
ForEach ($computer in $Computers) 
{

#Defining the variables
$TESTConnection = $null
$Object = $null
$Value = $null
$IPAddress = $null
$NameResolution = $null
$PingAddress = $null
$NIC = $null
$NIC2 = $null
$NICIP = $null



# Ping computer
$TESTConnection = Test-NetConnection -ComputerName $computer | Select-Object PingSucceeded 

    #If connection is live Get IP address
    IF ($TESTConnection.PingSucceeded -eq "True") {

        $IPAddress = Test-NetConnection -ComputerName $computer -InformationLevel "Detailed" | Select-Object RemoteAddress
        $NIC = Get-WmiObject win32_NetworkAdapterConfiguration -ComputerName $computer | Where-Object {$_.DNSDomain -eq "contoso.com"} | Select-Object MACAddress, DNSHostName
        $NIC2 = {Get-WmiObject win32_NetworkAdapterConfiguration -ComputerName $computer | Where-Object {$_.DNSDomain -eq "contoso.com"} | Select-Object IPAddress} 
        $Value = $TESTConnection.PingSucceeded
        $PingAddress = $IPAddress.RemoteAddress

        
      }  
    Else {
     
        $Value          = $TESTConnection.PingSucceeded
        $PingAddress    = $IPAddress.RemoteAddress
        $NICIP         = "NOT REACHABLE"
    } 
# Create object 
$Object = New-Object PSObject -Property ([ordered]@{ 

        Computer_Target                  = $computer
        ONLINE                           = $Value
        IPAddress_PING                   = $PingAddress
        IPResolved                       = $NIC2.IPAddress
        NameResolved                     = $NIC.DNSHostName
        MACAddress                       = $NIC.MACAddress
               

}) 
  
# Add object to array
$DataArray += $Object

#Display object
}

If($DataArray)
{
#Output Array Data into a CSV File
$DataArray | Export-Csv -Path $OutputFile -NoTypeInformation
}

#end of file

1
Please include the code with which you export to CSVMathias R. Jessen
According to docs.microsoft.com/en-us/windows/win32/cimwin32prov/… IPAddress is an array. A single network adapter can have multiple IP addresses. If you are sure there is ever only one, then use $NIC2.IPAddress[0]zett42
Zett42 - Thank you for your response. I think I understand now. So regardless of how many values it is still returning an Array. That's why I get that result I get in the CSV. While in the output it doesn't matter it is an array. By putting [0] at the end that will give me the first (and only) value. Hmmm.. Ok - I'll try that and let you know.Vada
Hmm... Well that didn't work. In fact, it seemed to return no values at all for the entire spreadsheet.Vada
Edited my original comment to include the entire code.Vada

1 Answers

0
votes

OK - I finally figured it out because I kept going around in circles and getting nowhere.

Thank you to Zett42 who led me in the right direction but I was still failing.

My first problem was that I hadn't identified $NIC2 as an Array which was part of my issue when trying $NIC2[0]. However, even after being Defined as an Array it still didn't work.

I then created another array and did this $NICIP = $NIC2.IPAddress and then output $NICIP[0] which works. Not exactly sure why that works.

Here is the entire code

#Definitions of variables
#************************
    #Defines the location and name of the text file from where computers will be pulled
    $Computers = Get-Content "c:\Temp\computers.txt"  

    #Defines the variable and format for Date
    $Date = Get-date -UFormat %h_%d_%y_%H%M

    #Define the name of the files to output
    $OutputFile = "C:\Temp\Verify-Computer-$Date.csv" #All Computers On-Line Status will be provided in this file

  
    #Defines the Array where to put the data and the $Ping variable to store the IP address
    $DataArray = @()
    $Resolution = New-Object System.Object.NetworkInformation.Ping

#Define A Function that will test which computers are on-line and create files to use in the other function.
ForEach ($computer in $Computers) 
{

#Defining the variables
$TESTConnection = $null
$Object = $null
$Value = $null
$IPAddress = $null
$NameResolution = $null
$PingAddress = $null
$NIC = $null
$NIC2 = @()
$NICIP = @()



# Ping computer
$TESTConnection = Test-NetConnection -ComputerName $computer | Select-Object PingSucceeded 

    #If connection is live Get IP address
    IF ($TESTConnection.PingSucceeded -eq "True") {

        $IPAddress = Test-NetConnection -ComputerName $computer -InformationLevel "Detailed" | Select-Object RemoteAddress
        $NIC = Get-WmiObject win32_NetworkAdapterConfiguration -ComputerName $computer | Where-Object {$_.DNSDomain -eq "contoso.com"} | Select-Object MACAddress, DNSHostName
        $NIC2 = Get-WmiObject win32_NetworkAdapterConfiguration -ComputerName $computer | Where-Object {$_.DNSDomain -eq "contoso.com"} | Select-Object IPAddress
        $Value = $TESTConnection.PingSucceeded
        $PingAddress = $IPAddress.RemoteAddress

        $NICIP = $NIC2.IPAddress
      }  
    Else {
     
        $Value          = $TESTConnection.PingSucceeded
        $PingAddress    = "NOT REACHABLE"
        
    } 
# Create object 
$Object = New-Object PSObject -Property ([ordered]@{ 

        Computer_Target                  = $computer
        ONLINE                           = $Value
        IPAddress_PING                   = $PingAddress
        IPResolved1                      = $NICIP[0]
        NameResolved                     = $NIC.DNSHostName
        MACAddress                       = $NIC.MACAddress
               

}) 
  
# Add object to array
$DataArray += $Object

#Display object
}

If($DataArray)
{
#Output Array Data into a CSV File
$DataArray | Export-Csv -Path $OutputFile -NoTypeInformation
}
#end of file