2017-07-26 10 views
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" 

} 

答えて

3
私もやる傾向にある

よくある間違いは、何をやっていることはこれです:

これは変数 $WhichNotificationに「doSomethingの」設定することでどういう
if ($WhichNotification = "dosomething") 

- で$trueに評価され、何かをifブロック。あなたが何をしたいか

はこれです:

if ($WhichNotification -eq "dosomething") 
関連する問題