If for any reason you don’t want to use monitoring applications and want to do it by script, well below is small script for monitoring mounted iSCSI or other type of shared drives on Microsoft Windows using PowerShell.
In case of drive disappear because of disconnection from provider, it can send and email and also it can simply place in task scheduler.
In below script, drive letter(s) can be placed in $drvs variable. for sending email notification, kindly modify value of $smtpServer based on your SMTP mail server and define email for sender and receiver of notification in $from_address & $to_address.
Script is using Send-MailMessage capability of PowerShell, for more information about it this link can be use.
# iSCSI/Drive mount monitoring script - Ver 0.1
# Sohrab Kasraeianfard
# Drive letter(s) here
$drives = "X"
##################################################
$smtpServer = "smtp.domain.local"
$from_address = "sender@domain.local"
$to_address = "receiver@domain.local"
##################################################
$drv = ""
$drives | %{
if (Test-Path -Path $_":") {
echo "$($_):\ is accesssible"
} else {
echo "$($_):\ is not accessible"
if ($drv -eq "" ) { $drv = $drv + "$($_):\" } else { $drv = $drv + " & $($_):\" }
}
}
##### Configuration of the Subject and the body of the email
$subject = "iSCSI/drive mount on $(HOSTNAME) is not accessible ($(Get-Date -Format MM\/dd\/yyyy))"
$body = "<br />--- This is an automated message ---<br /><br />
Please check iSCSI/drive mount $drv on $(HOSTNAME) as script can't access the drive.<br />"
##### Sending email if needed
if ( $drv -ne "" ) {
Send-Mailmessage -smtpServer $smtpServer -from $from_address -to $to_address -Subject $subject -Body $body -BodyAsHtml
}