34,597
社区成员
发帖
与我相关
我的任务
分享
# Created at 2010-08-23
# Created by obuntu
#
# 创建获取服务器错误和警告信息的函数
# 在POWERSHELL里,Get-EventLog还可以管理远程服务器,如果有多台服务器的话,会更方便管理
function get-sqlEventLog{
#参数为服务器名,后续调用函数时,输入服务器名就可以
Param(
[String] $serverName
)
# EntryType用来筛选错误和警告信息
# APPLICATION指的是应用程序的信息,其他的还有 SYSTEM,SECURITY
#利用get-date获取最近1个小时的错误和警告信息
Get-EventLog Application -ComputerName $serverName -EntryType 'Error','Warning' -After ((get-date).addhours(-1)) |Format-List
}
$headline=(Get-Date).toString() #写入时间
$headline_TIME="----------当前时间为"+$headline+"-----"
$headline_TIME|out-file "C:\ErrorLogInfo.txt"
‘-----------------------Server01的日志内容-----------------------' |out-file "C:\ErrorLogInfo.txt" -Append
get-sqlEventLog '192.168.2.1' |Out-File "C:\ErrorLogInfo.txt" -Append
‘-----------------------Server02的日志内容 -----------------------' |out-file "C:\ErrorLogInfo.txt" -Append
get-sqlEventLog '192.168.3.1' |Out-File "C:\ErrorLogInfo.txt" -Append
#...可以按照上述方式获取更多台服务器的日志
$errortext=Get-Content -Path "C:\ErrorLogInfo.txt"
if($errortext -match "Index")
{
$message =New-Object System.Net.Mail.MailMessage -ArgumentList xx@aa.com,yy@aa.com,'系统错误和警告信息',' 有错误发生,请尽快排除!!'
$attachment=New-Object System.Net.Mail.Attachment -ArgumentList 'C:\ErrorLogInfo.txt',‘Application/Octet’
$message.Attachments.Add($attachment)
$smtp=New-Object System.Net.Mail.SMTPClient -ArgumentList 192.168.4.1 #指定邮件服务器地址
$smtp.send($message)
}
# Created at 2010-08-23
# Created by obuntu
# 在-match后面可以利用正则表达式来筛选自己想关注的服务状态
# 在-computername后面指定多台服务器
$message=Get-Service -computername 192.168.2.1,192.168.3.1,192.168.4.1 | Where-Object {$_.Name -match "\bmsdtc\b|\bmssqlserver\b|\bsqlwriter\b|\bmsdtsserver100\b|\bsqlserveragent\b"} `
|sort machinename|format-table -Property machinename,name,status,displayname -auto |Out-String
#如果有服务停止,立即发送邮件
if($message -match "stopped")
{
$emailFrom = "xx@aa.com"
$emailTo = "yy@aa.com"
$subject = "服务停止警告"
$body="有服务发生停止,请尽快处理!!"
$body = $body+$message
$smtpServer = "192.168.5.1"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
#by obuntu
# 2010-8-23
$message=Get-WmiObject win32_logicaldisk -computername `
192.168.2.1,192.168.3.1,192.168.4.1 |Where-Object{$_.DriveType -eq 3} |Format-Table `
-auto systemname,DeviceID,`
@{label="FreeSpace (GB)";expression={"{0:F2}" -f ($_.FreeSpace/1024/1024/1024)}},`
@{label="TotalSize (GB)";expression={"{0:F2}" -f ($_.Size/1024/1024/1024)}},`
@{label="Note";expression={if((($_.FreeSpace/1024/1024/1024) -lt 5) {"此硬盘剩余空间小于5G,请及时释放!!"} else{}}} |out-string
# 每天的8点和14点进行磁盘空间检查
$h=(get-date).hour
if(($h -eq 8) -or ($h -eq 16))
{
$emailFrom = "xx@aa.com"
$emailTo = "yy@aa.com"
$subject = "SQL Server服务磁盘空间检查"
$body = $message
$smtpServer = "192.168.3.2"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
elseif($message -match "小于5G")
{
$emailFrom = "xx@aa.com"
$emailTo = "yy@aa.com"
$subject = "磁盘空间预警"
$body="有磁盘空间小于5G,请尽快处理!!"
$body = $body+$message
$smtpServer = "192.168.3.2"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
#by obuntu
# 2010-8-23
#将F:\bk_test里的所有文件,包括目录,满足一定条件的移动到 F:\bk_test2下
$sourcePath="F:\bk_test"
$targetPath="F:\bk_test2"
$sourcePathD=$sourcePath+"\*"
$DateToCompare = (Get-date).AddDays(-7)
Copy-Item $sourcePathD $targetPath -Force
Get-ChildItem -Path $sourcePath -Recurse -include *.* |where-object {$_.lastwritetime –lt $DateToCompare}`
| Move-Item -Destination { Join-Path $targetPath $_.FullName.SubString(($
sourcePath).Length) } -Force