Renil
Renil

Reputation: 11

Powershell: How to export AD PC results to CSV

newbie question here. I have this script below for powershell that gives me the PC information from AD. but i've been having trouble exporting the results to csv. also how do just get the ipv4 ip address and dont include ipv6? and say if the PC is offline, can it just give me another column to show that if it's offline, it returns like RPC server unavailable or Offline.enter image description here Appreciate the help.

$ArrComputers = Get-Content "C:\Temp\computers-systeminfo.txt"
#Specify the list of PC names in the line above. "." means local system

Clear-Host
foreach ($Computer in $ArrComputers) 
{
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
    $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
    $computerIPv4 = Get-WmiObject WIn32_NetworkAdapterConfiguration -Computer $Computer


        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
        "-------------------------------------------------------"
        "Manufacturer: " + $computerSystem.Manufacturer
        "Model: " + $computerSystem.SystemFamily
        "Machine Name: " + $computerSystem.DNSHostName
        "IP Address: " + $computerIPv4.Ipaddress
        "Serial Number: " + $computerBIOS.SerialNumber
        "CPU: " + $computerCPU.Name
        "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
        "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
        "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
        "Operating System: " + $computerOS.caption + " "+ $computerOS.BuildNumber

        "User logged In: " + $computerSystem.UserName
        "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        ""
        "-------------------------------------------------------"
}

Upvotes: 1

Views: 1408

Answers (2)

Dilly B
Dilly B

Reputation: 1472

assign a variable to foreach and output the results to a CSV file. The below code is not tested but should work. Using PscustomObject will make the data structured and a easy way to handle data. To learn more please refer https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.2

To find a PC online or offline you can use Test-Connection and save the result in pscustomobject.

    $ArrComputers = Get-Content "C:\Temp\computers-systeminfo.txt"
#Specify the list of PC names in the line above. "." means local system

Clear-Host
$output = foreach ($Computer in $ArrComputers) {
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
    $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
    $computerIPv4 = Get-WmiObject WIn32_NetworkAdapterConfiguration -Computer $Computer

    [PSCustomObject]@{
        "Manufacturer"     = $computerSystem.Manufacturer
        "Model"            = $computerSystem.SystemFamily
        "Machine Name"     = $computerSystem.DNSHostName
        "IP Address"       = $computerIPv4.Ipaddress
        "Serial Number"    = $computerBIOS.SerialNumber
        "CPU"              = $computerCPU.Name
        "HDD Capacity"     = "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
        "HDD Space"        = "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) + "Free(" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
        "RAM"              = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
        "Operating System" = $computerOS.caption + " " + $computerOS.BuildNumber
        "User logged In"   = $computerSystem.UserName
        "Last Reboot"      = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
    }
}
$output | Export-csv C:\Temp\outputfilename.csv -NoTypeInformation

Upvotes: 1

JPBlanc
JPBlanc

Reputation: 72680

Much questions :

First for RPC error at the begining of the loop try to establish a cimsession :

$cimSession = New-CimSession -ComputerName $Computer
if ($cimSession -eq $null)
{
  write-host "Pas Glop $Computer"
  continue
}

At leasr

For IPV4 :

$IpAddressV4 = (Get-NetIPConfiguration -CimSession $cimSession | Where-Object { $_.IPv4DefaultGateway -ne $null -and  $_.NetAdapter.Status -ne "Disconnected"  }).IPv4Address.IPAddress

To Put everythin in a CSV file :

$lines = @()
foreach ($Computer in $ArrComputers) 
{
  $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
  $line = New-Object -TypeName PSCustomObject -Property @{"Computer" = $Computer;"Manufacturer" = $($computerSystem.Manufacturer)}
  $lines += $line
}
$lines | Export-Csv "PathToYourCSVFile" -NoTypeInformation

Upvotes: 0

Related Questions