How to Remotely Collect Computer Inventory Details from Active Directory to Excel Using PowerShell

How to Remotely Collect Computer Inventory Details from Active Directory to Excel Using PowerShell

Eng.Mahmoud Enan

54 года назад

1,029 Просмотров

#eng_mahmoud_enan#Remotely_Inventory#Computer_Inventory#Active_Directory#Windows_Server

In this video, we will demonstrate how to remotely collect detailed computer inventory information from Active Directory and export it to Excel using PowerShell. You will learn step-by-step how to gather essential data such as computer names, operating systems, hardware components, and more.
Command:-
$computers = Get-ADComputer -Filter * -Property Name | Select-Object -ExpandProperty Name
$results = @() # Array to store results

foreach ($computer in $computers) {
try {
$systemInfo = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer
$cpuInfo = Get-CimInstance -ClassName Win32_Processor -ComputerName $computer
$ramInfo = Get-CimInstance -ClassName Win32_PhysicalMemory -ComputerName $computer
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer

$gpuInfo = Get-CimInstance -ClassName Win32_VideoController -ComputerName $computer
$soundInfo = Get-CimInstance -ClassName Win32_SoundDevice -ComputerName $computer
$networkInfo = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ComputerName $computer | Where-Object { $_.IPEnabled -eq $true }
$printerInfo = Get-CimInstance -ClassName Win32_Printer -ComputerName $computer
$scannerInfo = Get-CimInstance -ClassName Win32_Scanner -ComputerName $computer
$diskInfo = Get-CimInstance -ClassName Win32_DiskDrive -ComputerName $computer
$motherboardInfo = Get-CimInstance -ClassName Win32_BaseBoard -ComputerName $computer
$computerProductInfo = Get-CimInstance -ClassName Win32_ComputerSystemProduct -ComputerName $computer

# Retrieve the first IPv4 address
$ipAddress = ($networkInfo | Select-Object -ExpandProperty IPAddress | Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}$' }) -join ', '
# Retrieve disk details
$diskType = $diskInfo | Select-Object -First 1 -ExpandProperty MediaType
$diskSize = [math]::round(($diskInfo | Select-Object -First 1 -ExpandProperty Size) / 1GB)
# Get Free Disk Space
$freeDiskSpace = [math]::round((Get-PSDrive C).Free / 1GB) # Assuming C: drive
# Get last logged-in user
$lastUserProfile = Get-CimInstance -ClassName Win32_UserProfile -ComputerName $computer | Where-Object { $_.Special -eq $false } | Sort-Object LastUseTime -Descending | Select-Object -First 1
$lastLoggedInUser = if ($lastUserProfile) { $lastUserProfile.LocalPath.Split('\')[-1] } else { "No Users" }
# Store results in a hashtable
$result = [PSCustomObject]@{
ComputerName = $computer
Model = $systemInfo.Model
Manufacturer = $systemInfo.Manufacturer
CPU = ($cpuInfo | Select-Object -ExpandProperty Name) -join ', ' # Join CPU names
TotalRAM = [math]::round($systemInfo.TotalPhysicalMemory / 1GB)
OS = $osInfo.Caption
GPU = ($gpuInfo | Select-Object -First 1).Name
SoundCard = ($soundInfo | Select-Object -First 1).Name
NetworkAdapter = ($networkInfo | Select-Object -First 1).Description
ConnectionType = ($networkInfo | Select-Object -First 1).ServiceName
MACAddress = ($networkInfo | Select-Object -First 1).MACAddress
IPAddress = $ipAddress
Printer = if ($printerInfo) { ($printerInfo | Select-Object -First 1).Name } else { "No Printer" }
Scanner = if ($scannerInfo) { ($scannerInfo | Select-Object -First 1).Name } else { "No Scanner" }
DiskType = $diskType
DiskSizeGB = $diskSize
FreeDiskSpaceGB = $freeDiskSpace
MotherboardType = $motherboardInfo.Product
MotherboardSerial = $motherboardInfo.SerialNumber
HardDiskSerial = ($diskInfo | Select-Object -First 1 -ExpandProperty SerialNumber)
ComputerSerial = $computerProductInfo.IdentifyingNumber
LastBootUpTime = $osInfo.LastBootUpTime
ProcessorCount = ($cpuInfo | Measure-Object).Count
BIOSVersion = $systemInfo.BiosVersion -join ', '
LastLoggedInUser = $lastLoggedInUser # Add last logged-in user
}
# Add result to the array
$results += $result
} catch {
Write-Host "Unable to retrieve information from $computer"
}
}
$results | Export-Csv -Path "C:\ComputerInventory.csv" -NoTypeInformation -Encoding UTF8

Eng. Mahmoud Enan
Active Directory
Inventory
Computer Inventory
Network Inventory
Software Inventory
HardWare Inventory
Network Devices
Data Export
Hardware Information
Excel Export

Тэги:

#Eng._Mahmoud_Enan #PowerShell #Active_Directory #Inventory #Computer_Inventory #Remote_Query #Remotely_Inventory #System_Inventory #Active_Directory_Queries #IT_Reporting #Network_Devices #Inventory_Automation #Remote_Collection #Hardware_Information #Software_Inventory #Computer_Details #Windows_Server #Domain_Controller
Ссылки и html тэги не поддерживаются


Комментарии: