param(
# The share to monitor
[String]$Share = "\\server\share"
# The computer which hosts services to restart
[String]$ComputerName = 'someServer',
# The list of services to restart on the server defined by the ComputerName parameter.
[String[]]$Services = ('service1', 'server2')
# How often the share is polled, in seconds
[Int]$PollingFrequency = 10
)
function Write-Log {
param(
# A message to add to the log file.
[String]$Message,
# Indicates the severity of the event.
[ValidateSet('INFORMATION', 'WARNING', 'ERROR')]
[String]$LogLevel = 'INFORMATION',
# The delimiter for the log file.
[String]$Delimiter = "`t",
# The number of log entries the log file is allowed to contain.
[Int32]$MaximumSize = 2000
)
begin {
$path = Join-Path ([Environment]::GetEnvironmentVariable('TEMP', 'Machine')) 'ShareMonitor.log'
}
process {
'{1}{0}{2}{0}{3}' -f $Delimiter, (Get-Date).ToString('u'), $LogLevel, $Message | Out-File $path -Append
}
end {
# Truncate the log file
Set-Content $path -Value (Get-Content $path | Select-Object -Last $MaximumSize)
}
}
$stopwatch = New-Object System.Diagnostics.StopWatch
while ($true) {
# Watch the share until it goes away
$stopwatch.Reset()
$stopwatch.Start()
$i = 0
while (Test-Path $Share) {
# How often the state of the share will be checked.
Start-Sleep -Seconds $PollingFrequency
# Log a message every 10 minutes
if ($i -eq 0 -or $i -ge 600) {
Write-Log ('{0} is available' -f $Share)
$i = 0
}
$i += $PollingFrequency
}
Write-Log ('{0} has gone offline after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds) -LogLevel WARNING
$stopwatch.Reset()
$stopwatch.Start()
# In theory, at this point the share has gone. Wait for it to come back.
$i = 0
do {
Start-Sleep -Seconds $PollingFrequency
if ($ -eq 0 -or $i -ge 600) {
Write-Log ('Waiting for {0} to come online' -f $Share)
$i = 0
}
$i += $PollingFrequency
} until (Test-Path $Share)
Write-Log ('{0} came online after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds)
# Now restart services
try {
Restart-Service $Services -ComputerName $ComputerName -ErrorAction Stop
} catch {
Write-Log ('An error occurred while restarting services: {0}' -f $_.Exception.Message) -LogLevel ERROR
}
}
param(
# The share to monitor
[String]$Share = "\\server\share"
# The computer which hosts services to restart
[String]$ComputerName = 'someServer',
# The list of services to restart on the server defined by the ComputerName parameter.
[String[]]$Services = ('service1', 'server2')
# How often the share is polled while up, in seconds.
[Int]$PollingFrequency = 10,
# The pattern used to test while the share is down, in seconds.
[Int[]]$PollingPattern = (15, 15, 15, 15, 60),
# Send mail notifications to (mandatory).
[String]$To = 'email@domain.com ',
# Send mail notifications from (mandatory).
[String]$From,
# The SMTP server to send the message via (mandatory).
[String]$SmtpServer
)
function Write-Log {
param(
# A message to add to the log file.
[String]$Message,
# Indicates the severity of the event.
[ValidateSet('INFORMATION', 'WARNING', 'ERROR')]
[String]$LogLevel = 'INFORMATION',
# The delimiter for the log file.
[String]$Delimiter = "`t",
# The number of log entries the log file is allowed to contain.
[Int32]$MaximumSize = 2000
)
begin {
$path = Join-Path ([Environment]::GetEnvironmentVariable('TEMP', 'Machine')) 'ShareMonitor.log'
}
process {
'{1}{0}{2}{0}{3}' -f $Delimiter, (Get-Date).ToString('u'), $LogLevel, $Message | Out-File $path -Append
}
end {
# Truncate the log file
Set-Content $path -Value (Get-Content $path | Select-Object -Last $MaximumSize)
}
}
$mailParams = @{
To = $To
From = $From
SmtpServer = $SmtpServer
}
$stopwatch = New-Object System.Diagnostics.StopWatch
while ($true) {
# Watch the share until it goes away
$stopwatch.Reset()
$stopwatch.Start()
$i = 0
while (Test-Path $Share) {
# How often the state of the share will be checked.
Start-Sleep -Seconds $PollingFrequency
# Log a message every 10 minutes
if ($i -eq 0 -or $i -ge 600) {
Write-Log ('{0} is available' -f $Share)
$i = 0
}
$i += $PollingFrequency
}
Write-Log ('{0} has gone offline after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds) -LogLevel WARNING
$stopwatch.Reset()
$stopwatch.Start()
# In theory, at this point the share has gone. Wait for it to come back.
$i = $j = $k = 0
# The polling pattern will repeat twice inside this loop.
do {
Write-Log ('Waiting for {0} to come online' -f $Share)
# A notification will be sent once, at the end of the first iteration of the
# polling pattern.
if ($k -eq $j.Count) {
$params = @{
Subject = 'Share not available'
Body = 'Still waiting for {0} to come online after {1} seconds' -f $Share, ($PollingPattern | Measure-Object -Sum).Sum
}
Send-MailMessage @params @mailParams
}
$k++
Start-Sleep -Seconds $PollingPattern[$j++]
if ($j -eq $PollingPattern.Count) {
$j = 0
}
} until ((Test-Path $Share) -or ($k -ge $PollingPattern.Count * 2))
# Now restart services
try {
Restart-Service $Services -ComputerName $ComputerName -ErrorAction Stop
Send-MailMessage -Subject 'Service restart success' -Body ('Successfully restarted {0}' -f ($Services -join ', ')) @mailParams
} catch {
Write-Log ('An error occurred while restarting services: {0}' -f $_.Exception.Message) -LogLevel ERROR
Send-MailMessage -Subject 'Service restart failed' -Body ('Failed to restart {0}: {1}' -f ($Services -join ', '), $_.Exception.Message) @mailParams
}
}
[String]$From = 'Someone@domain.com',
[String]$SmtpServer = 'someserver'
Be a little careful with SmtpServer, all parameters except the last one must end with "," and SmtpServer is the last parameter.
param(
# The share to monitor
[String]$Share = "\\vsa33.mmt.s.com\temp"
# The computer which hosts services to restart
[String]$ComputerName = 'LA-74C4.mmt.s.com',
# The list of services to restart on the server defined by the ComputerName parameter.
[String[]]$Services = ('Dnscache')
# How often the share is polled while up, in seconds.
[Int]$PollingFrequency = 10,
# The pattern used to test while the share is down, in seconds.
[Int[]]$PollingPattern = (15, 15, 15, 15, 60),
# Send mail notifications to (mandatory).
[String]$To = 'lla@cy.com ',
# Send mail notifications from (mandatory).
[String]$From = 'mds@cy.com ',
# The SMTP server to send the message via (mandatory).
[String]$SmtpServer = 'smtprelay@cy.com '
)
function Write-Log {
param(
# A message to add to the log file.
[String]$Message,
# Indicates the severity of the event.
[ValidateSet('INFORMATION', 'WARNING', 'ERROR')]
[String]$LogLevel = 'INFORMATION',
# The delimiter for the log file.
[String]$Delimiter = "`t",
# The number of log entries the log file is allowed to contain.
[Int32]$MaximumSize = 2000
)
begin {
$path = Join-Path ([Environment]::GetEnvironmentVariable('TEMP', 'Machine')) 'ShareMonitor.log'
}
process {
'{1}{0}{2}{0}{3}' -f $Delimiter, (Get-Date).ToString('u'), $LogLevel, $Message | Out-File $path -Append
}
end {
# Truncate the log file
Set-Content $path -Value (Get-Content $path | Select-Object -Last $MaximumSize)
}
}
$mailParams = @{
To = $To
From = $From
SmtpServer = $SmtpServer
}
$stopwatch = New-Object System.Diagnostics.StopWatch
while ($true) {
# Watch the share until it goes away
$stopwatch.Reset()
$stopwatch.Start()
$i = 0
while (Test-Path $Share) {
# How often the state of the share will be checked.
Start-Sleep -Seconds $PollingFrequency
# Log a message every 10 minutes
if ($i -eq 0 -or $i -ge 600) {
Write-Log ('{0} is available' -f $Share)
$i = 0
}
$i += $PollingFrequency
}
Write-Log ('{0} has gone offline after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds) -LogLevel WARNING
$stopwatch.Reset()
$stopwatch.Start()
# In theory, at this point the share has gone. Wait for it to come back.
$i = $j = $k = 0
# The polling pattern will repeat twice inside this loop.
do {
Write-Log ('Waiting for {0} to come online' -f $Share)
# A notification will be sent once, at the end of the first iteration of the
# polling pattern.
if ($k -eq $j.Count) {
$params = @{
Subject = 'Share not available'
Body = 'Still waiting for {0} to come online after {1} seconds' -f $Share, ($PollingPattern | Measure-Object -Sum).Sum
}
Send-MailMessage @params @mailParams
}
$k++
Start-Sleep -Seconds $PollingPattern[$j++]
if ($j -eq $PollingPattern.Count) {
$j = 0
}
} until ((Test-Path $Share) -or ($k -ge $PollingPattern.Count * 2))
# Now restart services
try {
Restart-Service $Services -ComputerName $ComputerName -ErrorAction Stop
Send-MailMessage -Subject 'Service restart success' -Body ('Successfully restarted {0}' -f ($Services -join ', ')) @mailParams
} catch {
Write-Log ('An error occurred while restarting services: {0}' -f $_.Exception.Message) -LogLevel ERROR
Send-MailMessage -Subject 'Service restart failed' -Body ('Failed to restart {0}: {1}' -f ($Services -join ', '), $_.Exception.Message) @mailParams
}
}
param(
# The share to monitor
[String]$Share = "\\vsa33.mmt.s.com\temp",
# The computer which hosts services to restart
[String]$ComputerName = 'LA-74C4.mmt.s.com',
# The list of services to restart on the server defined by the ComputerName parameter.
[String[]]$Services = ('Dnscache'),
# How often the share is polled while up, in seconds.
[Int]$PollingFrequency = 10,
# The pattern used to test while the share is down, in seconds.
[Int[]]$PollingPattern = (15, 15, 15, 15, 60),
# Send mail notifications to (mandatory).
[String]$To = 'lla@cy.com ',
# Send mail notifications from (mandatory).
[String]$From = 'mds@cy.com ',
# The SMTP server to send the message via (mandatory).
[String]$SmtpServer = 'smtprelay@cy.com '
)
function Write-Log {
param(
# A message to add to the log file.
[String]$Message,
# Indicates the severity of the event.
[ValidateSet('INFORMATION', 'WARNING', 'ERROR')]
[String]$LogLevel = 'INFORMATION',
# The delimiter for the log file.
[String]$Delimiter = "`t",
# The number of log entries the log file is allowed to contain.
[Int32]$MaximumSize = 2000
)
begin {
$path = Join-Path ([Environment]::GetEnvironmentVariable('TEMP', 'Machine')) 'ShareMonitor.log'
}
process {
'{1}{0}{2}{0}{3}' -f $Delimiter, (Get-Date).ToString('u'), $LogLevel, $Message | Out-File $path -Append
}
end {
# Truncate the log file
Set-Content $path -Value (Get-Content $path | Select-Object -Last $MaximumSize)
}
}
$mailParams = @{
To = $To
From = $From
SmtpServer = $SmtpServer
}
$stopwatch = New-Object System.Diagnostics.StopWatch
while ($true) {
# Watch the share until it goes away
$stopwatch.Reset()
$stopwatch.Start()
$i = 0
while (Test-Path $Share) {
# How often the state of the share will be checked.
Start-Sleep -Seconds $PollingFrequency
# Log a message every 10 minutes
if ($i -eq 0 -or $i -ge 600) {
Write-Log ('{0} is available' -f $Share)
$i = 0
}
$i += $PollingFrequency
}
Write-Log ('{0} has gone offline after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds) -LogLevel WARNING
$stopwatch.Reset()
$stopwatch.Start()
# In theory, at this point the share has gone. Wait for it to come back.
$i = $j = $k = 0
# The polling pattern will repeat twice inside this loop.
do {
Write-Log ('Waiting for {0} to come online' -f $Share)
# A notification will be sent once, at the end of the first iteration of the
# polling pattern.
if ($k -eq $j.Count) {
$params = @{
Subject = 'Share not available'
Body = 'Still waiting for {0} to come online after {1} seconds' -f $Share, ($PollingPattern | Measure-Object -Sum).Sum
}
Send-MailMessage @params @mailParams
}
$k++
Start-Sleep -Seconds $PollingPattern[$j++]
if ($j -eq $PollingPattern.Count) {
$j = 0
}
} until ((Test-Path $Share) -or ($k -ge $PollingPattern.Count * 2))
# Now restart services
try {
Restart-Service $Services -ComputerName $ComputerName -ErrorAction Stop
Send-MailMessage -Subject 'Service restart success' -Body ('Successfully restarted {0}' -f ($Services -join ', ')) @mailParams
} catch {
Write-Log ('An error occurred while restarting services: {0}' -f $_.Exception.Message) -LogLevel ERROR
Send-MailMessage -Subject 'Service restart failed' -Body ('Failed to restart {0}: {1}' -f ($Services -join ', '), $_.Exception.Message) @mailParams
}
}
param(
# The share to monitor
[String]$Share = "\\vsa33.mmt.s.com\temp",
# The computer which hosts services to restart
[String]$ComputerName = 'LA-74C4.mmt.s.com',
# The list of services to restart on the server defined by the ComputerName parameter.
[String[]]$Services = ('Dnscache'),
# How often the share is polled while up, in seconds.
[Int]$PollingFrequency = 10,
# The pattern used to test while the share is down, in seconds.
[Int[]]$PollingPattern = (15, 15, 15, 15, 60),
# Send mail notifications to (mandatory).
[String]$To = 'lla@cy.com ',
# Send mail notifications from (mandatory).
[String]$From = 'mds@cy.com ',
# The SMTP server to send the message via (mandatory).
[String]$SmtpServer = 'smtprelay@cy.com '
)
function Write-Log {
param(
# A message to add to the log file.
[String]$Message,
# Indicates the severity of the event.
[ValidateSet('INFORMATION', 'WARNING', 'ERROR')]
[String]$LogLevel = 'INFORMATION',
# The delimiter for the log file.
[String]$Delimiter = "`t",
# The number of log entries the log file is allowed to contain.
[Int32]$MaximumSize = 2000
)
begin {
$path = Join-Path ([Environment]::GetEnvironmentVariable('TEMP', 'Machine')) 'ShareMonitor.log'
}
process {
'{1}{0}{2}{0}{3}' -f $Delimiter, (Get-Date).ToString('u'), $LogLevel, $Message | Out-File $path -Append
}
end {
# Truncate the log file
Set-Content $path -Value (Get-Content $path | Select-Object -Last $MaximumSize)
}
}
$mailParams = @{
To = $To
From = $From
SmtpServer = $SmtpServer
}
$stopwatch = New-Object System.Diagnostics.StopWatch
while ($true) {
# Watch the share until it goes away
$stopwatch.Reset()
$stopwatch.Start()
$i = 0
while (Test-Path $Share) {
# How often the state of the share will be checked.
Start-Sleep -Seconds $PollingFrequency
# Log a message every 10 minutes
if ($i -eq 0 -or $i -ge 600) {
Write-Log ('{0} is available' -f $Share)
$i = 0
}
$i += $PollingFrequency
}
Write-Log ('{0} has gone offline after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds) -LogLevel WARNING
$stopwatch.Reset()
$stopwatch.Start()
# In theory, at this point the share has gone. Wait for it to come back.
$i = $j = $k = 0
# The polling pattern will repeat twice inside this loop.
do {
Write-Log ('Waiting for {0} to come online' -f $Share)
# A notification will be sent once, at the end of the first iteration of the
# polling pattern.
if ($k -eq $j.Count) {
$params = @{
Subject = 'Share not available'
Body = 'Still waiting for {0} to come online after {1} seconds' -f $Share, ($PollingPattern | Measure-Object -Sum).Sum
}
Send-MailMessage @params @mailParams
}
$k++
Start-Sleep -Seconds $PollingPattern[$j++]
if ($j -eq $PollingPattern.Count) {
$j = 0
}
} until (Test-Path $Share)
# Now restart services
try {
Restart-Service $Services -ComputerName $ComputerName -ErrorAction Stop
Send-MailMessage -Subject 'Service restart success' -Body ('Successfully restarted {0}' -f ($Services -join ', ')) @mailParams
} catch {
Write-Log ('An error occurred while restarting services: {0}' -f $_.Exception.Message) -LogLevel ERROR
Send-MailMessage -Subject 'Service restart failed' -Body ('Failed to restart {0}: {1}' -f ($Services -join ', '), $_.Exception.Message) @mailParams
}
}
param(
# The share to monitor
[String]$Share = "\\vsa33.mmt.s.com\temp",
# The computer which hosts services to restart
[String]$ComputerName = 'LA-74C4.mmt.s.com',
# The list of services to restart on the server defined by the ComputerName parameter.
[String[]]$Services = ('Dnscache'),
# How often the share is polled while up, in seconds.
[Int]$PollingFrequency = 10,
# The pattern used to test while the share is down, in seconds.
[Int[]]$PollingPattern = (15, 15, 15, 15, 60),
# Consider a share to have failed when it is not available for more than a specific number of tests.
[Int]$FailAfter = 4,
# Send mail notifications to (mandatory).
[String]$To = 'lla@cy.com',
# Send mail notifications from (mandatory).
[String]$From = 'mds@cy.com',
# The SMTP server to send the message via (mandatory).
[String]$SmtpServer = 'smtprelay@cy.com'
)
function Write-Log {
param(
# A message to add to the log file.
[String]$Message,
# Indicates the severity of the event.
[ValidateSet('INFORMATION', 'WARNING', 'ERROR')]
[String]$LogLevel = 'INFORMATION',
# The delimiter for the log file.
[String]$Delimiter = "`t",
# The number of log entries the log file is allowed to contain.
[Int32]$MaximumSize = 2000
)
begin {
$path = Join-Path ([Environment]::GetEnvironmentVariable('TEMP', 'Machine')) 'ShareMonitor.log'
}
process {
'{1}{0}{2}{0}{3}' -f $Delimiter, (Get-Date).ToString('u'), $LogLevel, $Message | Out-File $path -Append
}
end {
# Truncate the log file
Set-Content $path -Value (Get-Content $path | Select-Object -Last $MaximumSize)
}
}
$mailParams = @{
To = $To
From = $From
SmtpServer = $SmtpServer
}
$stopwatch = New-Object System.Diagnostics.StopWatch
while ($true) {
# Watch the share until it goes away
$stopwatch.Reset()
$stopwatch.Start()
$i = $j = 0
while ($j -le $FailAfter) {
$isAvailable = Test-Path $Share
if ($isAvailable) {
# Reset the counter j if the share is available
$j = 0
} else {
# Increase the counter j if the share is not available
$j++
}
# How often the state of the share will be checked.
Start-Sleep -Seconds $PollingFrequency
# Log a message every 10 minutes
if ($i -eq 0 -or $i -ge 600) {
Write-Log ('{0} is {1}' -f $Share, ('not available', 'available')[$isAvailable])
$i = 0
}
$i += $PollingFrequency
}
Write-Log ('{0} has gone offline after {1} seconds' -f $Share, $stopwatch.Elapsed.TotalSeconds) -LogLevel WARNING
$stopwatch.Reset()
$stopwatch.Start()
# In theory, at this point the share has gone. Wait for it to come back.
$i = $j = $k = 0
# The polling pattern will repeat twice inside this loop.
do {
Write-Log ('Waiting for {0} to come online' -f $Share)
# A notification will be sent once, at the end of the first iteration of the
# polling pattern.
if ($k -eq $j.Count) {
$params = @{
Subject = 'Share not available'
Body = 'Still waiting for {0} to come online after {1} seconds' -f $Share, ($PollingPattern | Measure-Object -Sum).Sum
}
Send-MailMessage @params @mailParams
}
$k++
Start-Sleep -Seconds $PollingPattern[$j++]
if ($j -eq $PollingPattern.Count) {
$j = 0
}
} until (Test-Path $Share)
# Now restart services
try {
Restart-Service $Services -ComputerName $ComputerName -ErrorAction Stop
Send-MailMessage -Subject 'Service restart success' -Body ('Successfully restarted {0}' -f ($Services -join ', ')) @mailParams
} catch {
Write-Log ('An error occurred while restarting services: {0}' -f $_.Exception.Message) -LogLevel ERROR
Send-MailMessage -Subject 'Service restart failed' -Body ('Failed to restart {0}: {1}' -f ($Services -join ', '), $_.Exception.Message) @mailParams
}
}
Open in new window