从没有回答ping的远程计算机中捕获一些信息时出错

问题描述 投票:1回答:1

在知之甚少的情况下,我设法组装了下面显示的脚本,以获得团队在公司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

我怎么能避免这个错误,只是得到一个“不可Ping”的定义?

powershell scripting rpc
1个回答
0
投票

这是一种方法。 [grin]我没有使用Invoke-Command让事情并行运行,因为你没有表明需要这样做。如果你需要更快的速度,那么将foreach转换成一个脚本块并用Invoke-Command和可访问系统列表调用它。

它能做什么 ...

  • 创建一个虚假的计算机列表 这应该通过Import-CSV或类似Get-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"
© www.soinside.com 2019 - 2024. All rights reserved.