首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从不响应ping的远程计算机捕获一些信息时出错

从不响应ping的远程计算机捕获一些信息时出错
EN

Stack Overflow用户
提问于 2019-03-22 00:56:37
回答 1查看 28关注 0票数 1

在几乎一无所知的情况下,我设法组装了下面显示的脚本,以获得在公司的AD中注册的团队所拥有的ram内存量。

代码语言:javascript
复制
#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的计算机上,它会抛出错误:

代码语言:javascript
复制
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”的定义?

EN

回答 1

Stack Overflow用户

发布于 2019-03-22 02:05:12

这里有一种方法可以做到这一点。grin我没有使用Invoke-Command来让事情并行运行,因为你并没有指出需要这样做。如果您确实需要更高的速度,那么可以将foreach转换为脚本块,并使用Invoke-Command和可访问系统列表进行调用。

它的作用是..。

  • 创建了一个假的计算机列表

这应该通过Import-CSV或使用类似Get-ADComputer的东西来完成。

  • 设置“不可达”消息

  • 遍历

  • 检查的系统列表,查找“它在那里吗?”

  • 如果有响应,则获取内存和IE信息

<代码>H116如果没有响应,将这两项设置为“无法访问”消息

  • 构建将导出到CSV的自定义对象整齐地

  • 将该对象发送到$Results变量

  • 完成迭代

  • 在屏幕上显示$Results集合<代码>H226<代码>H127将该集合发送到CSV文件<代码>H228<代码>F229

这是代码。

代码语言:javascript
复制
# 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

屏幕输出...

代码语言:javascript
复制
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文件内容...

代码语言:javascript
复制
"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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55285552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档