powershelltcpsystem.diagnosticsarptraceroute

PowerShell interrogate a remote system


I have a function that I wrote that interrogates my local system. Just gathering whatever information I can get in a useful format. So I was wondering about a function that could interrogate other systems in this kind of way. function sys-remote <$ip-or-hostname> With that it could then try and return as much information about that system as it can. It's just an idea really, and I guess a number of points would be useful:

• With an IP address, how can we resolve the hostname in the most PowerShell'ish way?

• Whether a hostname of IP address is provided, can we resolve as much information as possible. i.e. MAC address, hostname, IP (and possibly other IP addresses if these can be visible to us)?

• Can we recover shared drives on that system so can see a list of possible shares to connect to.

• What about system information, would that always require WinRM, or can WMI or CIM suffice for most of the things in the below?

• Maybe return also a comma-separate list of whatever ports are open on that remote system if possible?

• What if the remote system is Linux. How much of the above can we reasonably obtain from a Linux system that we interrogate remotely from our Windows system (I guess that WinRM and WMI are out, but maybe CIM is still possible?)?

In general, it would be really useful to return a dump of information like this from a diagnostic point of view as would give a ton of information about a system to work from. Anything like the above (or indeed any other useful things to check for that I've not thought of here) would be really appreciated.

function sys {
    $System = get-wmiobject -class "Win32_ComputerSystem"
    $Mem = [math]::Ceiling($System.TotalPhysicalMemory / 1024 / 1024 / 1024)

    $wmi = gwmi -class Win32_OperatingSystem -computer "."
    $LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
    [TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
    $s = "" ; if ($uptime.Days -ne 1) {$s = "s"}
    $uptime_string = "$($uptime.days) day$s $($uptime.hours) hr $($uptime.minutes) min $($uptime.seconds) sec"

    $job_cpu         = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).Name }
    $job_cpu_cores   = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).NumberOfCores }
    $job_cpu_logical = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).NumberOfLogicalProcessors }
    ""
    "Hostname:        $($System.Name)"
    "Domain:          $($System.Domain)"
    "PrimaryOwner:    $($System.PrimaryOwnerName)"
    "Make/Model:      $($System.Manufacturer) ($($System.Model))"  #     "ComputerModel:  $((Get-WmiObject -Class:Win32_ComputerSystem).Model)"
    "SerialNumber:    $((Get-WmiObject -Class:Win32_BIOS).SerialNumber)"
    "PowerShell:      $($PSVersionTable.PSVersion)"
    "Windows Version: $($PSVersionTable.BuildVersion),   Windows ReleaseId: $((Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name 'ReleaseId').ReleaseId)"
    "Display Card:    $((Get-WmiObject -Class:Win32_VideoController).Name)"
    "Display Driver:  $((Get-WmiObject -Class:Win32_VideoController).DriverVersion),   Description: $((Get-WmiObject -Class:Win32_VideoController).VideoModeDescription)"
    "Last Boot Time:  $([Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem | select 'LastBootUpTime').LastBootUpTime)),   Uptime: $uptime_string"
    $IPDefaultAddress = @(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIpGateway})[0].IPAddress[0]
    $IPDefaultGateway = @(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIpGateway})[0].DefaultIPGateway[0]
    "Default IP:      $IPDefaultAddress / $IPDefaultGateway"
    Get-Netipaddress | where AddressFamily -eq IPv4 | select IPAddress,InterfaceIndex,InterfaceAlias | sort InterfaceIndex
    ""
    Wait-Job $job_cpu         | Out-Null ; $job_cpu_out = Receive-Job -Job $job_cpu
    Wait-Job $job_cpu_cores   | Out-Null ; $job_cpu_cores_out = Receive-Job -Job $job_cpu_cores 
    Wait-Job $job_cpu_logical | Out-Null ; $job_cpu_logical_out = Receive-Job -Job $job_cpu_logical
    "CPU:   $job_cpu_out"
    "CPU Cores:   $job_cpu_cores_out,      CPU Logical Cores:   $job_cpu_logical_out"
    # Get-PSDrive | sort -Descending Free | Format-Table
    gwmi win32_logicaldisk | Format-Table DeviceId, VolumeName, @{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},@{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}}
    gwmi win32_winsat | select-object CPUScore,D3DScore,DiskScore,GraphicsScore,MemoryScore,WinSPRLevel | ft   # removed ,WinSATAssessmentState
    get-WmiObject -class Win32_Share | ft
}

Solution

  • No reason to do this sort of thing from scratch. There are many existing scripts for what you are doing. Via the Microsoft powershellgallery.com.

    PowerShell Script For Desktop Inventory Basic script to collect desktop inventory.

    PowerShell Hardware Inventory Script Scenario:PowerShell hardware Inventory Script. Have you ever wanted to have an inventory without the hassle of going to each finding the information needed to fill the information for your inventory? It is important to keep your inventory up to date. Every time there is a change y

    DownloadGet-Inventory.ps1

    You can just take your script and use Invoke-Command (Runs commands on local and remote computers.) in a PowerShell remote session to get remote computer info.