Need PowerShell Function help

Rockamakis 1 Reputation point
2021-09-24T17:15:24.783+00:00

I am relatively new to PowerShell and attempting to create a function to get system inventory.

Function Get-SystemInfo($ComputerName)
{

Foreach ( $computer in $computerName ) {

Write-Progress -Activity "Collecting System Information of $computer"

$system = @()

$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer
If($OS.Version -eq "10.0.17134") { $OS.Version = '1803' }
elseif($OS.Version -eq "10.0.17763") { $OS.Version = '1809' }
elseif($OS.Version -eq "10.0.18362") { $OS.Version = '1903' }
elseif($OS.Version -eq "10.0.18363") { $OS.Version = '1909' }
elseif($OS.Version -eq "10.0.19041") { $OS.Version = '2004' }
elseif($OS.Version -eq "10.0.19042") { $OS.Version = '2010' }
elseif($OS.Version -eq "10.0.20206") { $OS.Version = '2101' }

$sheetS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer
If ($null -eq $sheets.UserName) { Get-WinEvent -Computer $Computer -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 1|
Select-Object @{N='User';E={$_.Properties[1].Value}} { $sheets.UserName = 'User' }}

$pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$Computer'"
$cpu = Get-WmiObject -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object -Property Name, Manufacturer, Model, UserName
$bios = Get-WmiObject -ClassName Win32_Bios -ComputerName $computer

$MAC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $computer | Select-Object -Property @{name='IPAddress';Expression={($_.IPAddress[0])}},MacAddress | Where-Object IPAddress -NE $null

$monitor = Get-WmiObject WmiMonitorID -ComputerName $computer -Namespace root\wmi |
Select-Object @{n="Manufacturer";e={[System.Text.Encoding]::ASCII.GetString($.ManufacturerName -ne 00)}},
@{n="Model";e={[System.Text.Encoding]::ASCII.GetString($
.UserFriendlyName -ne 00)}},
@{n="Serial_Number";e={[System.Text.Encoding]::ASCII.GetString($_.SerialNumberID -ne 00)}}

$AD = Get-ADComputer $computer -Properties * | Select-Object DistinguishedName, LastLogonDate, OperatingSystemVersion, lastLogonTimestamp, whenChanged, whenCreated, CanonicalName, streetaddress, location
If($OS.Version -eq "10.0.17134") { $OS.Version = '1803' }
elseif($OS.Version -eq "10.0.17763") { $OS.Version = '1809' }
elseif($OS.Version -eq "10.0.18362") { $OS.Version = '1903' }
elseif($OS.Version -eq "10.0.18363") { $OS.Version = '1909' }
elseif($OS.Version -eq "10.0.19041") { $OS.Version = '2004' }
elseif($OS.Version -eq "10.0.19042") { $OS.Version = '2010' }
elseif($OS.Version -eq "10.0.20206") { $OS.Version = '2101' }

foreach ( $hardware in $computer) {
$SystemName = $hardware
$SystemStatus = $pingStatus
$UserName = $sheetS.UserName.trim("\")
$OSVersion = $OS.Version
$MacAddress = $MAC.MacAddress
$SystemMan = $cpu.Manufacturer
$SystemMod = $cpu.Model
$SystemSerial = $bios.SerialNumber
$Monitor1Man = $monitor[0]
$Monitor1Mod = $monitor[1]
$Monitor1Ser = $monitor[2]
$Monitor2Man = $monitor[3]
$Monitor2Mod = $monitor[4]
$Monitor2Ser = $monitor[5]
$Monitor3Man = $monitor[6]
$Monitor3Mod = $monitor[7]
$Monitor3Ser = $monitor[8]
$Street = $AD.streetaddress
$Location = $AD.location
$OU2 = $AD.CanonicalName

$SystemInfo = [PSCustomObject]@{Computer = $SystemName;
Status = $SystemStatus;
UserName = $UserName;
OS_Version = $OSVersion;
MAC_Address = $MacAddress;
System_Manufacturer = $SystemMan;
System_Model = $SystemMod;
System_Serial_Number = $SystemSerial;
Monitor_1_Manufacturer = $Monitor1Man;
Monitor_1_Model = $Monitor1Mod;
Monitor_1_Serial = $Monitor1Ser;
Monitor_2_Manufacturer = $Monitor2Man;
Monitor_2_Model = $Monitor2Mod;
Monitor_2_Serial = $Monitor2Ser;
Monitor_3_Manufacturer = $Monitor3Man;
Monitor_3_Model = $Monitor3Mod;
Monitor_3_Serial = $Monitor3Ser;
Street_Address = $Street;
Location = $Location;
OU = $OU2}

$system += $SystemInfo

}
$props = 'Computer','Status','User_Name','OS_Version','MAC_Address','System_Manufacturer','System_Model','System_Serial_Number','Monitor_1_Manufacturer','Monitor_1_Model','Monitor_1_Serial','Monitor_2_Manufacturer','Monitor_2_Model','Monitor_2_Serial','Monitor_3_Manufacturer','Monitor_3_Model','Monitor_3_Serial','Street_Address','Location','OU'
$system = ($system | Select-Object $props | Sort-Object Computer)
$system

    }

} # End Function

Right now if I run it it only prints out the system name, street address, location and OU with no other information. The end goal is for the output to display all the information of connected systems and for the disconnected systems it will only display the information that can be pulled from AD.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,756 Reputation points
    2021-09-24T20:22:36.64+00:00

    I took the liberty of neatening up your code and fixing a few errors. There's still some work for you to do to get the monitor data into discrete properties.

    Function Get-SystemInfo($ComputerName) {
        $versionhash = @{
                    "10.0.17134" = '1803'
                    "10.0.17763" = '1809'
                    "10.0.18362" = '1903'
                    "10.0.18363" = '1909'
                    "10.0.19041" = '2004'
                    "10.0.19042" = '2010'
                    "10.0.20206" = '2101'
                }
    
        Foreach ( $computer in $computerName ) {
            Write-Progress -Activity "Collecting System Information of $computer"
    
            $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer
            if ($versionhash.ContainsKey($OS.Version)){
                $OS.Version = $versionhash.($OS.Version)
            }
            else{
                $OS.Version = "UNKNOWN $($OS.Version)"
            }
            $sheetS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer
            If ($null -eq $sheets.UserName) {
                $SheetS.UserName =  Get-WinEvent -Computer $Computer -FilterHashtable @{Logname = 'Security'; ID = 4672 } -MaxEvents 1 |
                                        Select-Object -ExpandProperty $_.Properties[1].Value
            }
    
            $pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$Computer'"   # Why not use Test-Connection cmdlet?
            $cpu =  Get-WmiObject -ClassName Win32_ComputerSystem -ComputerName $computer | 
                        Select-Object -Property Name, Manufacturer, Model, UserName
            $bios = Get-WmiObject -ClassName Win32_Bios -ComputerName $computer
            $MAC =  Get-WmiObject win32_networkadapterconfiguration -ComputerName $computer | 
                        Select-Object -Property @{name = 'IPAddress'; Expression = { ($_.IPAddress[0]) } }, MacAddress | Where-Object IPAddress -NE $null
            $monitor =  Get-WmiObject WmiMonitorID -ComputerName $computer -Namespace root\wmi |
                            Select-Object   @{n = "Manufacturer"; e = { [System.Text.Encoding]::ASCII.GetString($_.ManufacturerName -ne 00) } },
                                            @{n = "Model"; e = { [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName -ne 00) } },
                                            @{n = "Serial_Number"; e = { [System.Text.Encoding]::ASCII.GetString($_.SerialNumberID -ne 00) } }
            Try{
                $AD =   Get-ADComputer $computer -Properties * -ErrorAction STOP | 
                            Select-Object DistinguishedName, LastLogonDate, OperatingSystemVersion, lastLogonTimestamp, whenChanged, whenCreated, CanonicalName, streetaddress, location
            }
            Catch{
                $AD = "" | Select-Object @{n=DistinguishedName;e={$computer + 'Not found in AD'}}, LastLogonDate, OperatingSystemVersion, lastLogonTimestamp, whenChanged, whenCreated, CanonicalName, streetaddress, location
            }
    
            [PSCustomObject]@{
                Computer                             = $computer
                Status                               = $pingStatus
                UserName                             = $sheetS.UserName.trim("\")
                OS_Version                           = $OS.Version
                MAC_Address                          = $MAC.MacAddress
                System_Manufacturer                  = $cpu.Manufacturer
                System_Model                         = $cpu.Model
                System_Serial_Number                 = $bios.SerialNumber
                Monitor_1_Manufacturer               = $monitor[0]
                Monitor_1_Model                      = $monitor[1]
                Monitor_1_Serial                     = $monitor[2]
                Monitor_2_Manufacturer               = $monitor[3]
                Monitor_2_Model                      = $monitor[4]
                Monitor_2_Serial                     = $monitor[5]
                Monitor_3_Manufacturer               = $monitor[6]
                Monitor_3_Model                      = $monitor[7]
                Monitor_3_Serial                     = $monitor[8]
                Street_Address                       = $AD.streetaddress
                Location                             = $AD.location
                OU                                   = $AD.CanonicalName
            }
        }
    } # End Function
    
    # how to use function
    Get-SystemInfo 'WS06' |
        Sort-Object Computer
    
    0 comments No comments

  2. Limitless Technology 39,496 Reputation points
    2021-09-27T17:38:11.053+00:00

    Hello @Rockamakis

    I would recommend to check the suggestions on the next post, as it covers the same topic already:

    https://social.technet.microsoft.com/Forums/Azure/en-US/268387c7-dea5-41c8-a986-8a49c18e3041/asset-inventory-assistance-with-powershell-script?forum=winserverpowershell

    Hope this helps with your query,

    --------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments