有沒有辦法在PowerShell函數中創建一個必須調用它才能考慮它的參數?使用可選的調用變量創建一個函數
通過命令行給出一個例子(大膽的是什麼,我想這樣做):
Invoke-Command
-computername
Server01
-Scriptblock
{...}
這裏是什麼,我想用的功能做榜樣
Function DoStuff($computername, -arg2, -domain $domain)
Test-parameter(-domain) if (-domain -eq $true) {
use $domain
}
Else {
$domain = "Domain1"
}
test-parameter($arg2) {
if ($arg2 -eq $true) {
Do something
}
else {
Do the opposite
}
}
綜上所述:
如果「-arg2」存在,我希望在腳本中發生一些事情。如果「-Domain」存在並且有它的一個參數,我希望使用它而不是set參數。
完美由於一噸! – user100541
請注意,'[string]'參數初始化爲空字符串,不是'$ null'。他們必須用'-eq'''進行測試 – helb