Windows PowerShell
02/24/2010 08:14 by cince Original
在Win7下只要右击脚本文件,选择Run with PowerShell,就会自动找到最占内存的10个进程,然后将它们占用的内存画成一个三维饼图,如下图所示.

1 # create new excel instance
2 $objExcel = New-Object -comobject Excel.Application
3 $objExcel.Visible = $True
4 $objWorkbook = $objExcel.Workbooks.Add()
5 $objWorksheet = $objWorkbook.Worksheets.Item(1)
6
7 # write information to the excel file
8 $i = 0
9 $first10 = (ps | sort ws -Descending | select -first 10)
10 $first10 | foreach -Process {$i++; $objWorksheet.Cells.Item($i,1) = $_.name; $objWorksheet.Cells.Item($i,2) = $_.ws}
11 $otherMem = (ps | measure ws -s).Sum - ($first10 | measure ws -s).Sum
12 $objWorksheet.Cells.Item(11,1) = "Others"; $objWorksheet.Cells.Item(11,2) = $otherMem
13
14 # draw the pie chart
15 $objCharts = $objWorksheet.ChartObjects()
16 $objChart = $objCharts.Add(0, 0, 500, 300)
17 $objChart.Chart.SetSourceData($objWorksheet.range("A1:B11"), 2)
18 $objChart.Chart.ChartType = 7019 $objChart.Chart.ApplyDataLabels(5)
1. 这个脚本调用了Excel的COM库。
2. 当然从命令耦合的角度来看,输出成文本格式更有利,但这个例子主要想说明PowerShell的强大以及微软产品优异的复用性。

Cmdlet + Regex + Pipeline + ...
比如当键入get-process查看当前进程列表时,系统返回的是这样的列表:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
318 8 12948 3872 84 1728 AppleMobileD
115 5 13816 13328 38 6920 audiodg
1315 21 11732 10988 108 2544 CcmExec
... ...
虽然看似一般的格式化文本,但其实这是一个数组,而每个数组元素又是Process类型的对象。同.NET一脉相承,PowerShell中的所有的类都继承自Object,且支持GetType()函数。因此我们可以执行(get-process).GetType()来看看它的类型:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
而数组中每个元素的类型可以用(get-process)[0].GetType()查看:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False Process System.ComponentM...
无缝调用.NET/COM
.NET Framework中包含了一个异常强大的库,而微软为了保证二进制层面上跨语言的兼容性,很多库都是用COM封装的。PowerShell的一大特色就是可以直接调用这些库。比如前面的示例用$objExcel = New-Object -comobject Excel.Application创建了一个Excel对象。而wikipedia上的一个脚本更示范了这种无缝调用的强大。下面这个3句话的脚本的作用是显示一个RSS源最近的8篇文章的标题。注意其中网络连接,内容下载,XML解析等工作全部由.NET库完成,正因为站在巨人的肩膀上,PowerShell在实际使用中往往左右逢源,简洁高效。
$rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"$blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)$blog.rss.channel.item | select title -first 8
编辑,运行,调试 - IDE
Windows程序开发,尤其是基于微软技术的开发很爽的一点就是有强大的IDE和专业的文档作支持。不论是Windows下的Visual Studio还是Linux下的Mono Develop,甚至连PowerShell这样的语言都有集编辑与调试为一体的IDE:Windows PowerShell ISE。
1 # create new excel instance
2 $objExcel = New-Object -comobject Excel.Application
3 $objExcel.Visible = $True
4 $objWorkbook = $objExcel.Workbooks.Add()
5 $objWorksheet = $objWorkbook.Worksheets.Item(1)
6
7 # write information to the excel file
8 $i = 0
9 $first10 = (ps | sort ws -Descending | select -first 10)
10 $first10 | foreach -Process {$i++; $objWorksheet.Cells.Item($i,1) = $_.name; $objWorksheet.Cells.Item($i,2) = $_.ws}
11 $otherMem = (ps | measure ws -s).Sum - ($first10 | measure ws -s).Sum
12 $objWorksheet.Cells.Item(11,1) = "Others"; $objWorksheet.Cells.Item(11,2) = $otherMem
13
14 # draw the pie chart
15 $objCharts = $objWorksheet.ChartObjects()
16 $objChart = $objCharts.Add(0, 0, 500, 300)
17 $objChart.Chart.SetSourceData($objWorksheet.range("A1:B11"), 2)
18 $objChart.Chart.ChartType = 7019 $objChart.Chart.ApplyDataLabels(5)
1. 这个脚本调用了Excel的COM库。
2. 当然从命令耦合的角度来看,输出成文本格式更有利,但这个例子主要想说明PowerShell的强大以及微软产品优异的复用性。
Cmdlet + Regex + Pipeline + ...
比如当键入get-process查看当前进程列表时,系统返回的是这样的列表:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
318 8 12948 3872 84 1728 AppleMobileD
115 5 13816 13328 38 6920 audiodg
1315 21 11732 10988 108 2544 CcmExec
... ...
虽然看似一般的格式化文本,但其实这是一个数组,而每个数组元素又是Process类型的对象。同.NET一脉相承,PowerShell中的所有的类都继承自Object,且支持GetType()函数。因此我们可以执行(get-process).GetType()来看看它的类型:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
而数组中每个元素的类型可以用(get-process)[0].GetType()查看:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False Process System.ComponentM...
无缝调用.NET/COM
.NET Framework中包含了一个异常强大的库,而微软为了保证二进制层面上跨语言的兼容性,很多库都是用COM封装的。PowerShell的一大特色就是可以直接调用这些库。比如前面的示例用$objExcel = New-Object -comobject Excel.Application创建了一个Excel对象。而wikipedia上的一个脚本更示范了这种无缝调用的强大。下面这个3句话的脚本的作用是显示一个RSS源最近的8篇文章的标题。注意其中网络连接,内容下载,XML解析等工作全部由.NET库完成,正因为站在巨人的肩膀上,PowerShell在实际使用中往往左右逢源,简洁高效。
$rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"$blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)$blog.rss.channel.item | select title -first 8
编辑,运行,调试 - IDE
Windows程序开发,尤其是基于微软技术的开发很爽的一点就是有强大的IDE和专业的文档作支持。不论是Windows下的Visual Studio还是Linux下的Mono Develop,甚至连PowerShell这样的语言都有集编辑与调试为一体的IDE:Windows PowerShell ISE。
COMMENT[0]

