Powershell中的PC ping脚本
该脚本不断从导入CSV列1 ping PC主机名列表 第1列(主机名)第2列(用户)←手动更新字段 当脚本识别出3次失败的ping尝试时,它会通过/IP address/failed Pings向我们的电子邮件地址发送电子邮件,其中包含Time/Hostname/Owner(取自.csv文件) 这很好用。但是,当电脑重新联机时,它会发送另一封包含上述内容的电子邮件,但不会显示正确的所有者。有人能帮助将Powershell中的PC ping脚本,powershell,email,ping,Powershell,Email,Ping,该脚本不断从导入CSV列1 ping PC主机名列表 第1列(主机名)第2列(用户)←手动更新字段 当脚本识别出3次失败的ping尝试时,它会通过/IP address/failed Pings向我们的电子邮件地址发送电子邮件,其中包含Time/Hostname/Owner(取自.csv文件) 这很好用。但是,当电脑重新联机时,它会发送另一封包含上述内容的电子邮件,但不会显示正确的所有者。有人能帮助将$Back所有者与相应的用户关联起来吗?就像它与最初的电子邮件回复一起工作一样。此时,无论第一封
$Back
所有者与相应的用户关联起来吗?就像它与最初的电子邮件回复一起工作一样。此时,无论第一封通知电子邮件是否正确,它都会在电子邮件的用户栏中显示任何内容或姓氏
$computers = Import-Csv C:\temp\Reporting\MainHosts.csv
$Sources = @($Env:COMPUTERNAME)
$To = "Username@Email.com"#,"Username@Email.com"
$From = "Username@Email.com"
$SMTPServer = "SERVER"
[datetime]$TimeStop = "16:17"
$StartTime = Get-Date -format 'dd-MM-yyyy hh:mm:ss'
Clear-Host
$Header = @"
<style>BODY{font-family: Arial; font-size: 11pt;}
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;text-align: center;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;text-align: center;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;text-align: center;}
</style>
"@
######################Load data array (Table) ######################
$Status = @{}
foreach ($Source in $Sources) {
foreach ($computer in $computers) {
$computername = $($computer.Hostname)
$owner = $($computer.User)
$Status.Add("$Source`:$computername", [PSCustomObject]@{
Time = ""
Hostname = $computername
Owner = $owner
From = $Source
'IP Address' = $null
'Failed Pings' = 0
})
}
}
###################### Script Begin Message ######################
do {
$Results = foreach ($Source in $Sources) {
foreach ($computer in $computers) {
$computername = $($computer.Hostname) #Defining the Hostname column within .csv sheet
$owner = $($computer.User)
try {
Write-Host "." -NoNewline
Get-WmiObject "Win32_PingStatus" -ComputerName $Source -Filter "Address = '$computername'" -ErrorAction Stop | Select PSComputerName,Address,IPV4Address,StatusCode
} catch {
Write-Warning "Error with $computername`: $($Error[0])"
}
}
}
$Back = @()
foreach ($Result in $Results) {
$Key = "$($Result.PSComputerName):$($Result.Address)"
$Status[$Key].'IP Address' = $Result.IPV4Address
$Status[$Key].Time = Get-Date
if ($Result.StatusCode -eq 0) {
if ($Status[$Key].'Failed Pings' -ge 3) {
$Back += [PSCustomObject]@{
Time = Get-Date
Hostname = $Result.Address
Owner = $owner
From = $Result.PSComputerName
IPAddress = $Result.IPV4Address
Status = "Connectivity Returned"
}
}
$Status[$Key].'Failed Pings' = 0
} else {
$Status[$Key].'Failed Pings' ++
}
}
# Email Alert
if ($Back) {
$HTML = $Back | Sort Destination,From | ConvertTo-Html -Head $Header -PreContent "<p>Ping Detection Script has detected the following workstations are now online and <b>restored connectivity!</b><br></p>" | Out-String
Send-MailMessage -To $To -From $From -Subject "**No Action Required** - Ping Detection Script Reporting Connections Restored" -Body $HTML -BodyAsHtml -SmtpServer $SMTPServer
}
$Data = $Status.Values | Where { $_.'Failed Pings' -eq 3 -or ( ($_.'Failed Pings' -ne 0 -and -not ($_.'Failed Pings' % $Alert))) }
if ($Data) {
$HTML = $Data | Sort Destination,From | ConvertTo-Html -Head $Header -PreContent "<p>Ping Detection Script has detected <b>Offline</b> workstations! See below list<br></p>" | Out-String
Send-MailMessage -To $To -From $From -Subject "**Urgent Action Required** - Ping Detection Script Reporting Offline Workstations" -Body $HTML -BodyAsHtml -SmtpServer $SMTPServer
Write-Host "Alert Email Sent!`n"
Start-Sleep -Seconds 1
}
Clear-Host
$Status.Values | Sort Destination,From | Format-Table -AutoSize
Write-Host "Ping Script Monitoring ..." -NoNewline
Start-Sleep -Seconds 4
} until ($Time.Hour -eq $TimeStop.Hour -and $Time.Minute -eq $TimeStop.Minute)
Write-Host "`nScript shutting down, time limit reached: $($TimeStop.Hour):$($TimeStop.Minute)"
Write-Output $Status.Values | Sort Destination,From | Format-Table -AutoSize
$computers=Import Csv C:\temp\Reporting\MainHosts.Csv
$Sources=@($Env:COMPUTERNAME)
$To=”Username@Email.com"#,"Username@Email.com"
$From=”Username@Email.com"
$SMTPServer=“服务器”
[日期时间]$TimeStop=“16:17”
$StartTime=获取日期-格式为'dd-MM-yyy-hh:MM:ss'
清除主机
$Header=@“
正文{字体系列:Arial;字体大小:11pt;}
表{边框宽度:1px;边框样式:实心;边框颜色:黑色;边框折叠:折叠;文本对齐:居中;}
TH{边框宽度:1px;填充:3px;边框样式:纯色;边框颜色:黑色;背景颜色:#6495ED;文本对齐:居中;}
TD{边框宽度:1px;填充:3px;边框样式:纯色;边框颜色:黑色;文本对齐:居中;}
"@
######################加载数据数组(表)######################
$Status=@{}
foreach($Sources中的Source){
foreach($computers in$computers){
$computername=$($computer.Hostname)
$owner=$($computer.User)
$Status.Add(“$Source`:$computername”,[PSCustomObject]@{
Time=“”
主机名=$computername
所有者=$Owner
From=$Source
“IP地址”=$null
“ping失败”=0
})
}
}
######################脚本开始消息######################
做{
$Results=foreach($Sources中的Source){
foreach($computers in$computers){
$computername=$($computer.Hostname)#在.csv工作表中定义主机名列
$owner=$($computer.User)
试一试{
写入主机“”-非脱机
获取WmiObject“Win32_PingStatus”-ComputerName$源-筛选器“Address='$ComputerName'”-错误操作停止|选择PSComputerName、Address、IPV4Address、StatusCode
}抓住{
写入警告“错误为$computername`:$($Error[0])”
}
}
}
$Back=@()
foreach($Result in$Results){
$Key=“$($Result.PSComputerName):$($Result.Address)”
$Status[$Key]。'IP Address'=$Result.IPV4Address
$Status[$Key]。时间=获取日期
如果($Result.StatusCode-等式0){
如果($Status[$Key]。'Failed Pings'-ge 3){
$Back+=[PSCustomObject]@{
时间=获取日期
主机名=$Result.Address
所有者=$Owner
From=$Result.PSComputerName
IPAddress=$Result.IPV4Address
Status=“已返回连接”
}
}
$Status[$Key]。“ping失败”=0
}否则{
$Status[$Key]。“ping失败”++
}
}
#电子邮件警报
如果($返回){
$HTML=$Back |排序目的地,从| ConvertTo HTML-Head$Header-PreContent“Ping检测脚本已检测到以下工作站现在处于联机状态并已恢复连接!
”|输出字符串
发送邮件消息-至$To-从$From-主题“**无需操作**-Ping检测脚本报告已恢复连接”-正文$HTML-正文-BodyAsHtml-SmtpServer$SmtpServer
}
$Data=$Status.Values |其中{$.'Failed PING'-eq 3-或($.'Failed PING'-ne 0-和-not($.'Failed PING'$Alert))}
如果($数据){
$HTML=$Data |排序目的地,从| ConvertTo HTML-Head$Header-PreContent“Ping检测脚本已检测到脱机工作站!请参阅下面的列表
”|输出字符串
发送邮件消息-至$To-从$From-主题“**需要紧急操作**-Ping检测脚本报告脱机工作站”-正文$HTML-正文ashtml-SmtpServer$SmtpServer
写入主机“已发送警报电子邮件!`n”
开始睡眠-秒1
}
清除主机
$Status.Values |排序目标,来自|格式表-自动调整大小
写入主机“Ping脚本监视…”-非脱机
开始睡眠-4秒
}直到($Time.Hour-eq$TimeStop.Hour-和$Time.Minute-eq$TimeStop.Minute)
写入主机“`nScript正在关闭,已达到时间限制:$($TimeStop.Hour):$($TimeStop.Minute)”
写入输出$Status.Values |排序目标,从|格式表-自动调整大小
将$Owner
添加到脚本开始消息之后第一个嵌入循环的WMI对象中(并使用…Owner=$Result…
):
或者忘记第一个嵌入循环中的所有者,并在foreach($Result in$Results){…
循环中动态检索所有者(owner=$Computers |其中{$$.Name=$Result.PSComputerName}
):
$Back += [PSCustomObject]@{
Time = Get-Date
Hostname = $Result.Address
Owner = $Computers | Where {$_.Name = $Result.PSComputerName}
From = $Result.PSComputerName
IPAddress = $Result.IPV4Address
Status = "Connectivity Returned"
}
将$Owner
添加到脚本开始消息之后第一个嵌入循环的WMI对象中(并使用…Owner=$Result…
):
或者忘记第一个嵌入循环中的所有者,并在foreach($Result in$Results){…
循环中动态检索所有者(owner=$Computers |其中{$$.Name=$Result.PSComputerName}
):
$Back += [PSCustomObject]@{
Time = Get-Date
Hostname = $Result.Address
Owner = $Computers | Where {$_.Name = $Result.PSComputerName}
From = $Result.PSComputerName
IPAddress = $Result.IPV4Address
Status = "Connectivity Returned"
}