0
我剛剛跳入PowerShell,並試圖編寫一個腳本,該腳本執行基於調用哪個參數而創建的函數。使用參數調用基於哪個參數調用函數的Powershell
例:發送通知-WhichNotifcation DoSomething的
例2:發送通知-WhichNotification dosomethingelse
我現在只調用第一個函數,但從來沒有第二個。我究竟做錯了什麼?
param(
[parameter(Mandatory = $true)]
[ValidateSet("dosomething", "dosomethingelse")]
[ValidateNotNull()]
[string]$WhichNotification
)
#Variables
$mailTo = "[email protected]"
$mailFrom = "[email protected]"
$smtpServer = "x.x.x.x"
$mailSubject1 = "Do Something"
$MailSubject2 = "do something else"
function Send-dosomething
{
Send-MailMessage -To $mailTo -From $mailFrom -SmtpServer $smtpServer -Subject $mailSubject1 -Body "message1"
}
function Send-dosomethingelse
{
Send-MailMessage -To $mailTo -From $mailFrom -SmtpServer $smtpServer -Subject $MailSubject2 -Body "message2"
}
if ($WhichNotification = "dosomething") {
Send-dosomething
}
elseif ($WhichNotification = "dosomethingelse") {
Send-dosomethingelse
}
else {
Write-Host "Invalid"
}