【问题标题】:Error catching some info from remote computers who dont answer to ping从不响应 ping 的远程计算机捕获某些信息时出错
【发布时间】:2019-03-21 16:56:37
【问题描述】:

在不知不觉中,我设法组装了如下所示的脚本,以获得公司AD中注册的团队所拥有的ram内存量。

#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"
}

该脚本有效,但在某些不响应 ping 的计算机上,它会引发错误:

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

如何避免此错误并简单地获得“不可 Pingable”的定义?

【问题讨论】:

  • 欢迎来到本站。自我介绍真的没必要。请记住,问题和答案将存在多年,以帮助其他有类似问题的人。更新您的个人资料是一种更好的自我介绍方式。
  • 将您的 WMI 调用放在 if ($Ping) 中,这样只有在 ping 有效时才会运行该代码。 [grin] ///// 同样,不要手动构建 CSV,而是构建一个包含所需属性的自定义对象。然后使用 Export-CSV cmdlet 将其发送到 CSV 文件。

标签: powershell scripting rpc


【解决方案1】:

这是执行此操作的一种方法。 [grin] 我没有使用 Invoke-Command 让事情并行运行,因为您没有指出需要这样做。如果您确实需要更快的速度,请将foreach 转换为脚本块并使用Invoke-Command 和可访问系统列表调用它。

它的作用......

  • 创建虚假的计算机列表
    这应该通过Import-CSVGet-ADComputer 之类的东西来完成。
  • 设置“不可达”消息
  • 遍历系统列表
  • 检查“是否存在?”
  • 如果有响应,则获取 RAM 和 IE 信息
  • 如果没有响应,则将这两项设置为“无法访问”消息
  • 构建一个自定义对象,该对象将整齐地导出为 CSV
  • 将对象发送到$Results变量
  • 完成迭代
  • 在屏幕上显示 $Results 集合
  • 将该集合发送到 CSV 文件

这是代码...

# 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

屏幕输出...

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 文件内容...

"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"

【讨论】:

  • @FacundoCaballe - 不客气!很高兴能帮上一点忙……[grin]
猜你喜欢
  • 2013-01-04
  • 1970-01-01
  • 2013-09-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多