3

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.

Ashar
  • 2,942
  • 10
  • 58
  • 122
  • VonC's answer solves your problem. Your question cannot be closed as a duplicate while the bounty period hasn't ended yet, but the following posts are duplicates: https://stackoverflow.com/q/71603970/45375 https://stackoverflow.com/q/12393999/45375 https://stackoverflow.com/q/72168232/45375 – mklement0 Aug 02 '23 at 01:58

1 Answers1

4

You are using $myconnuser = '$connuser' in your script, which... will literally assign the string '$connuser' to the variable $myconnuser because single quotes in PowerShell do not expand variables.

You should use $myconnuser = $connuser instead.
Same idea for mypassword

The script would then be:

param (   [string]$passdrives,
          [string]$connuser,
          [string]$connpass,
          [int]$minFreeSpaceGB
)

$myconnuser = $connuser
$mypassword = ConvertTo-SecureString -AsPlainText -Force -String $connpass

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $myconnuser, $mypassword

$disks = Get-WmiObject -ComputerName remotehost1 -Credential $credential -Class Win32_LogicalDisk -Filter "DriveType = 3"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250