Hi Team,<\/p>\n
I’m new to PowerShell. I’ve created this script:<\/p>\n
$AllServers = Get-ADComputer -Filter {OperatingSystem -Like \"*Windows Server*\"} |\nForEach-Object { \n if(Test-Connection $_.dnshostname -Count 4 -BufferSize 16 -Quiet){New-Object psobject -Property @{Name_of_Server=$_.dnshostname;pingtest='Pass' }\n }}\n$AllServers.Name_of_Server | foreach {\n $os = Get-WmiObject win32_OperatingSystem -ComputerName $_\n Get-WMIObject Win32_Logicaldisk -ComputerName $_ -filter \"drivetype=3\" | Select PSComputerName,DeviceID,\n@{Name=\"SizeGB\";Expression={$_.Size/1GB -as [int]}},\n@{Name=\"FreeGB\";Expression={[math]::Round($_.Freespace/1GB,2)}} |\nSort FreeGB | Format-Table -Autosize }\n<\/code><\/pre>\n
Advertisement
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.<\/p>\n
It works fine but does reports RPC error for few servers:<\/p>\n
Get-WMIObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
\nAt line:7 char:2<\/p>\n
\n- Get-WMIObject Win32_Logicaldisk -ComputerName $_ -filter \"drivetype= …<\/li>\n
- \n
<\/code><\/pre>\n<\/li>\n- CategoryInfo : InvalidOperation: (
[Get-WmiObject], COMException<\/li>\n - FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand<\/li>\n<\/ul>\n
If PING is successful, then I don’t see a reason for why RPC errors are reported.<\/p>\n
Any ideas?<\/p>","upvoteCount":3,"answerCount":10,"datePublished":"2017-12-14T23:34:25.000Z","author":{"@type":"Person","name":"nitinpandey2","url":"https://community.spiceworks.com/u/nitinpandey2"},"acceptedAnswer":{"@type":"Answer","text":"
Try this:-<\/p>\n
ForEach ($COMPUTER in (Get-ADComputer -Filter {OperatingSystem -Like \"*Windows Server*\"} | Select -ExpandProperty Name)) \n{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n \n{write-host \"cannot reach $computer\" -f red}\n else {\n TRY{\n $ErrorActionPreference = \"Stop\"\nGet-WmiObject win32_OperatingSystem -ComputerName $computer\n Get-WMIObject Win32_Logicaldisk -ComputerName $computer -filter \"drivetype=3\" | Select PSComputerName,DeviceID,\n@{Name=\"SizeGB\";Expression={$_.Size/1GB -as [int]}},\n@{Name=\"FreeGB\";Expression={[math]::Round($_.Freespace/1GB,2)}} |\nSort FreeGB | Format-Table -Autosize\n}\n\nCatch{\n Write-Host \"$($computer) \" -BackgroundColor red -NoNewline\n Write-Warning $Error[0] \n }\n}\n} \n \n \n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-12-14T23:56:24.000Z","url":"https://community.spiceworks.com/t/improvement-with-a-powershell-script/624359/2","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi Team,<\/p>\n
I’m new to PowerShell. I’ve created this script:<\/p>\n
$AllServers = Get-ADComputer -Filter {OperatingSystem -Like \"*Windows Server*\"} |\nForEach-Object { \n if(Test-Connection $_.dnshostname -Count 4 -BufferSize 16 -Quiet){New-Object psobject -Property @{Name_of_Server=$_.dnshostname;pingtest='Pass' }\n }}\n$AllServers.Name_of_Server | foreach {\n $os = Get-WmiObject win32_OperatingSystem -ComputerName $_\n Get-WMIObject Win32_Logicaldisk -ComputerName $_ -filter \"drivetype=3\" | Select PSComputerName,DeviceID,\n@{Name=\"SizeGB\";Expression={$_.Size/1GB -as [int]}},\n@{Name=\"FreeGB\";Expression={[math]::Round($_.Freespace/1GB,2)}} |\nSort FreeGB | Format-Table -Autosize }\n<\/code><\/pre>\nThe 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.<\/p>\n
It works fine but does reports RPC error for few servers:<\/p>\n
Get-WMIObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
\nAt line:7 char:2<\/p>\n
\n- Get-WMIObject Win32_Logicaldisk -ComputerName $_ -filter \"drivetype= …<\/li>\n
- \n
<\/code><\/pre>\n<\/li>\n- CategoryInfo : InvalidOperation: (
[Get-WmiObject], COMException<\/li>\n - FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand<\/li>\n<\/ul>\n
If PING is successful, then I don’t see a reason for why RPC errors are reported.<\/p>\n
Any ideas?<\/p>","upvoteCount":3,"datePublished":"2017-12-14T23:34:25.000Z","url":"https://community.spiceworks.com/t/improvement-with-a-powershell-script/624359/1","author":{"@type":"Person","name":"nitinpandey2","url":"https://community.spiceworks.com/u/nitinpandey2"}},{"@type":"Answer","text":"
RPC errors will come is WMI is not working ps cannot access the server<\/p>","upvoteCount":0,"datePublished":"2017-12-15T00:00:40.000Z","url":"https://community.spiceworks.com/t/improvement-with-a-powershell-script/624359/3","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"
Legend mate. Just one small output error.<\/p>\n
It gives below with each time there is an error:<\/p>\n
<\/p>","upvoteCount":0,"datePublished":"2017-12-15T00:12:19.000Z","url":"https://community.spiceworks.com/t/improvement-with-a-powershell-script/624359/4","author":{"@type":"Person","name":"nitinpandey2","url":"https://community.spiceworks.com/u/nitinpandey2"}},{"@type":"Answer","text":"
Or RPC will come if computer object exists but the server really doesn’t
<\/p>","upvoteCount":0,"datePublished":"2017-12-15T00:13:50.000Z","url":"https://community.spiceworks.com/t/improvement-with-a-powershell-script/624359/5","author":{"@type":"Person","name":"nitinpandey2","url":"https://community.spiceworks.com/u/nitinpandey2"}},{"@type":"Answer","text":"