Inventory Script Hangs

This is an example of a script that I am currently using, the issue is that I am having is that occasionally, it will stop working after a certain computer from the list. What would cause this and is there a way to add a timeout function so it does not hang on a single system?

$Workstations = Get-Content C:\PSscripts\results2.txt

$workstations | ForEach-Object {
$CPUInfo = Get-WmiObject Win32_Processor -ComputerName $_ -ErrorAction SilentlyContinue
$MODEL = gwmi Win32_ComputerSystem -ComputerName $_ -ErrorAction SilentlyContinue|select -ExpandProperty Model
$SerialNumber = Get-WmiObject win32_computersystemproduct -ComputerName $_ -ErrorAction SilentlyContinue|select -ExpandProperty IdentifyingNumber
$DISPLAY = gwmi Win32_PnPSignedDriver -ComputerName $_ -ErrorAction SilentlyContinue | select deviceclass,devicename | where {$.deviceclass -like “display”}
$OSInfo = Get-WmiObject Win32_OperatingSystem -ComputerName $
-ErrorAction SilentlyContinue
$USERS = get-WmiObject -ComputerName $_ -ClassName Win32_UserProfile -ErrorAction SilentlyContinue| select -ExpandProperty LocalPath
$Date = Get-Date -format “yyyy-MM-d”
foreach ($CPU in $CPUInfo)
{
[PSCustomObject]@{
WorkStationName = $CPU.SystemName
Model = $Model
USERS = $USERS -join ‘, ‘
}

}

} -OutVariable InfoColl

$InfoColl|Export-Csv -Path C:\PSscripts\IOC_Users$((Get-Date).ToString(‘MMddyyyy’)).csv -

I’d say it is pretty likely that one of the computers you have in your input file is not available. You may check this before you query it with a

Regardless of that - Instead of Get-WmiObject you should use Get-CimInstance.

Then, if you really have to query more than one WMI/CIM class you should consider using CIM sessions to wrap all queries you do for one computer. This will speed up you script.

And … it does make any sense to query a lot of WMI/CIM classes when you don’t use tham later on. :wink:

And last but not least … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

I will remember the </> in the future. This is a partial of a script that I use on another network just did not have a convenient way to transfer it over, all the WMIObjects are eventually sent to a CSV, I just did not include it in the snippet for brevity. How would you format it to wrap it in a single ciminstance call?

The version i am actually using, included below, runs a test-connection on the systems before running the commands.

$Workstations = Get-Content C:\PSscripts\WadsList.csv

