3

I'm an admin on two servers (Win 2008 R2). I have a scheduled task that copies large files from a share on one server to another. Before I do the copy, I check that the destination disk on the other server has enough free space. I've been running these tasks under my own account and want to change to a system account with lower privileges than me.

I've been using the following recipe to get the free space ratio:

PowerShell.exe Get-WmiObject Win32_LogicalDisk -ComputerName <REMOTESERVER> -Filter "DeviceID='C:'" | Foreach-Object {$_.FreeSpace / $_.Size}

It works on my account, but gives me 0x80070005 (E_ACCESS DENIED) from the non-admin account.

Should I even be using WMI for this or is there something simpler?

1
  • You want to use a user account with less privileges than you? It sounds like your user account has too many privileges. You should be using the principle of least privilege with all of your user accounts.
    – joeqwerty
    Commented Jul 21, 2015 at 20:23

3 Answers 3

1

You can use WMI to get the free disk space as a non-administrative user, but you first of all need to change the WMI permissions on the remote server.

From wmimgmt.msc on the remote server, and then right-click on "WMI Control (Local)", and select the Security tab, then highlight CIMv2 and click the "Security" button. From here give the user you want to be able to run the script as "Remote Enable" permission and the script should be able to get the remote WMI information.

2
  • Tried that, and it wasn't enough. I think I need to do something with DCOM permissions as well?
    – giltay
    Commented Jul 23, 2015 at 15:03
  • Ah, sorry just double checked and I'd also added my test account to the "Distributed COM Users" group on the remote server..
    – Mike1980
    Commented Jul 23, 2015 at 18:32
2

You can get the size by mapping the drive to a letter and querying it with the Get-PSDrive cmdlet. Works without admin access on the server or even write access to the share.

net use T: \\server\share
$freebytes=(Get-PSDrive T).Free
net use T: /delete
0

There is a non WMI based EXE called Freedisk that works without admin privileges.
https://technet.microsoft.com/en-us/library/cc731162.aspx
It returns a 0 if there is enough space and a 1 if there is not enough space.

PS C:\temp> freedisk \\server1\share1 9mb
OK
PS C:\temp> $lastExitCode
0
PS C:\temp> freedisk \\server1\share1 99999999999999999mb
To small!!
PS C:\temp> $lastExitCode
1
1
  • Thanks, although it looks like this isn't available on Windows Server past 2003. I'll see if there's a 2003 server still around.
    – giltay
    Commented Jul 23, 2015 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .