Pages

Tuesday, April 11, 2017

Check Availability of a Share Path using PowerShell

Check Availability of a Share Path and log output to a text file located on that share:

Here is a simple PowerShell script which checks if a particular Network path (SMB Share/File Share/NAS or SAN  originating path) is online and reachable or not. It Writes a text file at the target location and keeps logging information at every fixed interval which can later be used to analyze availability of the target location.

######################################################################################
# This script checks availability of a Share at given interval and writes output     #
# in the file at that same shared location.                                          #
# Change the FileShare / SMB / NAS /SAN storage path in the variable path.           #
# Sleep timing (start-sleep) to check at whatever specific interval you want.        #
######################################################################################

## Main Loop to start and run endlessly :
for ($i = 1; $i -gt 0; $i++) {

## Take the Date_time variable to be used to write log in the output file:
 $Now = Get-Date -Format yyyy_MM_dd_hh:mm:ss

## Path of the Share along with file name which you want to check and log output to:
 $FilePath = "\\SharePath\d\logs\TestFile.txt"
        if (Test-Path $FilePath )
            { Write-Output "Share is alive at $Now" >> $FilePath }
        else { New-Item -ItemType "File" -Path $FilePath; Write-Output "Share is alive at $Now" >> $FilePath }

## Time to sleep and check again (3600 = 1 Hour) Convert the duration i seconds and put in at the below line:
 Start-Sleep -Seconds 3600
}

# Points to consider:
* Make sure this script is run from a location from where you want to check the availability of share.
* User running this script should have enough rights to create file on the target location.