Gathering multiple object data into columned CSV

This topic contains 2 replies, has 2 voices, and was last updated by  Jack Vance 5 months, 4 weeks ago.

  • Author
  • #67579

    Jack Vance

    I am attempting to gather multiple objects of data into a single CSV file. Unfortunately, my knowledge of Powershell is somewhere between "none" and "Started googling it last week".
    What I'm trying to gather is the PC Name in one column. The Mac Address in the second. The serial number in the third column. The last logged in user in the 4th column. The WAN IP in the 5th column.
    This would be run on the local machine. I'm not querying remote objects.
    Is this possible and can anyone assist me?

  • #67584

    Cyril Muffat

    You can do something like that :

    function Get-OSInfo
    param (
            Write-Debug "Computer Name is $ComputerName"
            Write-Verbose "Attempting connection to $ComputerName"
            $os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $ComputerName
            $bios = Get-WmiObject -Class win32_BIOS -ComputerName $ComputerName
            Write-Verbose "Building property list"
            $prop = @{  'ComputerName'=$ComputerName;
            $obj = New-object -Type PSObject -Property $prop
            $obj | Export-Csv -Path C:\test\test.csv -Append -Delimiter ";" -NoTypeInformation
    get-osinfo -ComputerName .

    This is a very handy tool to explore WMI class and find what ur looking for,

  • #67653

    Jack Vance

    I have now completed a working script. I appreciate everyone's help. I figured I should share for anyone who also wants to accomplish this.
    This has 3 requirements to get it working.
    The script must be run from an administrative prompt.
    Powershell 4.0 or higher needs to be installed.
    The following command needs to run before the script: powershell Set-ExecutionPolicy Unrestricted
    # The name of the computer
    $PCName = (gwmi Win32_ComputerSystem).Name
    # The MACAddr of the network enabled adapter. If you have more than one active NIC with an IP assigned (static or DHCP), this could be an issue and need adjusting.
    $MAC = (gwmi win32_networkadapter | where {$_.NetEnabled -eq $True}).MACAddress -replace ":"
    # The Serial Number of the PC
    $SerialNumber = (gwmi Win32_BIOS).SerialNumber
    # LastLoggedInUser
    $LastLoggedInUser = (Get-WmiObject -Class Win32_NetworkLoginProfile | Sort-Object -Property LastLogon -Descending | Select-Object -Property Name -First 1).Name
    # Getting the LastLoginTime and formatting from WMI string to usable Time Format
    # Getting the LastLoginTime and formatting from WMI string to usable Time Format
    $swbem = New-Object -ComObject wbemscripting.swbemdatetime
    $os = (Get-WmiObject -Class Win32_NetworkLoginProfile | Sort-Object -Property LastLogon -Descending | Select-Object -Property LastLogon -First 1).LastLogon
    $swbem.Value = $os
    $LastLogonTime = $swbem.GetVarDate()
    # This may be blocked by a proxy and require a credential paramter for invoke-webrequest, use 'get-help Invoke-WebRequest -full' for more detail
    $WANIP = (Invoke-WebRequest
    #Adding collected info into an array for export as column data. Format is 'labelname=variablename'.
    $Export = New-Object PSObject -Property @{PCName=$PCName; MAC=$MAC; SerialNumber=$SerialNumber; LastLoggedInUser=$LastLoggedInUser; LastLogonTime=$LastLogonTime;WANIP=$WANIP}
    #Exporting to CSV, but doing a select order for the columsn first, since the order of the array above is not guaranteed.
    $Export | Select PCName,MAC,SerialNumber,LastLoggedInUser,LastLogonTime,WANIP | Export-Csv ($pcname + ".CSV") -NoTypeInformation

You must be logged in to reply to this topic.