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.

5 comments:

  1. Thanks Prakash.. It works very well..

    ReplyDelete
  2. Use following as per your requirement:
    It will Test availability of the share Path, Create a Testfile at the share Delete a test file from Share and log it in the Log file:


    $Now = Get-Date -Format yyyy_MM_dd_hh:mm:ss
    $CreateFile = "\\SharePath\Test_File.txt"
    $LogFile = "\\SharePath\Log_File.txt"

    if (Test-Path $LogFile ) { $ShareAlive = "Share is alive at $Now" }
    else { New-Item -ItemType "File" -Path $LogFile; $ShareAlive = "Share is alive at $Now" }


    $CreateTime = (New-Item -ItemType "File" -Path "$CreateFile").CreationTime
    $Created = "$createfile Created successfully at $CreateTime"
    Start-Sleep -Seconds 60

    Remove-Item -Path $CreateFile
    $DeletedTime = Get-Date -Format yyyy_MM_dd_hh:mm:ss
    $Deleted = "$CreateFile Deleted Successfully at $DeletedTime"

    $AllDetails = "$ShareAlive; $Created; $Deleted" >> $LogFile


    ReplyDelete
    Replies
    1. Agar tu nahi hota.. to Mera Kya hota.... God bless u Bhai.. seriously.. these words coming from my heart...

      Delete
  3. Tu bus meri aisi hi help Kar ta rahe or mai appraisal lata rahi... Seriously I want to hug u... 😎.. I'm out of words.......

    ReplyDelete