Will need to test the switch has a temperature control that returns any data via show env temperature
Param (
[Parameter(ValueFromPipeline=$true)]
[String[]]$Commands = @("LOGINCOMMANDS","en","terminal length 0","show env temperature"),
[string]$RemoteHost = "SWITCH-IP",
[string]$Port = "23",
[int]$WaitTime = 500
)
$Result = ""
$Result | out-file \\server\location\TEMP.txt
cls
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
If ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
ForEach ($Command in $Commands)
{ $Writer.WriteLine($command)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
}
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = @()
While($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result = ($Encoding.GetString($Buffer, 0, $Read))
$Result = [string]::concat($Result,",")
"$Result" | out-file '\\server\location\TEMP.txt' -Append
}
}
$RCSV= Get-Content \\server\location\TEMP.txt | Where {$_ -match 'temperature: '}
write-host $RCSV
[int]$count = 0
[int]$datas = 0
foreach ($line in $RCSV)
{
$check=$false
$check = $line -match 'temperature: '
if ($check = $true)
{
$newline = $line -replace '.*: '
$NL = $newline -replace 'C'
write-host " NL = $NL"
$datas = $datas + $NL
$count = $count + 1
}
}
[int]$avg=($datas/$count)
[int]$favg=($avg * 1.8 + 32)
CLS
write-host "Average Core Temp = $favg degrees F"
if ($favg -lt 90)
{
send-mailmessage -from "Switch@Domain.edu" -to "Alert@Domain.edu" -subject "Core Temperature Cool" -body "Average Core Temp = <font color='blue'><b> $favg degrees F</b></font>" –BodyasHtml -smtpServer smtp.domain.edu
}
elseif ($favg -ge 90 -and $favg -le 100)
{
send-mailmessage -from "Switch@Domain.edu" -to "Alert@Domain.edu" -subject "Core Temperature Warm" -body "Average Core Temp = <font color='orange'><b> $favg degrees F</b></font>" –BodyasHtml -smtpServer smtp.domain.edu
}
elseif ($favg -ge 100)
{
send-mailmessage -from "Switch@Domain.edu" -to "Alert@Domain.edu" -subject "**Core Temperature HOT**" -body "<font color='red'><b> Average Core Temp = $favg degrees F - Check environment!</font></b>" –BodyasHtml -priority High -smtp smtp.domain.edu
}
else
{
send-mailmessage -from "Switch@Domain.edu" -to "Alert@Domain.edu" -subject "Core Temperature Faulty Reading" -body "Unable to pull accurate core temperature - Check Core Temperature script" –BodyasHtml -smtpServer smtp.domain.edu
}
Comments