Exporting WMI Information to CSV

This topic contains 1 reply, has 2 voices, and was last updated by  Olaf Soyk 2 months, 2 weeks ago.

  • Author
    Posts
  • #95205

    Tony P
    Participant

    Hi forum,

    I have been trying to get wmi information from a list of domain attached computers but the end result is an empty csv file. The code I am working with is right below.

    $computers = Get-Content C:\servers1.txt

    $PCAuditArray = @()

    foreach($computer in $computers) {

    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)) {

    $Result = "" | Select Name, Model, CPU, OS, SerialNumber, Application, Vendor

    #Get-WmiObject -Namespace "root\cimv2" -Class Win32_Process -Impersonation 3 -ComputerName $computer
    $All_ComputerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $computer
    $All_ComputerCPU = Get-WmiObject Win32_Processor -ComputerName $computer
    $All_ComputerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $computer
    $All_LogicalDisks = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $computer
    $All_ComputerAPP = Get-wmiobject Win32_Product -ComputerName $computer
    $All_ComputerBIOS = Get-wmiobject Win32_Bios -ComputerName $computer

    $Result.Name = $All_ComputerSystem.name
    $Result.Model = $All_ComputerSystem.model
    $Result.CPU = $All_ComputerCPU.name
    $Result.OS = $All_ComputerOS.name
    $Result.SerialNumber = $All_ComputerBIOS.serialnumber
    $Result.Application = $All_ComputerAPP.name
    $Result.Vendor = $All_ComputerAPP.vendor
    }

    }

    $PCAuditArray | Export-Csv C:\system-info.csv

    Can someone please assist.

    Many thanks,
    Tony

  • #95206

    Olaf Soyk
    Participant

    Tony,
    welcome to the Powershell.org forum. Next time could you please format your code as code. It's easier to read and to understand then. And it avoids unwanted line breaks as well. Thanks.

    You're creating $PCAuditArray but you're not filling it in.

    I created something similar a while ago for a client I was working for. You could use it as a template to adapt it to your needs

    < #
    .SYNOPSIS
    
    Get-PCInfo [[-ComputerName] ]
    
    .DESCRIPTION
    
    .PARAMETER ComputerName
      
    #>
    function Get-WMIPCInfo {
        [CmdletBinding()]
        param(
            $ComputerName = $ENV:ComputerName
        )
        if (Test-Connection $ComputerName -Quiet -Count 1) {
            Write-Debug -Message "$ComputerName reachable"
            $so = New-CimSessionOption -Protocol DCOM 
            $WMIData = New-CimSession -CN $ComputerName -SessionOption $so
            $BIOS = Get-CimInstance -Class CIM_BIOSElement -CimSession $WMIData | Select-Object -Property *
            $OS = Get-CimInstance -Class CIM_OperatingSystem -CimSession $WMIData | Select-Object -Property *
            $ServiceBranch = (([wmiclass]"\\$($ComputerName)\root\default:stdRegProv").GetStringValue(2147483650, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'ReleaseID')).svalue
            $DISK = Get-CimInstance -Class CIM_LogicalDisk -CimSession $WMIData | Where-Object {$_.DeviceID -eq "C:"} | Select-Object -Property *
            $Computer = Get-CimInstance -Class CIM_ComputerSystem -CimSession $WMIData | Select-Object -Property *
            $IPConfig = Get-CimInstance -Class Win32_NetworkAdapterConfiguration -CimSession $WMIData | Where-Object {$_.DefaultIPGateway -match "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}"}  | Select-Object -Property *
            $UserQuery = ( C:\Windows\System32\quser.exe /server:$ComputerName 2> null)
            if ($UserQuery) {
                $UserQuery[1].Trim() -match "^(\S+)\s+.*((\d{2}\.){2}\d{4}\s+\d{2}:\d{2})" | Out-Null
                $LoggedOnUser = $Matches[1]
                $LogonTime = Get-Date -Date $Matches[2]
            }
            Remove-CimSession $WMIData
            Write-Debug -Message "`$BIOS: $BIOS"
            Write-Debug -Message "`$OS:  $OS"
            Write-Debug -Message "`$DISK:  $DISK"
            Write-Debug -Message "`$Computer:  $Computer"
            Write-Debug -Message "`$IPConfig:  $IPConfig"
            Write-Debug -Message "`$LoggedOnUser:  $LoggedOnUser"
            $CIMOutput = [PSCustomObject]@{
                ComputerName   = $BIOS.PSComputerName;
                Model          = $Computer.Model;
                BIOSName       = $BIOS.Name;
                SMBIOSVersion  = $BIOS.SMBIOSBIOSVersion;
                BIOSVersion    = $BIOS.BIOSVersion;
                ReleaseDate    = $BIOS.ReleaseDate;
                SerialNumber   = $BIOS.SerialNumber;
                OSCaption      = $OS.Caption;
                OSVersion      = $OS.Version;
                ServiceBranch  = $ServiceBranch;
                InstallDate    = $OS.InstallDate;
                LastBootUpTime = $OS.LastBootUpTime;
                PhysicalRAM    = [math]::round((($Computer.TotalPhysicalMemory) / 1GB), 2);
                DiskSize       = [math]::round(($DISK.Size / 1GB), 2);
                DiskFreeSpace  = [math]::round(($DISK.FreeSpace / 1GB), 2);
                IPV4Address    = $IPConfig.IPAddress[0];
                DefaultGateway = $IPConfig.DefaultIPGateway[0];
                DNSDomain      = $IPConfig.DNSDomain;
                DHCPEnabled    = $IPConfig.DHCPEnabled;
                LoggedOnUser   = $LoggedOnUser
                LogonTime      = $LogonTime
            }
            $CIMOutput
        }
        else {
            Write-Warning "`tThe computer '$($ComputerName)' is not reachable`n"
        }
    }
    

    It's a little bit quick and dirty but it should run at least and you can format the output how you like it. When you run it without a computername it will run on the local host.

You must be logged in to reply to this topic.