Hi Team,

I’m new to PowerShell. I’ve created this script:

$AllServers = Get-ADComputer -Filter {OperatingSystem -Like "*Windows Server*"} |
ForEach-Object { 
   if(Test-Connection $_.dnshostname -Count 4 -BufferSize 16 -Quiet){New-Object psobject -Property @{Name_of_Server=$_.dnshostname;pingtest='Pass' }
   }}
$AllServers.Name_of_Server | foreach {
 $os = Get-WmiObject win32_OperatingSystem -ComputerName $_
 Get-WMIObject Win32_Logicaldisk -ComputerName $_ -filter "drivetype=3" | Select PSComputerName,DeviceID,
@{Name="SizeGB";Expression={$_.Size/1GB -as [int]}},
@{Name="FreeGB";Expression={[math]::Round($_.Freespace/1GB,2)}} |
Sort FreeGB | Format-Table -Autosize }

The script first fetches all Windows Servers from AD object, PINGs them, and then queries for disk allocated vs disk free for the servers to which PING succeeded.

It works fine but does reports RPC error for few servers:

Get-WMIObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:7 char:2

  • Get-WMIObject Win32_Logicaldisk -ComputerName $_ -filter "drivetype= …
  • CategoryInfo : InvalidOperation: (:slight_smile: [Get-WmiObject], COMException
  • FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

If PING is successful, then I don’t see a reason for why RPC errors are reported.

Any ideas?

3 Spice ups

Try this:-

ForEach ($COMPUTER in (Get-ADComputer -Filter {OperatingSystem -Like "*Windows Server*"} | Select -ExpandProperty Name)) 
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
 
{write-host "cannot reach $computer" -f red}
 else {
 TRY{
  $ErrorActionPreference = "Stop"
Get-WmiObject win32_OperatingSystem -ComputerName $computer
 Get-WMIObject Win32_Logicaldisk -ComputerName $computer -filter "drivetype=3" | Select PSComputerName,DeviceID,
@{Name="SizeGB";Expression={$_.Size/1GB -as [int]}},
@{Name="FreeGB";Expression={[math]::Round($_.Freespace/1GB,2)}} |
Sort FreeGB | Format-Table -Autosize
}

Catch{
   Write-Host "$($computer) " -BackgroundColor red -NoNewline
   Write-Warning $Error[0] 
    }
}
} 
 
 

RPC errors will come is WMI is not working ps cannot access the server

Legend mate. Just one small output error.

It gives below with each time there is an error:

Capture.PNG

Or RPC will come if computer object exists but the server really doesn’t :slight_smile:

why you have this? remove this line if not needed.

Get-WmiObject win32_OperatingSystem -ComputerName $computer

Or RPC will come if computer object exists but the server really doesn’t :slight_smile:

can you screenhot?

Hey mate. Sorry got pulled off on a project. Will get back on this in a day or two. Thanks.

You checked the firewall on the offending servers right? If they’re pinging but failing to connect via rpc from your work station it may be that someone has done a manual configuration of the firewall… Just a thought.

TCP 135 & random ports are used by RPC service. You can test it by using some TCP listener tools to find it out. But if the servers from the same IP range responds to your query then there can be WMI issue.

Thanks!

Make sure your text file of computer names doesn’t have a blank line at the end. Just about all of us pull that trick at some point.