从无法回答ping的远程计算机捕获某些信息时出错

时间:2019-03-21 16:56:37

标签: powershell scripting rpc

我几乎不了解,就设法整理了以下所示的脚本,以获得在公司广告中注册的团队所拥有的内存。

#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

如何避免出现此错误并仅获得“不可触发”的定义?

1 个答案:

答案 0 :(得分:0)

这是执行此操作的一种方法。 [咧嘴]我没有使用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"