Access denied

I am getting below error while running script to get uptime of servers, for few servers it is working fine where as for others I am getting blank box instead of server up time .I am having same access on all the servers but still only for few I am facing this issue

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

At C:\Scripts\New folder\script.ps1:48 char:22

+   $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerNa ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException

    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values.

Parameter name: dmtfDate"

June 23rd, 2015 9:34am


Did you do proper error handling (e.g. error handling) to get more specific information?

Are there any significant differences between those servers that deny access and the others, maybe, the former are 64-bit, whereas the latter are 32-bit?

wizend

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 10:15am

I am new to powershell , I got this script from someone, All the severs are 2012 64 bit.

please advise what changes I need to need to do in script.

June 23rd, 2015 10:46am

You must be an admin on the remote system and you must have the firewall ports opened and WMI must allow remote access.

There is no scripting trick here.  You are being refused access to WMI on the remote system.

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 11:14am

Hi SAM19919,

This error indicate a permission issue as Jrv mentioned.

I recommend you can list all the server names in problem, then troubleshoot the error:

$computer="server1","server2","server3"
foreach($c in $computer){
try{
gwmi win32_process -ComputerName $c}
catch{
write-output "server $c encounter error $($_.exception.message)"
}
}

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

June 24th, 2015 4:15am

HI,

thanks for the info, it was getting blocked on firewall,

Now I got one more error for 4 server only

Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values.
Parameter name: dmtfDate"
At C:\Scripts\uptime\PingStatusWithUptime.ps1:36 char:2
+     $Uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateT ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Please advise on this one also.

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2015 11:06am

Do the conversion in two steps and guard for null values.

June 25th, 2015 12:24pm

but it is working fine for all other servers with same format , this error is for only 4 specific server and all these servers are 2008 R2 and others are 2012, is this is the reason?
Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 5:21am

There is no way to know without your script.  The error message is also incomplete.

June 26th, 2015 5:43am

$DATEnTIME = Get-Date
Function GetStatusCode
{
 Param([int] $StatusCode) 
 switch($StatusCode)
 {
  0   {"Success"}
  11001   {"Buffer Too Small"}
  11002   {"Destination Net Unreachable"}
  11003   {"Destination Host Unreachable"}
  11004   {"Destination Protocol Unreachable"}
  11005   {"Destination Port Unreachable"}
  11006   {"No Resources"}
  11007   {"Bad Option"}
  11008   {"Hardware Error"}
  11009   {"Packet Too Big"}
  11010   {"Request Timed Out"}
  11011   {"Bad Request"}
  11012   {"Bad Route"}
  11013   {"TimeToLive Expired Transit"}
  11014   {"TimeToLive Expired Reassembly"}
  11015   {"Parameter Problem"}
  11016   {"Source Quench"}
  11017   {"Option Too Big"}
  11018   {"Bad Destination"}
  11032   {"Negotiating IPSEC"}
  11050   {"General Failure"}
  default {"Failed"}
 }
}
Function GetUpTime
{
 param([string] $LastBootTime)
 $Uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBootTime)
 "Days: $($Uptime.Days); Hours: $($Uptime.Hours); Minutes: $($Uptime.Minutes); Seconds: $($Uptime.Seconds)"
}
#Change value of the following parameter as needed
$OutputFile = "C:\Scripts\New folder\Output.htm"
$ServerList = Get-Content "C:\Scripts\New folder\ServerList.txt"
$Result = @()
Foreach($ServerName in $ServerList)
{
 $pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$ServerName'"

 $Uptime = $null
 if($pingStatus.StatusCode -eq 0)
 {
  $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerName -ErrorAction SilentlyContinue
  $Uptime = GetUptime( $OperatingSystem.LastBootUpTime )
 }

    $Result += New-Object PSObject -Property @{
     ServerName = $ServerName
  IPV4Address = $pingStatus.IPV4Address
  Status = GetStatusCode( $pingStatus.StatusCode )
  Uptime = $Uptime
 }
}
if($Result -ne $null)
{
 $HTML = '<style type="text/css">
 #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
 #Header td, #Header th {font-size:14px;border:1px solid #417fa4;padding:3px 7px 2px 7px;}
 #Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#417fa4;color:#fff;}
 #Header tr.alt td {color:#000;background-color:#417fa4;}
 </Style>
 <br><center><h1><font color="#417fa4">Sava</font><font color="#BABABA">SeniorCare</font></center></h1></br>
 <h2><center>Network Team Server Quick Check</center></h2>
  <head id="Head1"><title>Server Uptime at a Glance</title>'

    $HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
  <TR>
   <TH><B>Server Name</B></TH>
   <TH><B>IP Address</B></TD>
   <TH><B>Status</B></TH>
   <TH><B>Uptime</B></TH>
  </TR>"
    Foreach($Entry in $Result)
    {
        if($Entry.Status -ne "Success")
  {
   $HTML += "<TR bgColor=Red>"
  }
  else
  {
   $HTML += "<TR>"
  }
  $HTML += "
      <TD>$($Entry.ServerName)</TD>
      <TD>$($Entry.IPV4Address)</TD>
      <TD>$($Entry.Status)</TD>
      <TD>$($Entry.Uptime)</TD>
     </TR>"
    }
    $HTML += "</Table></BODY></HTML> <BR>This file was created automatically on $DATEnTIME </BR>"
 $HTML | Out-File $OutputFile
}

    
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2015 4:00am

This is the script which is not working for 4 specific server
July 2nd, 2015 4:01am

Remove this: "-ErrorAction SilentlyContinue" to get the real error.
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2015 9:52am

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

At C:\Scripts\New folder\script.ps1:48 char:22

+   $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerNa ...

+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException

    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values.

Parameter name: dmtfDate"

At C:\Scripts\New folder\script.ps1:34 char:2

+  $Uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateT ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentOutOfRangeException

July 3rd, 2015 4:30am

That is the real error that is your issue now.  Perhaps the firewall is not configured for WMI.

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 4:32am

same firewall is in use for other servers also , even I have checked and traffic is not getting blocked at firewall please advide
  • Edited by SAM19919 16 hours 51 minutes ago
July 3rd, 2015 10:19am

Every server has it's own firewall and separate settings.  The issue has nothing to do with scripting.  It is a break/fix/configuration issue which we cannot fix for you.

"Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scripts\New folder\script.ps1:48 char:22"

You cannot access the remote server.  Look at logs on remote server for more details.

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 10:24am

same firewall is in use for other servers also , even I have checked and traffic is not getting blocked at firewall please advide
  • Edited by SAM19919 Friday, July 03, 2015 2:18 PM
July 3rd, 2015 2:18pm

same firewall is in use for other servers also , even I have checked and traffic is not getting blocked at firewall please advide
  • Edited by SAM19919 Friday, July 03, 2015 2:18 PM
Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 2:18pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics