利用Powershell来管理服务器

自己使用Powershell的目的是为了更方便的管理与SQL Server相关,所以就顺便发到SQL Server版本,希望对大家有所启发。

Powershell 是微软开发的windows平台上的新脚本语言,目的在于实现类似于bash 等方便管理windows 系统的功能,为系统管理员带来更大的方便。在实际应用中自己也尝试着去学习和应用powershell,经过一段时间的摸索,发现其功能确实强大,能够在windows平台上灵活的管理文件系统、注册表、进程、服务、SQL Server、exchange等,而且容易上手,简化了很多管理的方式。

一、Powershell的资源

网上有很多Powershll的优秀资源,顺便跟大家分享下
1,微软的Powershll官方网站
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx
要想了解一项新技术,最好的资源莫过于官方网站了,有一些非常好的入门资料,值得一看。

2,powershell.com
http://powershell.com/cs/
这是idera公司维护的一个关于powershell的学习网站,里面有很多非常好的实践和问题解决方案,能够提供很好的启发。

3,simple-talk 网站

http://www.simple-talk.com/search/default.aspx?search=powershell
simple- talk是redgate维护的一个知识网站,里面网罗了很多数据库和其他技术的大师(如Joe Celko),是一个非常好的知识库。最早引起自己对Powershell兴趣的就是看了上面地址的一系列利用powershell管理SQL Server的文章。

4,电子书
首推下述两本
1, 《Manning.Power.Shell.in.Practice.Jun.2010》 从书名也可以看出具有很强的实践性
2, 《OReilly.Windows.PowerShell.Cookbook.Oct.2007》OReilly出这种语言书也很具含金量

不过都是英文版。

二、注意事项

Powershell目前已经发展到了V2 版本,在安装SQL Server 2008时,会自动安装V1,但有很大的功能限制,为了获得更好的功能,尽量考虑使用V2。下载地址为:
http://support.microsoft.com/kb/968929

三、Powershell 和SQL Server

在使用SQL Server 2008时,我们会发现自动安装了POWERSHELL。说明POWERSHELL已经和SQL Server进行了很好的融合,要通过powershell来管理SQL Server主要是通过直接操作SMO(SQL Server Mnagement Object)来实现。但平时应用的不多,所以未对其过多深入,有兴趣的朋友可以参考下联机丛书


四、自己的Powershell实践

像我在管理SQL Server时,最经常遇到的需求是:
1,如何获得windows的事件查看器里的警告和错误信息,并发送警告,
2,如何获得SQL Server相关服务的运行状态,并发送警告;
3,如何获得SQL Server服务器的空间信息,并发送警告;
4,如何移动历史备份文件到新的目录,防止做磁带备份时产生冗余。

当然,上述内容,通过SQL Server 编写T-SQL应该也能实现,但相对而言会复杂很多,而通过POWERSHELL的话,有些甚至只需要几行语句就可以了。

1,如何获得windows的事件查看器里的警告和错误信息,并发送警告,

Python code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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)
}

2,如何获得SQL Server相关服务的运行状态,并发送警告;

Python code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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)
}


3,如何获得SQL Server服务器的空间信息,并发送警告;

Python code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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)
  }

 
4,如何移动历史备份文件到新的目录,防止做磁带备份时产生冗余。

Python code
?
1
2
3
4
5
6
7
8
9
10
11
#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


五、小结

只是单纯介绍一些POWERSHELL的实际应用,对一些语法并未进行介绍,但如果想了解一个命令的话,有2种方式,如想要了解Copy -Item的用法,可以用Get-Help Copy-Item 和 Copy-Item | Get-Member 。

上述脚本,可以使用任务计划运行,也可以通过SQL Server创建作业来运行,其实也包含了基本的监控雏形。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值