I have a PS scrips as below, and I am trying to rewrite as, the script should detect the servers only if the below $SyncStatusNotMatch condition exists for 10 mins. Any ideas on how to achieve that ?
Param(
$MailList = ""
)
foreach ($Module in $Modules)
{
if (-not(get-module $module))
{
Import-Module $Module -ErrorAction Stop
}
}
$SyncStatusNotMatch = @(
"Enabled"
"Success"
) -join "|"
$CSVPath = "\\winrepo\winscripts\Get-SyncStatusAlert\SyncStatus.csv"
If (-not(test-path $CSVPath))
{
Write-Verbose "$(Get-Date): CSV does not exist it is being created" -Verbose
$Null = New-Item $CSVPath -ErrorAction Stop
}
Write-Verbose "$(Get-Date): Importing CSV for previous alerts" -Verbose
if ($env:Winusername)
{
Write-verbose "$(Get-date): Creating Secret Server Login" -Verbose
$SecurePassword = ConvertTo-SecureString $env:WinPassword -AsPlainText -Force -ErrorAction Stop
$SSCred = New-Object System.Management.Automation.PSCredential ($env:WinUserName,$SecurePassword) -ErrorAction Stop
$null = Get-SecretServerToken -OperatorCredentials $SSCred -ErrorAction Stop
}
$SyncStatus = Import-Csv $CSVPath -ErrorAction Stop
Write-Verbose "$(Get-Date): Obtaining data from Netscalers" -Verbose
$NSexsre = Get-sre
Write-Verbose "$(Get-Date): Comparing existing alerts with new alerts" -Verbose
$StillActives = foreach ($Status in $SyncStatus)
{
$StatusResult = $nsexsre |
Where-Object hasyncstatus -notmatch $SyncStatusNotMatch |
Where-Object hasyncstatus -eq $Status.hasyncstatus |
Where-Object name -eq $Status.name
if ($Null -ne $StatusResult)
{
[pscustomobject]@{
Name = $Status.name
HAState = $Status.HAState
HASyncStatus = $Status.HASyncStatus
AlertDate = $Status.AlertDate
}
}
}
$SendAlert = @()
if ($StillActives.count -gt 0)
{
Write-Verbose "$(Get-Date): Verifying if a previous alert has not been sent in 12 hours" -Verbose
foreach ($StillActive in $StillActives)
{
if ((Get-Date ($StillActive.alertdate)) -lt ((get-date).AddHours(-12)))
{
$SendAlert += $StillActive
}
}
$nsexsrefiltered = ($NSExsre |
Where-Object hasyncstatus -NotMatch $SyncStatusNotMatch |
Where-Object name -notmatch ($StillActives.name -join "|")
)
}
else
{
$nsexsrefiltered = $NSExsre | Where-Object hasyncstatus -NotMatch $SyncStatusNotMatch
}
if ($Null -ne $nsexsrefiltered)
{
Write-Verbose "$(Get-Date): Adding new alerts to the list" -Verbose
$UpdateList = foreach ($ns in $nsexsrefiltered)
{
[pscustomobject]@{
Name = $ns.Name
HAState = $ns.HAState
HASyncStatus = $ns.HASyncStatus
AlertDate = (Get-Date)
}
}
}
if(($Null -eq $StillActives) -and ($null -eq $UpdateList))
{
$NoAlerts =
[pscustomobject]@{
Name = $Null
HAState = $Null
HASyncStatus = $Null
AlertDate = $Null
}
$NoAlerts | Export-Csv $CSVPath -NoTypeInformation
}
Else
{
if ($Null -ne $UpdateList)
{
$ActivesToExport = $StillActives + $UpdateList
$ActivesToExport | Export-Csv $CSVPath -NoTypeInformation
}
else
{
$StillActives | Export-Csv $CSVPath -NoTypeInformation
}
}
If ($Null -ne $UpdateList)
{
$SendAlert += $UpdateList
}
If ($SendAlert.count -ne 0)
{
Write-Verbose "$(Get-Date): Sending email to $MailList" -Verbose
Foreach ($Alert in $SendAlert)
{
Write-Verbose "$(Get-Date): Sending Alert for: $($Alert.name)" -Verbose
}
$Style = @"
<style>
BODY{font-family: Arial; font-size: 10pt;}
TABLE{border: 1px solid black; border-collapse: collapse;}
TH{border: 1px solid black; background: #dddddd; padding: 5px; }
TD{border: 1px solid black; padding: 5px; }
.odd {background: #DCDCDC;}
.even {background: #FFFFFF;}
</style>
"@
$PreContent = "Link to the SOP for addressing this issue: https://x/vAMsFQ"
$Message = $SendAlert | ConvertTo-Html -Head $style -PreContent $PreContent | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$MailSplat = @{
To = $MailList
From = "noreply@.com"
SmtpServer = "hub.corp.com"
Subject = "out of Sync"
Body = $Message | Out-String
BodyAsHtml = $true
}
Send-MailMessage @MailSplat
}
Else
{
Write-Verbose "$(Get-Date): No Alerts to send" -Verbose
}