【发布时间】: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-CSVcmdlet 将其发送到 CSV 文件。
标签: powershell scripting rpc