Ashar
Ashar

Reputation: 3065

unable to authenticate credentials on remote windows to get drive details

Below is the PowerShell check_disk_space.ps1 is used to get remote Windows server drives information but instead I get an error:

param (   [string]$passdrives,
          [string]$connuser,
          [string]$connpass,
          [int]$minFreeSpaceGB
              )
    $myconnuser = '$connuser'
    $mypassword = '$connpass' | ConvertTo-SecureString -AsPlainText -Force
    
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $myconnuser, $mypassword
    
    $disks = Get-WmiObject -ComputerName remotehost1 -Credential $credential -Class Win32_LogicalDisk -Filter "DriveType = 3";
 

Output:

powershell.exe -File .\check_disk_space.ps1  -passdrives "C" -minFreeSpaceGB "30" -connuser "remuser" -connpass "passxxx"

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

At D:\Git-Runners\s10\_work\poc\check_disk_space.ps1:38 char:11

+ ...    $disks = Get-WmiObject -ComputerName remotehost1 -Credential $credenti ...

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

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

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

The command errors are as above.

However, if I remove -Credential $credential from Get-WmiObject and run command prompt as a different user as remuser the same check_disk_space.ps1 runs successfully!!

I tried the below but it too did not help:

Enable Remote WMI Access: On the remote host, you may need to enable and configure WMI access for remote users. To do this, follow these steps:

Open "Control Panel" -> "Administrative Tools" -> "Computer Management."
In the left pane, expand "Services and Applications" and select "WMI Control."
Right-click on "WMI Control" and select "Properties."
Go to the "Security" tab and set the necessary permissions for the user specified in $connuser.

I'm more inclined toward a solution with an updated powershell code rather than other solutions.

I would like to understand how I can successfully execute the PowerShell script to get drive details by passing -Credential $credential to Get-WmiObject.

Upvotes: 3

Views: 133

Answers (0)

Related Questions