2
我在寫一些基本的powershell庫,我需要檢查一個特定的參數是否在一組值中。函數參數檢查
在這個例子中,我定義了一個帶有可選參數的函數ALV_time。如果定義,它可能只有2個值,否則我發出警告。它可以工作,但是這是僅允許一些參數值的正確方法,還是存在標準方法?
$warningColor = @{"ForegroundColor" = "Red"}
function AVL_Time {
[CmdletBinding()]
param (
$format
)
process {
# with format parameter
if ($format) {
# list format possible parameters
$format_parameters = @("short: only date", "long: date and time")
if ($format -like "short") {
$now = Get-Date -Format "yyyy-MM-dd"
}
# long date
elseif ($format -like "long") {
$now = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
# if wrong format parameter
else {
Write-Host @warningColor "Please use only those parameters:"
$format_parameters | foreach {
Write-Host @warningColor "$_"
}
}
}
# without format parameter
else {
$now = Get-Date -Format "yyyy-MM-dd"
}
# return time
return $now
}
}