Setup: Remote computer with WMI enabled and firewall open
Problem: You want to collect various information remotely
Solution:
Copy this script and save it on the disk with .ps1 extension. eg. Get-ComputerDetails.ps1
Open PowerShell console and execute the script.
# The script takes the name of the remote computer as parameter
param(
[string] $strComputer
)
# ask for credentials to connect to remote computer. Credentials can be stored on the disk for unattended execution.
$cred = get-credential
$result = "" | select Name
Add-Member -memberType NoteProperty -name Uptime -inputObject $result -value ""
Add-Member -memberType NoteProperty -name DNS_IP_Address -inputObject $result -value ""
Add-Member -memberType NoteProperty -name No_Processors -inputObject $result -value 0
Add-Member -memberType NoteProperty -name No_Cores_perProcessor -inputObject $result -value 0
Add-Member -memberType NoteProperty -name Processor_Type -inputObject $result -value ""
Add-Member -memberType NoteProperty -name Processor_Speed -inputObject $result -value ""
Add-Member -memberType NoteProperty -name No_Memory_Slots_Used -inputObject $result -value 0
Add-Member -memberType NoteProperty -name RAM_Configuration -inputObject $result -value "N/A"
Add-Member -memberType NoteProperty -name RAM_Total_MB -inputObject $result -value ""
Add-Member -memberType NoteProperty -name caption -inputObject $result -value ""
Add-Member -memberType NoteProperty -name domain -inputObject $result -value ""
Add-Member -memberType NoteProperty -name Service_Pack -inputObject $result -value ""
Add-Member -memberType NoteProperty -name SystemType -inputObject $result -value ""
Add-Member -memberType NoteProperty -name Manufacturer -inputObject $result -value ""
Add-Member -memberType NoteProperty -name Model -inputObject $result -value ""
$result.name = $strComputer
# Find the IP address registered in DNS
$result.DNS_IP_Address=([System.Net.Dns]::GetHostAddresses($strComputer) | select IPAddressToString).IPAddressToString
# Query WMI for basic information about the OS
$OS = get-wmiobject Win32_OperatingSystem -computername $strComputer -credential $cred| select caption,csdversion,LastBootUpTime
$result.caption = $OS.Caption
$result.Service_Pack = $OS.CSDVersion
# Query WMI information remotely about computer hardware
$Computer = get-wmiobject Win32_computerSystem -computername $strComputer -credential $cred| select domain,systemtype,Manufacturer,Model,TotalPhysicalMemory,NumberOfProcessors
$result.domain = $computer.domain.split(".")[0].toupper()
$result.SystemType = $Computer.SystemType.trimend("-based PC")
$result.Manufacturer = $Computer.Manufacturer.trimend("Corporation")
$result.Model = $Computer.Model
# Query WMI information remotely about processors and number of cores
$processors = get-wmiobject win32_Processor -computername $strComputer -credential $cred| select Name,MaxClockSpeed,NumberOfCores
$result.No_Processors = $Computer.NumberOfProcessors
if ($Computer.NumberOfProcessors -eq 1) {
$result.Processor_Type = $processors.Name -replace "\s *", " "
$result.Processor_Speed = $processors.MaxClockSpeed
if ($processors.NumberOfCores) {$result.No_Cores_perProcessor = $processors.NumberOfCores}
} else {
if ($processors.NumberOfCores) {$result.No_Cores_perProcessor = $processors.NumberOfCores}
$result.Processor_Type = $processors[0].Name -replace "\s *", " "
$result.Processor_Speed = $processors[0].MaxClockSpeed
}
# Compute the uptime of the system
$lastboot = [System.Management.ManagementDateTimeconverter]::ToDateTime("$($OS.LastBootUpTime)")
$uptime = (Get-Date) - $lastboot
$result.Uptime = $uptime.days.tostring()+" days, "+$uptime.hours.tostring()+" hours"
#Query WMI information remotely about memory configuration
$memory = get-wmiobject win32_PhysicalMemory -computername $strComputer -credential $cred| select devicelocator,capacity
$result.No_memory_slots_used = $memory.count
$MemoryConfiguration = ""
if ($Computer.Model -ne "Virtual Machine") {
foreach ($memorybank in $memory)
{
$memcap= "{0:N0}" -f ($memorybank.capacity/1GB)
$MemoryConfiguration += ";"+$memorybank.DeviceLocator + ", " + $memcap + "GB"
}
$result.RAM_Configuration = $MemoryConfiguration.substring(1)
}
[int]$mem = $Computer.TotalPhysicalMemory/1mb
$result.RAM_Total_MB = $mem
$result
Tip 1: For unattended execution save the credentials on the disk and load them at run-time. Store a secure string (eg. a password) localy on disk in PowerShell
Tip 2: Export the result to csv adding this line at the end of the script
$result | Export-Csv -NoTypeInformation c:\path_to_file.csv