$workstations | ForEach-Object {
    if (Test-Connection -Computername $_ -Count 1 -ErrorAction
SilentlyContinue)
    {
    $CPUInfo = Get-CimInstance Win32_Processor -ComputerName $_ -ErrorAction
SilentlyContinue
    $MODEL = Get-CimInstance Win32_ComputerSystem -ComputerName $_
-ErrorAction SilentlyContinue|select -ExpandProperty Model
    $SerialNumber = Get-CimInstance win32_computersystemproduct
-ComputerName $_ -ErrorAction SilentlyContinue|select -ExpandProperty
IdentifyingNumber
    $DISPLAY = Get-CimInstance Win32_PnPSignedDriver -ComputerName $_
-ErrorAction SilentlyContinue | select deviceclass,devicename | where
{$_.deviceclass -like "*display*"}
    $OSInfo = Get-CimInstance Win32_OperatingSystem -ComputerName $_
-ErrorAction SilentlyContinue
    $IP = Get-CimInstance win32_NetworkAdapterConfiguration -Filter
'IPEnabled=True' -ComputerName $_ -ErrorAction SilentlyContinue|select
-ExpandProperty IPAddress
    $DNS1 = Get-CimInstance win32_NetworkAdapterConfiguration -Filter
'IPEnabled=True' -ComputerName $_ -ErrorAction SilentlyContinue|select
-ExpandProperty DNSServerSearchOrder
    $Gateway = Get-CimInstance win32_NetworkAdapterConfiguration -Filter
'IPEnabled=True' -ComputerName $_ -ErrorAction SilentlyContinue|select
-ExpandProperty DefaultIPGateway
    $MAC = Get-CimInstance win32_NetworkAdapterConfiguration -Filter
'IPEnabled=True' -ComputerName $_ -ErrorAction SilentlyContinue|select
-ExpandProperty MACAddress
    $OSTotalVisibleMemory = [math]::round(($OSInfo.TotalVisibleMemorySize /
1MB), 2)
    $PhysicalMemory = Get-CimInstance CIM_PhysicalMemory -ComputerName $_
-ErrorAction SilentlyContinue| Measure-Object -Property capacity -Sum | % {
[Math]::Round(($_.sum / 1GB), 2) }
    $DiskInfo = Get-CimInstance win32_LogicalDisk -ComputerName $_ -Filter
"DeviceID='C:'" -ErrorAction SilentlyContinue|Select-Object
DeviceID,Size,FreeSpace
    $DiskName = ($DiskInfo.DeviceID)
    $DiskSize = [math]::round($DiskInfo.Size / 1GB, 2)
    $DiskFreeSpace = [math]::Round($DiskInfo.freespace / 1GB, 2)
    $HHDSerial = Get-CimInstance win32_physicalmedia -ComputerName $_
-ErrorAction SilentlyContinue|select -ExpandProperty serialnumber
    $LastUser = Get-CimInstance Win32_ComputerSystem -ComputerName $_
-ErrorAction SilentlyContinue|select username 
    $User = Get-CimInstance -ComputerName $_ -Classname Win32_userprofile
-ErrorAction SilentlyContinue | Select -ExpandProperty LocalPath
    $CurrentBuild = Invoke-Command -ComputerName $_ -ScriptBlock
{(Get-ItemProperty HKLM:\SOFTWARE\USAF\SDC\ImageRev).CurrentBuild}
-ErrorAction SilentlyContinue
    $Patches = Get-CimInstance -ClassName Win32_QuickFixEngineering
-ComputerName $_ -ErrorAction SilentlyContinue -ExpandProperty
    $Date = Get-Date -format "yyyy-MM-d"
    foreach ($CPU in $CPUInfo)
    {
        [PSCustomObject]@{
            WorkStationName              = $CPU.SystemName
            Serial                       = $SerialNumber
            Model                        = $Model.model
            Display                      = $DISPLAY -join ‘; ‘
            OS                           = $OSInfo.Version
            CB                           = $CurrentBuild
            IP                           = $IP -join '; '
            DNS                          = $DNS1 -join '; '
            Gateway                      = $Gateway -join '; '
            MAC                          = $MAC -join '; '
            OSTotalVisibleMemory         = $OSTotalVisibleMemory
            PhysicalMemory               = $PhysicalMemory
            DiskInfo                     = $DiskInfo -join '; '
            DiskSize                     = $DiskSize -join '; '
            DiskFreeSpace                = $DiskFreeSpace -join '; '
            HHDSerial                    = $HHDSerial -join '; '
            LastUser                     = $LastUser
            User                         = $User.localpath -join '; '
            Patches                      = $Patches.HotFixID -join '; '
            Date                         = $Date
        }
        }
    }
} -OutVariable InfoColl

$InfoColl|Export-Csv -Path
c:\PSscripts\$((Get-Date).ToString('MMddyyyy')).csv

Not a single CIM instance but a single CIM session! :wink:

But since the code you posted is broken I will show an example instead:

$ComputerNameList = 'Computer1', 'Computer2'
$Result =
foreach ($ComputerName in $ComputerNameList) {
    if (Test-Connection -ComputerName $ComputerName) {
        $CimSession = New-CimSession -ComputerName $ComputerName
        
        $OS       = Get-CimInstance -ClassName CIM_OperatingSystem -CimSession $CimSession
        $BIOS     = Get-CimInstance -ClassName CIM_BIOSElement     -CimSession $CimSession
        $Computer = Get-CimInstance -ClassName CIM_ComputerSystem  -CimSession $CimSession
        [PSCustomObject]@{
            ComputerName    = $ComputerName
            OperatingSystem = $OS.Caption
            Manufacturer    = $Bios.Manufacturer
            RAM             = [MATH]::Ceiling($Computer.TotalPhysicalMemory / 1GB)
        }
        Remove-CimSession -CimSession $CimSession
    }
}
$Result