让powershell脚本在另外一个 powershell控制台运行的方法
Running Commands Elevated
http://powershell.com/cs/blogs/tips/archive/2010/09/27/running-commands-elevated.aspx
通过启动进程的方法
Start-Process powershell.exe -argumentlist '-command write-host "Hello world"' -verb runas
创建一个Event Logs
Creating New Event Logs
http://powershell.com/cs/blogs/tips/archive/2010/09/28/creating-new-event-logs.aspx
创建:
New-EventLog -LogName LogonScripts -Source ClientScripts
写:
Write-EventLog LogonScripts -Source ClientScripts -Message 'Test Message' -EventId 1234 -EntryType Warning
打开系统日志界面
Opening Event Log GUI
http://powershell.com/cs/blogs/tips/archive/2010/09/29/opening-event-log-gui.aspx
就一条命令:
Show-Eventlog
使用Write-Cmdlets需要注意的地方
Use Write-Cmdlets with Care!
http://powershell.com/cs/blogs/tips/archive/2010/09/30/use-write-cmdlets-with-care.aspx
下面的函数使用了两种不同的输出方法:
Function test
{
Write-Host "Result A"
"Result B"
}
当直接运行方法的时候,会得到所有的两个输出,但是write-host是直接输出到控制台的,所以这个值是无法通过变量获得的。
Test
Result A
Result B
$a = test
Result A
$a
Result B
Write-output方法
Do You Know Write-Output?
http://powershell.com/cs/blogs/tips/archive/2010/10/01/do-you-know-write-output.aspx
这个方法跟write-host不同,在函数里输出的值是可以在变量里获取到的。
Function testA
{
'My Result'
}
Function testB
{
Write-Output 'My Result'
}
Write-ouput需要注意的地方
Write-Output is Picky
http://powershell.com/cs/blogs/tips/archive/2010/10/04/write-output-is-picky.aspx
Function Convert-Dollar2EuroA($amount, $rate=0.8) {
$amount * $rate
}
Function Convert-Dollar2EuroB($amount, $rate=0.8) {
Write-Output $amount * $rate
}
Function Convert-Dollar2EuroC($amount, $rate=0.8) {
Write-Output ($amount * $rate)
}
注意其差别:
Convert-Dollar2EuroA 100
80
Convert-Dollar2EuroB 100
100
*
0,8
Convert-Dollar2EuroC 100
80
下载网页内容
Download Web Page Content
http://powershell.com/cs/blogs/tips/archive/2010/10/05/download-web-page-content.aspx
直接使用.net下的WebClient对象。
$url = 'http://blogs.msdn.com/b/powershell/'
$wc = New-Object System.Net.WebClient
$wc.DownloadString($url)
截取网页里的内容
Scraping Information from Web Pages
http://powershell.com/cs/blogs/tips/archive/2010/10/06/scraping-information-from-web-pages.aspx
通过正则表达式类:
$regex = [RegEx]'<div class="post-summary">(.*?)</div>'
$url = 'http://blogs.msdn.com/b/powershell/'
$wc = New-Object System.Net.WebClient
$content = $wc.DownloadString($url)
$regex.Matches($content) | Foreach-Object { $_.Groups[1].Value }
获取powershell团队博客的题目
Getting PowerShell Team Blog Topic Headers
还是通过正则:
$regex = [RegEx]'<span></span>(.*?)</a></h4>'
$url = 'http://blogs.msdn.com/b/powershell/'
$wc = New-Object System.Net.WebClient
$content = $wc.DownloadString($url)
$regex.Matches($content) | Foreach-Object { $_.Groups[1].Value }
使用Continue
Using 'Continue'
http://powershell.com/cs/blogs/tips/archive/2010/10/08/using-continue.aspx
温习循环结构中的Continue的用法。
For($x=1; $x -lt 20; $x++) {
If ($x % 4) { continue }
$x
}
以上来自powershell.com
2010年九月25日到十月8日的PowerTip of the Day