• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 947
  • Last Modified:

I do not see the correct IP addresses after run the Export-csv powershel cmdlet

Hi folks,
I have a script for inventory data including ip address that do export to csv  file . ip address in the column instead of addresses have a record System.String []. I bring this script completely:

$Machines = Get-Content -Path C:\powershell\scripts\Computers.txt
$AllComputers = @()

foreach ($machine in $machines)
    $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
    $compname = $objCompSys.Name
    $physicalRAM = [Math]::Ceiling($objCompSys.TotalPhysicalMemory / 1GB)
    $CurrentUser = $objCompSys.username  
    $objCompOS = Get-WmIObject -class "Win32_OperatingSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
    $WindowsVersion = $objCompOS.Caption
    $ServicePack =  $objCompOS.csdversion
    $WinInsDate = $objCompOS.ConvertToDateTime($objCompOS.InstallDate) | Get-Date -Format d                        
    $DiskItems = Get-WMIObject -Class Win32_DiskDrive -Filter {DeviceId = '\\\\.\\PHYSICALDRIVE0'} -computername $machine | select -ExpandProperty size
    $DiskSize = $DiskItems.Size/1GB
    $PhyDrive = $objPhydrive.size
    $NetAdapters = Get-WmIObject -class "Win32_NetworkAdapterConfiguration" -cn $machine  | where{$_.IPEnabled -eq "True"}
    $IPAddress =   $NetAdapters.IPAddress

    $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine
    $procType = $objProc.Name
    $Object = New-Object PSObject -Property @{
    CompName     = $compname
    WindowsVersion = $WindowsVersion
    ServicePack = $ServicePack
    WinInsDate = $WinInsDate
    IPAddress = $IPAddress
    CurrentUser = $CurrentUser
    ProcType = $procType
    DiskSize = $DiskSize
    MemoryCapacity = $physicalRAM
    #DiskSize = $Item.Size/1024/1024/1024
    # FreeSpace = $Item.FreeSpace/1024/1024/1024
    $AllComputers += $Object

Open in new window

$AllComputers | select CompName,IPaddress,WindowsVersion,ServicePack,WinInsDate,CurrentUser,ProcType,DiskSize,MemoryCapacity  | Export-Csv c:\Powershell\change2.csv

Open in new window

  What I have to change in the code to see the IP addresses of computers
1 Solution
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Use the following instead:

Export-Csv c:\Powershell\change2.csv  -NoTypeInformation

There could be multiple NIC adapters configured with multiple IP addresses on each of the computer. So when you execute,

$NetAdapters = Get-WmIObject -class "Win32_NetworkAdapterConfiguration" -cn $machine  | where{$_.IPEnabled -eq "True"}

it returns multiple adapters, so $NetAdapters becomes an array object. Please try the line

$IPAddresses = foreach($Adapter in $Netadapters){$adapter.ipaddress}

instead of

$IPAddress =   $NetAdapters.IPAddress
Probably IPaddress is a multi-valued attribute.. Try..

$AllComputers | select CompName,{$_.IPaddress},WindowsVersion,ServicePack,WinInsDate,CurrentUser,ProcType,DiskSize,MemoryCapacity  | Export-Csv c:\temp\change2.csv

Open in new window

We Need Your Input!

WatchGuard is currently running a beta program for our new macOS Host Sensor for our Threat Detection and Response service. We're looking for more macOS users to help provide insight and feedback to help us make the product even better. Please sign up for our beta program today!

nesher13Author Commented:
To Subsun:

it does not solve the problem
Are you getting a empty output?

What you get as a result when you just print $AllComputers?
nesher13Author Commented:
To SreRaj,

thank you very much. it works fine
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now