Link to home
Start Free TrialLog in
Avatar of Hani_SA
Hani_SA

asked on

PowerShell Script for Disk usage

Hey Guys,

I'm looking for a powershell script that returns the disk usage on a File Server mounted partitions.
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Hi,
a good starting point for such scripts is normally the Powershell script gallery.
Maybe this one fits your needs:
https://gallery.technet.microsoft.com/scriptcenter/Use-Powershell-to-Gather-b5c746d0

HTH
Rainer
You need to be specific. "disk usage" is very broad.
Avatar of Hani_SA
Hani_SA

ASKER

I meant a ps script that returns as output the current disk space usage on a specific partition which I intent to use as an alert for threshold monitoring.
Avatar of Hani_SA

ASKER

Hey Rainer,

I tried the script but couldn't get any result.
PS > $cred = Get-Credential
PS > Get-RemoteDiskInformation -ComputerName Test1 -Credential $cred

I'm trying locally on my laptop
by using
powershell -ExecutionPolicy ByPass -File .\Get-RemoteDiskInformation.ps1 -ComputerName MyLaptop $cred

Also is there a way to get rid of the unauthorized Access message
.\Get-RemoteDiskInformation.ps1 cannot be loaded because running scripts is disabled on this system.
and integrate the permission within the script

Thanks
You should execute set-executionPolicy RemoteSigned once. Then you should be able to execute the script without prompt, as is with ByPass on PowerShell invokation as shown above.

You are not getting results because the script just defines the function, but does not execute it. And the parameters you provide might be consumed by PowerShell, not the command itself, as you do it. You need to import the function by dot-sourcing the script, and then call the function. That is:
PowerShell -ExecutionPolicy Bypass -Command { . .\Get-RemoteDiskInformation.ps1; Get-RemoteDiskInformation -ComputerName MyLaptop }
# or, if you need credentials:
PowerShell -ExecutionPolicy Bypass -Command { . .\Get-RemoteDiskInformation.ps1; Get-RemoteDiskInformation -ComputerName MyLaptop -credential (Get-Credential MyUser) }

Open in new window

Avatar of Hani_SA

ASKER

Thanks Qlemo,

It worked as suggested so what is the purpose of set-executionPolicy RemoteSigned  if I need to use PowerShell -ExecutionPolicy Bypass every time.
You see my intention is use this script as input from a monitoring tool that will call on this script to run so I can only only provide the script name in the tool without arguments, do you think there is a way to integrate this bypass command in the script itself.
In addition the script provides an extensive amount of info, is there a way to fine-tuned in a way to get only:
Drive
DiskSize
UsedSpace
PercentFree
PercentUsed

where Drive is to be defined in the script as a static value since I'm only interested in monitoring 1 critical partition on my file server let say for example v:\

Thanks for your help
You don't need to provide the execution policy when calling PowerShell if you set it correctly with set-executionpolicy, only to override it.
Avatar of Hani_SA

ASKER

