locked
Script to Monitor free space in UNC Path / Network drive RRS feed

  • Question

  • The below script I ran & getting the successful output but it is taking a static value of ($share). I want to make script where I can flow a value/parameter of (Share) as dynamic from storage & get the out similarly I am getting as my given script.

    $share=Get-Content "\\servername\sharedfolder name"
    $nwobj=new-object -comobject WScript.Network
    $status=$nwobj.mapnetworkdrive("O:",$share)
    $drive=get-psdrive O
    $free=($drive.free)
    $used=($drive.used)
    $usedP =($used/$total * 100)
    $total=($free+$used)
    $freeP=($free/$total * 100)
    $Threshold=([math]::Round($freeP))
    Write-Output "Share $share has free space of $Threshold %"

    $status=$nwobj.removenetworkdrive("O:")

    $Warning = 20
    $Critical = 15

    If ($Threshold -le "$Critical") {

    Write-Output "The Share $share has disk space below 15 %"

    Write-Host "Total Disk Space: $total GB"
    Write-Host "totalGB: $total"

    Write-Host "Message.freeGB: Free Disk Space: $free GB"
    Write-Host "freeGB: $free"

    Write-Host "Used Disk Space: $used GB"
    Write-Host "usedGB: $used"

    Write-Host "Free Disk Space: $freeP %"
    Write-Host "freeP: $freeP"

    #Write-Host "Message.usedP: Used Disk Space: $usedP %"
    Write-Host "usedP: $usedP"
    }
    elseIf ($Threshold -le "$Warning")
    {
    Write-Output "$share has disk space below 20 %"
     }

    Wednesday, October 21, 2020 6:00 AM

