PowerTip of the Day from powershell.com上周汇总(六)
限制String類型參數(shù)的長度
Limiting String Input Length
http://powershell.com/cs/blogs/tips/archive/2010/08/23/limiting-string-input-length.aspx
function Get-FileName {
param(
[ValidateLength(1,8)]
[String]
$FileName
)
"Your filename {0} is {1} chars long" -f $FileName, $FileName.Length
}
調(diào)用示例Get-FileName "win.txt"
?
?
用正則驗證輸入的參數(shù)
Validate Input Using Regular Expressions
http://powershell.com/cs/blogs/tips/archive/2010/08/24/validate-input-using-regular-expressions.aspx
function Get-KnowledgeBaseArticle {
param(
[ValidatePattern('^KB\d{6}$')]
[String]
$KB
)
"You entered Knowledgebase ID $KB"
}
調(diào)用示例Get-KnowledgeBaseArticle -KB "KB123456"
?
?
用Powershell查看對象類型
Finding Object Types with Powershell
http://powershell.com/cs/blogs/tips/archive/2010/08/25/finding-object-types-with-powershell.aspx
'Hallo'.GetType().FullName
(4).GetType().FullName
(2.6).GetType().FullName
(Get-Date).GetType().FullName
結(jié)果:
System.String
System.Int32
System.Double
System.DateTime
?
?
轉(zhuǎn)換對象類型
Converting Object Types
http://powershell.com/cs/blogs/tips/archive/2010/08/26/converting-object-types.aspx
[DateTime] '4.5.2010'
以下是獲取本地日期格式的方法:
[DateTime]::Parse('4.5.2010')
實際上相當(dāng)于.net framework下的DateTime.Parst(string).
?
?
限制數(shù)字參數(shù)范圍
Restrict Input to Numeric Ranges
http://powershell.com/cs/blogs/tips/archive/2010/08/27/restrict-input-to-numeric-ranges.aspx
function Set-CursorSize {
param(
[ValidateRange(1,100)]
[Int]
$Percent
)
$Host.UI.RawUI.CursorSize = $Percent
}
對int型的參數(shù)做一個范圍限制。
主要通過[ValidateXXXX]
?
?
?
?
?
以上來自powershell.com
2010年八月份16日到20日的PowerTip of the Day
?
總結(jié)
以上是生活随笔為你收集整理的PowerTip of the Day from powershell.com上周汇总(六)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NDK/JNI01--NDK下载配置
- 下一篇: CSS规化