Does this setting be saved after logoff/logon.
Can you explain the intention of this command:
Command { . .\Get-RemoteDiskInformation.ps1;
you mentioned that it's related to an import !
Also I found this script :

Get-WmiObject -ClassName Win32_Volume -Filter "DriveLetter = 'C:'"-ComputerName localhost   |

select PSComputerName,Caption,Label,
@{N='Capacity_GB'; E={[math]::Round(($_.capacity / 1GB), 2)}},

@{N='FreeSpace_GB'; E={[math]::Round(($_.Freespace / 1GB), 2)}},

@{N='PercentUsed'; E={[math]::Round(((($_.capacity - $_.FreeSpace) / $_.capacity) * 100), 2) }},

@{N='PercentFree'; E={[math]::Round((($_.FreeSpace / $_.capacity) * 100), 2) }}

is there a way to monitor a specified path under c:\ instead of the entire volume i.e C:\TestFolder in order to get the above values for this specified partition
The default scope of Set-ExecutionPolicy is LocalMachine, so yes, it is persistent.

The need for "importing" (it is called dot-sourcing in PS) is kind of complex to explain. A script builds an own scope, and everything done in the script does not effect the calling environment. This includes function and variable definitions. Only the objects generated as output (like the disk info here) are returned and stay valid after the script has executed.
However, if you run . .\scriptname.ps1 all definitions of the script are "imported" into the current shell environment, and can hene be used. This includes the function defined in the script.

The script you found can be used if have drive letters. Mount points are not necessarily drives, but can also be paths. So it depends on what you really want to know. I guess in your case the new script will work fine, and it doesn't matter which one you'll use.
But you can take the original approach. Just put together a "master script" doing the work:
 . .\Get-RemoteDiskInformation.ps1
Get-RemoteDiskInformation -ComputerName MyLaptop |
  ? { $_.Drive -eq 'V:' } |
  select Drive, DiskSize, UsedSpace, PercentFree, PercentUsed

Open in new window


Regarding your new request - you told us you just want to monitor a single, specific volume (V:). Now you take a twist to monitor a path. The answer is "no, you can't do that" with that WMI call, because getting folder stats is something completely different than to get volume or drive stats. Aside from that, the only useful info from above you can restrict to a path is "PercentUsed", anything else remains a volume number.
Avatar of Hani_SA

ASKER

Thanks Qlemo for your valuable info.

It works perfectly well except for the import part; I received the below error so I had to use PowerShell -ExecutionPolicy Bypass -Command once again:
Get-RemoteDiskInformation.ps1 cannot be loaded. The file is not digitally signed.

Is there a way to bypass that ?

Also regarding the path monitor which I requested at a later stage, I found a class to do that, do you think it achieve that purpose that said let's say I want to monitor a specific mount point path on my file server i.e v:\shared\business and not the entire v:\ mount point

'{0:0.00}' -f ((Get-Childitem -path v:\shared\business -recurse | Measure-Object length -sum).Sum/1GB)

Do you agree in case I integrate this ps in my monitoring tool, will it serve the intent of monitoring a threshold let's say below 10 % for the specified path

Thanks
That latest script fragment should work to get the used space.
But be aware that it is a costly operation. Get-ChildItem has to retrieve the complete FileObject or FolderObject with many of its attributes, and doing that remote might cause some traffic. If you can do so, perform the action locally, where the folder you want to monitor is located at.

Having said that, what's the 10% based on? Are you setting an arbitrary limit for used space, say 100 MB, and if the real used space is above 90 MB (less than 10% "free") trigger an alert?
Avatar of Hani_SA

ASKER

I'm planning to run this command remotely, do you have any alternative ?
My end result is to have an event on my monitoring tool triggered once the disk utilization threshold on this particular path reaches 80% usage, how to achieve that ?
80% of which size?
Avatar of Hani_SA

ASKER

let say this is the path I wish to monitor:
v:\shared\business
it has a 100 GB size
Can you provide a script to return the size of this path and alerts me whenever usage is above 80% (80 GB)

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hani_SA

ASKER

Dear Qlemo,

Thank you very much for your help, I fine-tuned the script and now it looks like this
$limit = 5 # GB
$usedGB = '{0:0.00}' -f ((Get-Childitem -path C:\Test  -recurse | Measure-Object length -sum | select -Expand Sum)/1GB)
New-Object PsObject -Property @{
  used = $usedGB
  free  = $(if ($limit -gt $usedGB) {$limit - $usedGB} else {0})
  freePercent = ($limit - $usedGB) / $limit * 100.0

}

Open in new window


If you can please explain this New-Object PsObject -Property @{
However I run into another issue since the server on which I intend to run the command has powershell 1.0 and another issue is that the shared disk I am monitoring has over 400 GB space which could cause a performance issue since it will take a lot of time to traverse the path not to mention the calculation method which could not be accurate.
What do you propose to overcome those issues ?
The New-Object line creates a new custom object with the properties as supplied. In PS, everything is an object.
With PS1, you had to use Add-Member (or more complicated methods) to add properties to an object.

It is very unlikely you have PS1 installed, even if the path of PowerShell.exe suggests so. There is no reason to have PS1, it can always be upgraded to PS2.

I don't know of a better way to calculate folder specific stats. All I can say is that I would run a job on the server machine itself performing the check.
Have a look at OvaFlow.com also.

Instead of scripting all this it does all this under the bonnet. You can monitor up to 100 servers and takes about 15 minutes to install. All totally free as well.