All replies

  • Is there any way to pass the parameter?
    Friday, October 23, 2020 7:10 AM
  • Please fix your first post as requested.

    This forum is not for having others rewrite scripts you have found.


    \_(ツ)_/

    Friday, October 23, 2020 8:19 AM
  • I am asking this was helped to me & I am getting the output over the same script. Now, would like to know that How to pass a multiple UNC's path dynamically through the script to get the similar output as per the above script I am getting.
    • Edited by Pint2 Monday, October 26, 2020 8:02 AM
    Monday, October 26, 2020 8:01 AM
  • I am asking this was helped to me & I am getting the output over the same script. Now, would like to know that How to pass a multiple UNC's path dynamically through the script to get the similar output as per the above script I am getting.

    Please fix your first post as requested.

    This forum is not for having others rewrite scripts you have found.



    \_(ツ)_/

    Monday, October 26, 2020 12:35 PM
  • get-content C:\fileShares.txt | foreach {
       $share = $_
       #...
    }

    As i got, you need something like this, where fileShares.txt would contain pathes to your shares each on a new line.

    also, regarding to your necropost, you maybe interested in how to write functions and use param block


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Monday, October 26, 2020 10:19 PM
    Monday, October 26, 2020 10:15 PM
  • Thanks!

    However, this given command line pipe with foreach is giving me output but after the end of text file when it didn't get the share name gives an error:

    The network name cannot be found.
    At line:5 char:1
    + $status=$nwobj.mapnetworkdrive("O:",$share)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], FileNotFoundException
        + FullyQualifiedErrorId : System.IO.FileNotFoundException
     
    get-psdrive : Cannot find drive. A drive with the name 'O' does not exist.
    At line:6 char:8
    + $drive=get-psdrive O
    +        ~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (O:String) [Get-PSDrive], DriveNotFoundException
        + FullyQualifiedErrorId : GetLocationNoMatchingDrive,Microsoft.PowerShell.Commands.GetPSDriveCommand

    Thursday, October 29, 2020 4:09 AM
  • its because u have empty lines in your file

    for covering this case you can add additional minor change for skipping empty lines

    get-content C:\fileShares.txt | where {!($_ -match '^\s*$')}  | foreach {
       $share = $_
       #...
    }


    The opinion expressed by me is not an official position of Microsoft

    Thursday, October 29, 2020 5:17 AM
  • Great Thanks!

    One more issue is upcoming, like when I have go back to the shared folder then it is giving an error. I mean that if I was using the path \\server\Folderroot\Sub-folder then getting the proper output as required but If I go changed the path like \\server\Folderroot then it is giving me an error below:

    The network path was not found.
    At line:5 char:1
    + $status=$nwobj.mapnetworkdrive("O:",$share)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], FileNotFoundException
        + FullyQualifiedErrorId : System.IO.FileNotFoundException
     
    get-psdrive : Cannot find drive. A drive with the name 'O' does not exist.
    At line:6 char:8
    + $drive=get-psdrive O
    +        ~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (O:String) [Get-PSDrive], DriveNotFoundException
        + FullyQualifiedErrorId : GetLocationNoMatchingDrive,Microsoft.PowerShell.Commands.GetPSDriveCommand
     
    Attempted to divide by zero.
    At line:10 char:1
    + $usedP = [Math]::Round(($used / $total) * 100, 2)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], RuntimeException
        + FullyQualifiedErrorId : RuntimeException
     
    Attempted to divide by zero.
    At line:13 char:1
    + $freeP=[Math]::Round(($free / $total) * 100, 2)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], RuntimeException
        + FullyQualifiedErrorId : RuntimeException


    • Edited by Pint2 Thursday, October 29, 2020 6:27 AM
    Thursday, October 29, 2020 6:06 AM
  • dont get exact scenario. Why u need folder inside the folder if free space would be exact the same

    The opinion expressed by me is not an official position of Microsoft


    • Edited by Vector BCO Thursday, October 29, 2020 6:37 AM
    Thursday, October 29, 2020 6:34 AM
  • Ok.

    As per your good advise, I have made some more changes according to our need. This script should be run into Solarwinds Monitoring but when we apply it then getting some error that

    "get-psdrive : Cannot find drive. A drive with the name 'O' does not exist"

    I have made some changes on my script. Please help me to make some correction if it is wrong somewhere:

    Once we are passing a shared path as avariable into below query then the above error comes:

    param ( [Parameter(Mandatory=$true)]
        [string]$share)
    #Get-Content $share| where {!($_ -match '^\s*$')} |foreach{
    $share=$_
    $nwobj=new-object -comobject WScript.Network
    $status=$nwobj.mapnetworkdrive("O:",$share)
    $drive=get-psdrive O
    $free=[Math]::Round(($drive.free)/1TB,2)
    $used=[Math]::Round(($drive.used)/1TB,2)
    $total=[Math]::Round($free+$used)
    $usedP = [Math]::Round(($used / $total) * 100, 2)
    #$total = [Math]::Round(($free+$used) / 1073741824, 2);

    $freeP=[Math]::Round(($free / $total) * 100, 2)
    $Threshold=([math]::Round($freeP))
    Write-Output "Share $share has free space of $Threshold %"

    $status=$nwobj.removenetworkdrive("O:")


    $Warning = 15
    $Critical = 20

    If ($Threshold -le "$Critical") {

    Write-Output "The Share $share has disk space below 15 %"

    Write-Host "Total Disk Space: $total TB"
    Write-Host "Free Disk Space: $free TB"


    Write-Host "Used Disk Space: $used TB"
    Write-Host "Free Disk Space: $freeP %"


    Write-Host "Used Disk Space: $usedP %"

    }
    elseIf ($Threshold -le "$Warning")
    {
    Write-Output "$share has disk space below 20 %"
     }
    # }

    Wednesday, November 4, 2020 6:20 AM
  • If you would use PowerShell instead of VBScript this would work.

    You can look into the Galley for scripts that do what you are trying to do and do it currectly using correctly written PS code.

    This forum is closing at the end of the day today.  You will need to go to another forum to ask your question.

    Microsoft forums are not at Microsoft Q&A supported products | Microsoft Docs


    \_(ツ)_/

    Wednesday, November 4, 2020 6:32 AM
  • Can you please advice on PS script only please?
    Wednesday, November 4, 2020 6:37 AM
  • Or, Can we get the DFS folder space one by one if we put the root folder path.

    Example: If the DFS is \\abc.domain\abcd\"There will be multiple folders"

    So, I want to get all shares space under "abcd"

    Wednesday, November 4, 2020 6:42 AM
  • And also want to make a Validation checks also before accessing the share path?

    Please help

    Wednesday, November 4, 2020 6:45 AM
  • DFS does not have a size.  It is just a redirector that gets files from other shares.  You must access the underlying drives to get an accurate space usage.  I recommend that you start by learning what DFS is and how it works.  This will tell you how to get the usage data.  YOu will also have to lwarn basic PowerShell to do this.  I recommend using one of the scripts in teh Galelry for this.


    \_(ツ)_/

    Wednesday, November 4, 2020 6:47 AM
  • Share your recommended script please
    Wednesday, November 4, 2020 7:00 AM
  • Please look in the Gallery or use your search engine.   I really don't have time to do this for you.


    \_(ツ)_/

    Wednesday, November 4, 2020 7:02 AM