PowerTip of the Day from powershell.com上周汇总(八)

查找WMI实例的路径

Finding WMI Instance Path Names

http://powershell.com/cs/blogs/tips/archive/2010/09/07/finding-wmi-instance-path-names.aspx

通过路径名不仅可以创建WMI实例,也可以通过WMI实例查找路径名信息。

Get-WMIObject Win32_Share | Select-Object __Path

需要注意的是path前面是两个下划线

 

 

通过类型转换访问事件日志

Accessing Event Logs via Conversion

http://powershell.com/cs/blogs/tips/archive/2010/09/08/accessing-event-logs-via-conversion.aspx

神奇的类型转换在powershell中使代码对日志的访问变得很容易

[System.Diagnostics.EventLog]"System"

 

 

链式类型转换

Counting Special Characters

http://powershell.com/cs/blogs/tips/archive/2010/09/10/counting-special-characters.aspx

Powershell中,可以将类型链式的转换多次,比如将一个字符串转换成char[]然后再转换成Byte[]

[Byte[]][Char[]]"Hello World!"

这种方法实用之处在于,可以将文本以二进制的方式写入注册表,比如:

Md HKCU:\Software\Test

Set-ItemProperty HKCU:\Software\Test ProductName -Type Binary -Value ([Byte[]][Char[]]"MyProduct") ReTweet

 

 

查找指定字符的数量

Counting Special Characters

http://powershell.com/cs/blogs/tips/archive/2010/09/10/counting-special-characters.aspx

通过类型转换的方式也可以帮助我们数某个字符在文本中出现的次数。

@([Byte[]][char[]]$text -eq 9).Count

这行代码会首先把$text里的文本转换成byte[],然后过滤里面字符编码是9(tab)的字符。然后,@确保结果是个数组,这样count就可以查出符合条件的字符数量。

 

 

 

以上来自powershell.com

2010年九月份7日到10日的PowerTip of the Day

posted @ 2010-09-14 11:11  哥本哈士奇(aspnetx)  阅读(396)  评论(0编辑  收藏  举报