Error catching some info from remote computers who dont answer to ping

1

With little knowledge, I managed to assemble the script shown below, to obtain the amount of ram memory that the teams registered in the AD of the company have.

#Import AD's module
	Import-Module ActiveDirectory

#Grab a list of computer names from Active Directory (in City 3)
$ComputerList = Get-ADComputer -Filter * -searchbase "OU=Workstations,OU=Machines,OU=CUSTOM,DC=xxxxxx,DC=xxx" | select-object Name

#Output file
	$csvOutput = 'C:\Temp\RAM\RAM List.csv'
#Deletes the output file if it exists
	If (Test-Path $csvOutput){
		Remove-Item $csvOutput
	}
	#Fills in the first line of the output file with the headline
	Add-Content -Path $csvOutput -Value "Name,Pingable,RAM"

#Go through each computer in the List
$ComputerList | % {
	
	#Put the current computer name in a variable called $ComputerName
	$ComputerName = $_.Name
	
	#Ping the remote computer
	$Ping = Test-Connection $ComputerName -Count 2 -EA Silentlycontinue
    
    $colItems = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $ComputerName
	
	If ($ping){
		#If Ping is successfull, try to grab IE's version and put it in $IEVersionString's variable.
		#$IEVersionString = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("\\$ComputerName\C$\Program Files\Internet Explorer\iexplore.exe").Fileversion
		foreach ($objItem in $colItems){
        $displayGB = [math]::round($objItem.TotalPhysicalMemory/1024/1024/1024, 0)
        }
		#Edit the CSV file and add an extra line with the results of the above operations (Ping/IE Version)
		Add-Content -Path $csvOutput -Value "$($ComputerName),YES,$($displayGB)"
		#Write console output and show what computer is being processed and IE's version
		Write-Host "$($ComputerName) - $($displayGB) "GB""
}

}
    Else{
		#If we're here, the machine is NOT pingable
		#Edit the CSV file and add an extra line with the results of the Ping (No)
		Add-Content -Path $csvOutput -Value "$($ComputerName),NO,N/A"
		#Write console output and show what computer is being processed and state that it's not pingable
		Write-Host "$($ComputerName) - Not Pingable"
}

The script works, but on some computers that do not respond to the ping, it throws the error:

Get-WmiObject : El servidor RPC no está disponible. (Excepción de HRESULT: 0x800706BA)
En C:\Users\fcaballe\Desktop\GetRam_AD-Source.ps1: 25 Carácter: 30
+     $colItems = get-wmiobject <<<<  -class "Win32_ComputerSystem" -namespace "root\CIMV2" -comput
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

How I can avoid this error and simply get a "Not Pingable" definition?

powershell
scripting
rpc
asked on Stack Overflow Mar 21, 2019 by Facundo Caballe • edited Mar 21, 2019 by (unknown user)

1 Answer

0

here's one way to do this. [grin] i did not use Invoke-Command to get things to run in parallel since you did not indicate that such was needed. if you DO need more speed, then convert the foreach into a scriptblock and call that with Invoke-Command and the list of accessible systems.

what it does ...

  • creates a fake list of computers
    that should be done via Import-CSV or with something like Get-ADComputer.
  • sets the "not reachable" message
  • iterates thru the system list
  • checks for "is it there?"
  • if it responds, then get the RAM & IE info
  • if it does NOT respond, set the two items to the "not reachable" message
  • builds a custom object that will export to a CSV neatly
  • sends the object out to the $Results variable
  • finishes the iteration
  • shows the $Results collection on screen
  • sends that collection to the CSV file

here's the code ...

# fake reading in a CSV file
#    in real life, use Import-CSV [or Get-ADComputer]
$ComputerList = @"
ComputerName
LocalHost
10.0.0.1
127.0.0.1
BetterNotBeThere
$env:COMPUTERNAME
"@ | ConvertFrom-Csv

$Offline = '__Offline__'

$Results = foreach ($CL_Item in $ComputerList)
    {
    if (Test-Connection -ComputerName $CL_Item.ComputerName -Count 1 -Quiet)
        {
        $GCIMI_Params = @{
            ClassName = 'CIM_ComputerSystem'
            ComputerName = $CL_Item.ComputerName
            }
        $TotalRAM_GB = [math]::Round((Get-CimInstance @GCIMI_Params).TotalPhysicalMemory / 1GB, 0)

        $GCI_Params = @{
            Path = "\\$($CL_Item.ComputerName)\c$\Program Files\Internet Explorer\iexplore.exe"
            }
        $IE_Version = (Get-ChildItem @GCI_Params).
            VersionInfo.
            ProductVersion
        }
        else
        {
        $TotalRAM_GB = $IE_Version = $Offline
        }

    [PSCustomObject]@{
        ComputerName = $CL_Item.ComputerName
        TotalRAM_GB = $TotalRAM_GB
        IE_Version = $IE_Version
        }
    }

# on screen
$Results

# to CSV    
$Results |
    Export-Csv -LiteralPath "$env:TEMP\FacundoCaballe_Ram_IE_Report.csv" -NoTypeInformation

onscreen output ...

ComputerName     TotalRAM_GB IE_Version      
------------     ----------- ----------      
LocalHost                  8 11.00.9600.16428
10.0.0.1         __Offline__ __Offline__     
127.0.0.1                  8 11.00.9600.16428
BetterNotBeThere __Offline__ __Offline__     
[MySysName]                8 11.00.9600.16428

CSV file content ...

"ComputerName","TotalRAM_GB","IE_Version"
"LocalHost","8","11.00.9600.16428"
"10.0.0.1","__Offline__","__Offline__"
"127.0.0.1","8","11.00.9600.16428"
"BetterNotBeThere","__Offline__","__Offline__"
"[MySysName]","8","11.00.9600.16428"
answered on Stack Overflow Mar 21, 2019 by Lee_Dailey

User contributions licensed under CC BY-SA 3.0