Share via


Script to Monitor free space in UNC Path / Network drive

Question

Friday, June 3, 2016 9:03 PM

Hi All,

Good Day,

I was trying to achieve the below:

To monitor a network share and its free space so i managed to write a script as below where it maps the drive as a letter queries the free space added few variables to convert the space in & and added to remove decimal and all worked like a charm.

For the above output i wanted to write a event log to capture the free space. Issue here is that there are 2 conditions on the script i mentioned that if the free space is below 20 % write a warning event and if it is below 10% write a critical event.

Issue is that when the space is below 10 both the logs warning & critical are written. Ex if free space is below 9 both critical & warning is written. Ideally script is doing its job correctly.

But i just want only 1 event for below 10 % critical and one warning for below 20 but above 10.

I am not able to figure the exact "if criteria" to ignore the 20 % if it is below 10 % and want only 1 event for below 10 and not 2

Can anyone tell me what am i missing here ?

$share="\10.154.139.146\My_Share"
$nwobj=new-object -comobject WScript.Network
$status=$nwobj.mapnetworkdrive("Z:",$share)
$drive=get-psdrive Z
$gb=(1024 * 1024 * 1024)
$free=($drive.free) /$gb
$used=($drive.used) /$gb
$total=($free+$used)
$free%=($free/$total*100)
$free=([math]::Round($free))
Write-Output "Share $share has free space of $free%"
$status=$nwobj.removenetworkdrive("Z:")

If ($free -lt 10) {Write-EventLog -LogName Application -Source "Network Share Monitoring" -EntryType Error 

-EventID 210 -Message "The Share $Share has disk space below 20 % and Current value is $free%"}

If ($free -lt 20) {Write-EventLog -LogName Application -Source "Network Share Monitoring" -EntryType 

Warning -EventID 210 -Message "The Share $Share has disk space below 20 % and Current value is $free%"}

Event viewer:

Gautam.75801

All replies (6)

Wednesday, June 8, 2016 12:01 PM ✅Answered

You were right for a point that it will not work as it dint work on few machines.

I got it altered as below and now it is working as expected:

Ideally we have few NAS which needed its free space to be monitored remotely using monitoring tool, As the tool did not have the capability we had to build a script and then make it log an event so our monitoring tool can pick the event.

Note: This script will not work if you add \ in the last 

Ex: \172.22.2.2\c$ will work
\172.22.2.2\c$\ - Will not work

$share="\YOURSHAREPATH"
$nwobj=new-object -comobject WScript.Network
$status=$nwobj.mapnetworkdrive("O:",$share)
$drive=get-psdrive O
$free=($drive.free)
$used=($drive.used)
$total=($free+$used)
$freeinPercentage=($free/$total*100)
$freewithoutdecimal=([math]::Round($freeinPercentage))
Write-Output "Share $share has free space of $freewithoutdecimal %"
$status=$nwobj.removenetworkdrive("O:")

$Warning = "20"
$Critical = "10"

If ($freewithoutdecimal -le "$Critical") {Write-EventLog -LogName Application -Source "Network Share Monitoring" -EntryType Error -EventID 210 -Message "The Share $Share has disk space below 10 % and Current value is $freewithoutdecimal %"}

elseIf ($freewithoutdecimal -le "$Warning") {Write-EventLog -LogName Application -Source "Network Share Monitoring" -EntryType warning -EventID 210 -Message "The Share $Share has disk space below 20 % and Current value is $freewithoutdecimal %"} 

Gautam.75801


Friday, June 3, 2016 11:05 PM | 1 vote

Network shares do not have free space.  You have to query the underlying physical storage.  This can be NAS or other large storage and can be DFSR storage.

The numbers you are seeing are mostly bogus.

This 1024 * 1025 is really this -> 1Gb.

($drive.free) /1Gb

I highly recommend doing a tutorial on the Windows file system and PowerShell. It will help you understand how to do this.

You methods do not work because you are not accessing a physical drive.  In any case this is how we would do it in PowerShell.

$drive = New-PsDrive Z -PSProvider FileSystem -Root \10.154.139.146\My_Share
$drive.Free/1Gb

Of course "Free" is always null for a share.

\(ツ)_/


Friday, June 3, 2016 11:24 PM

Hi Jrv,

Thanks for your reply.

Agree as it still will reflect the free space of the physical storage of the drive its self.

I see the numbers shown in the event log are correct as i intentionally filled the drive to test and once i ran them it reported them correctly as per the value in disk management.

But seems you shortened a 10 line script to 1 but i don't understand few variables on writing the event log to the eventvwr as i have mentioned the below  

If ($free -lt 10) {Write-EventLog -LogName Application -Source "Network Share Monitoring" -EntryType Error -EventID 210 -Message "The Share $Share has disk space below 20 % and Current value is $free%"} 
If ($free -lt 20) {Write-EventLog -LogName Application -Source "Network Share Monitoring" -EntryType Warning -EventID 210 -Message "The Share $Share has disk space below 20 % and Current value is $free%"}

I was not able to find any way to use only 1 for 10 % free.

If disk space is below 20 % warning occurs correctly & if it is below 10 % both occur as below 10 % is also below 20 % so it is logging both.

Is there any IF Criteria i can again refine to spoof the only 1 event critical for below 10 % instead of 2 events ?

As i will be capturing the Events via a monitoring tool with the same Event id's by differentiating them via  Error or warning.

Gautam.75801


Friday, June 3, 2016 11:55 PM

Ok so i got this working as expected by altering the event content so finally the below works:

$share="\10.154.139.146\My_Share"
$nwobj=new-object -comobject WScript.Network
$status=$nwobj.mapnetworkdrive("Z:",$share)
$drive=get-psdrive Z
$gb=(1024 * 1024 * 1024)
$free=($drive.free) /$gb
$used=($drive.used) /$gb
$total=($free+$used)
$free%=($free/$total*100)
$free=([math]::Round($free))
Write-Output "Share $share has free space of $free%"
$status=$nwobj.removenetworkdrive("Z:")

$Warning = "20"
$Critical = "10"

If ($free -le "$Critical") {Write-EventLog -LogName Application -Source "Network Share Monitoring" -

EntryType Error -EventID 210 -Message "The Share $Share has disk space below 10 % and Current value is 

$free%"}

elseIf ($free -le "$Warning") {Write-EventLog -LogName Application -Source "Network Share Monitoring" -

EntryType warning -EventID 210 -Message "The Share $Share has disk space below 20 % and Current value is 

$free%"}

Gautam.75801


Saturday, June 4, 2016 12:02 AM

I don't see what this has to do with your original question.

There is no valid "free space" on a remote share.  You will eventually see what I am saying.  Until then,,,good luck.

\(ツ)_/


Thursday, June 9, 2016 1:15 AM

Hi,

Thank you for taking the time to update the result. Your sharing might be helpful for other people who has the similar problem.

Best Regards,
Eve Wang

Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact [email protected].