Query Single PC

$Computers = Read-host ("Enter The computer name that you want to query? (example: COMPNAME)")

Foreach ($computer in $Computers)
  {
    $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
    $computerHDDType = Get-WMiObject Win32_diskdrive -ComputerName $Computer | Where-Object Mediatype -EQ 'Fixed Hard disk media' | Select SystemName,Model
    $disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
write-host ""
        write-host "System Information for: " $computerSystem.Name -BackgroundColor Yellow -ForegroundColor Black
        "-------------------------------------------------------"
        "Model : " + $computerSystem.Model
        "Serial Number : " + $computerBIOS.SerialNumber
        "BIOS : " + $computerBIOS.SMBIOSBIOSVersion
        "CPU: " + $computerCPU.Name
        "HDD Model/Type : " + $computerHDDType.Model
{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
        "Disk : Remote computer C: disk has {0:#.0} GB free of {1:#.0} GB Total" -f ($disk.FreeSpace/1GB),($disk.Size/1GB)
        "RAM : " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + " GB Available"
        "Operating System: " + $computerOS.caption + " | Service Pack: " + $computerOS.Version + " " + "(" + " " + $ComputerOS.OSArchitecture + " " + ")"
        "User logged In : " + $computerSystem.UserName
        "Last Reboot : " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        "-------------------------------------------------------"

 Write-Host “Press a key to continue…”
 $x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyUp”)
   }

I am asking for some assistance from some PS experts. I have gotten as far as I can with this script. This has been working great for me and my team. I am noticing that if a PC has a single SSD/HDD then it reports everything well…if it has a couple disks…it shows both disks but only reports the C: drive diskspace…

If I could ask for some help in querying both disks (if found) and report the size and availability for each (again, if available)

If you want to report all disks available then replace:

$disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")

with this

foreach ($drive in $computerHDD)
{
    $disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='$($drive.DeviceID)'")

    write-host ""
    # The rest of reporting code goes here
}

Although there is so much more to improve your script, for example Get-CimInstance instead of Get-WmiObject and Write-Information instead of Write-Host

2 Likes

Totally agreed … and to show you a little bit what it’s meant:

$Computer   = $ENV:COMPUTERNAME
$CimSession = New-CimSession -ComputerName $Computer
$OS   = Get-CimInstance -CimSession $CimSession -ClassName Win32_OperatingSystem
$PC   = Get-CimInstance -CimSession $CimSession -ClassName Win32_ComputerSystem
$BIOS = Get-CimInstance -CimSession $CimSession -ClassName Win32_BIOS
$CPU  = Get-CimInstance -CimSession $CimSession -ClassName Win32_Processor

[PSCustomObject]@{
    OSCaption    = $OS.Caption
    OSVersion    = $OS.Version
    Model        = $PC.Model
    Manufacturer = $PC.Manufacturer
    BIOS         = $BIOS.SMBIOSBIOSVersion
    CPU          = $CPU.Name
}

I think that’s much easier to read an it should even run a little bit faster as you don’t have to create a new connection for each query. Instead you “re-use” the same cim session for all queries. :wink:

4 Likes

Thank you for this tid-bit…I am trying to figure out and incorporate this into the script…
My initial trials have gone poorly…I’m showing the space available in the 2nd disk and not the 1st C:drive trying to discern why it’s not reading off the 1st disk but does see both if you query the logical disk:

Querying my own local disks…
…
PS C:\WINDOWS\system32> Get-WmiObject Win32_logicaldisk -filter DriveType=3

DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 93154594816
Size : 178429927424
VolumeName : OSDisk

DeviceID : D:
DriveType : 3
ProviderName :
FreeSpace : 441613918208
Size : 500105736192
VolumeName :
…

That does work a bit better and much easier…I’m going to have to play around with this to learn more…
Thank you for this!!!

Here is an example including suggestions from Olaf, and there is one “todo” within code for you to solve as well:


param (
    [Parameter(Mandatory = $true)]
    [string[]] $Domain = [System.Environment]::MachineName
)

$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"

foreach ($Computer in $Domain)
{
    if (($Computer -ne [System.Environment]::MachineName) -and ($Computer -ne "localhost"))
    {
        $Cred = Get-Credential -Message "Credentials are required to acess '$Computer'"
        $CimServer = New-CimSession -ComputerName $Computer -Credential $Cred
    }
    else
    {
        $CimServer = New-CimSession -ComputerName $Computer
    }

    $PSDefaultParameterValues["Get-CimInstance:CimSession"] = $CimServer

    $System = Get-CimInstance -ClassName Win32_ComputerSystem
    $BIOS = Get-CimInstance -ClassName Win32_BIOS
    $OS = Get-CimInstance -ClassName Win32_OperatingSystem
    $CPU = Get-CimInstance -ClassName Win32_Processor
    $Disk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType = 3"
    $DiskType = Get-CimInstance -ClassName Win32_diskdrive |
    Where-Object Mediatype -EQ 'Fixed Hard disk media'
    Remove-CimSession -CimSession $CimServer

    Write-Information -MessageData "System info: $($System.Name)"

    $Result = [PSCustomObject]@{
        Model = $System.Model
        SerialNumber = $BIOS.SerialNumber
        BIOS = $BIOS.SMBIOSBIOSVersion
        CPU = $CPU.Name
        RAM =  [string]::Format("{0:N2} GB", $System.TotalPhysicalMemory / 1GB)
        System = $OS.Caption
        ServicePack = "$($OS.Version) ($($OS.OSArchitecture))"
        User = $System.UserName
        LastReboot = [System.Convert]::ToDateTime($OS.LastBootUpTime)

        # TODO: This should be part of second loop
        DriveModel = $DiskType.Model
    }

    foreach ($Drive in $Disk)
    {
        $FreeSpace = @{
            MemberType = "NoteProperty"
            Name = "FreeSpace $($Drive.DeviceID)"
            Value = [string]::Format("{0:N2} GB", $Drive.FreeSpace / 1GB)
            Force = $true
        }

        $DiskSize = @{
            MemberType = "NoteProperty"
            Name = "DiskSize $($Drive.DeviceID)"
            Value = [string]::Format("{0:N2} GB", $Drive.Size / 1GB)
            Force = $true
        }

        $Result | Add-Member @FreeSpace
        $Result | Add-Member @DiskSize
    }
    
    Write-Output $Result
}
3 Likes