删除错误记录
Deleting Error Records
http://powershell.com/cs/blogs/tips/archive/2010/10/11/deleting-eating-error-records.aspx
Trap { "Whew, an error: $_"; continue } 1/$null
如果不加continue,红色的错误信息会显示出来。
改变ErrorAction
Change ErrorAction for Your Private Error Handling
比如:
$local:ErrorActionPreference = "Stop"
$local:ErrorActionPreference = "SilentlyContinue"
使用Break
Do You Use "Break"?
http://powershell.com/cs/blogs/tips/archive/2010/10/13/do-you-use-quot-break-quot.aspx
作用,直接终止循环。
Do {
$pwd = Read-Host 'Enter your password'
If ($pwd -eq 'topsecret') { break }
} While ($true)
理解变量继承(作用域)
Understanding Variable Inheritance
http://powershell.com/cs/blogs/tips/archive/2010/10/14/understanding-variable-inheritance.aspx
$a = 1
Function test {
"variable a contains $a"
$a = 2
"variable a contains $a"
}
test
variable a contains 1
variable a contains 2
然后输入
$a
输出的结果是
1
变量可以从父级继承过来,但是如果在自己的作用域再声明的话,那么它们就不是一个变量了。
创建静态变量
Creating "Static" Variables
http://powershell.com/cs/blogs/tips/archive/2010/10/15/creating-quot-static-quot-variables.aspx
Function test {
$script:nestlevel += 1
If ($script:nestlevel -gt 10) { break }
"I am at nest level $script:nestlevel"
Test
}
test
代码用到了递归。每次迭代访问静态变量。
使用私有变量
Working with Private Variables
http://powershell.com/cs/blogs/tips/archive/2010/10/18/working-with-private-variables.aspx
变量可以向下继承,即子方法可以调用父级代码的变量。如果想要避免这种调用关系的话,需要用到私有变量。
$private:a = 1
Function test {
"variable a contains $a"
$a = 2
"variable a contains $a"
}
test
variable a contains
variable a contains 2
$a
1
可以看出父级私有变量a在方法里并没有被访问到。
改变控制台颜色
Changing Console Colors
http://powershell.com/cs/blogs/tips/archive/2010/10/19/changing-console-colors.aspx
$host.UI.RawUI.BackgroundColor = 'Blue'
[System.Console]::BackgroundColor = 'Blue'
重置控制台颜色
Resetting Console Colors
http://powershell.com/cs/blogs/tips/archive/2010/10/20/resetting-console-colors.aspx
改变颜色:
[System.Console]::BackgroundColor = 'Blue'
[System.Console]::ForegroundColor = 'Yellow'
Clear-Host
恢复颜色:
[System.Console]::ResetColor()
Clear-Host
清空控制台内容
Clearing Console Content
http://powershell.com/cs/blogs/tips/archive/2010/10/21/clearing-console-content.aspx
可以用cls和Clear-Host。但是这两种方法性能不高。
get-command clear-host | Select-Object -expand Definition
$space = New-Object System.Management.Automation.Host.BufferCell
$space.Character = ' '
$space.ForegroundColor = $host.ui.rawui.ForegroundColor
$space.BackgroundColor = $host.ui.rawui.BackgroundColor
$rect = New-Object System.Management.Automation.Host.Rectangle
$rect.Top = $rect.Bottom = $rect.Right = $rect.Left = -1
$origin = New-Object System.Management.Automation.Host.Coordinates
$Host.UI.RawUI.CursorPosition = $origin
$Host.UI.RawUI.SetBufferContents($rect, $space)
重写Clear-Host函数,性能就提高多了:
Function Clear-Host { [System.Console]::Clear() }
显示可以字符集(?)
Listing Available Culture IDs
http://powershell.com/cs/blogs/tips/archive/2010/10/22/listing-available-culture-ids.aspx
[System.Globalization.CultureInfo]::GetCultures('WindowsOnlyCultures')
也可以指定控制台使用何种字符集。
$c = [System.Globalization.CultureInfo]'ar-IQ'
[System.Threading.Thread]::CurrentThread.CurrentCulture = $c; Get-Date
把字符集转换成国家名
Translating Culture IDs to Country Names
http://powershell.com/cs/blogs/tips/archive/2010/10/25/translating-culture-ids-to-country-names.aspx
[System.Globalization.CultureInfo]::GetCultureInfoByIetfLanguageTag('ar-IQ')
显示结果:
LCID Name DisplayName
---- ---- -----------
2049 ar-IQ Arabic (Iraq)
执行程序并且保持联系(即对它的控制)
Launching Programs and Keeping in Touch
http://powershell.com/cs/blogs/tips/archive/2010/10/26/launching-programs-and-keeping-in-touch.aspx
Start-Process加上-passThru参数就可以让其返回一个Process对象的引用,这样后面就可以控制这个进程。
$process = Start-Process notepad.exe -passThru
$process
$process.Kill()
以提升模式(?)运行程序
Run Programs Elevated
http://powershell.com/cs/blogs/tips/archive/2010/10/27/run-programs-elevated.aspx
当UAC开启时,想要以提升模式运行程序。
Start-Process powershell -verb runas
使用不同的用户运行程序
Running Programs as Different User
http://powershell.com/cs/blogs/tips/archive/2010/10/28/running-programs-as-different-user.aspx
Start-Process powershell -LoadUserProfile -Credential (Get-Credential)
会弹出界面提示以哪个用户运行。
根据关键字查找方法
Finding Methods with Specific Keywords
http://powershell.com/cs/blogs/tips/archive/2010/10/29/finding-methods-with-specific-keywords.aspx
$key = 'Kill'[System.Diagnostics.Process].Assembly.GetExportedTypes() | Where-Object { $_.isPublic}| Where-Object { $_.isClass } | Where-Object { @($_.GetMethods() | Where-Object { $_.Name -like "*$key*" }).Count -gt 0 } | Select-Object -expandProperty FullName
感觉不如直接查MSDN的方便。
---------------------------------------------------------------
aspnetx的BI笔记系列索引:
在Silverlight下用Visifire显示多维数据集中的数据
在Silverlight下用Visifire显示多维数据集中的数据(二)
---------------------------------------------------------------