![](/img/trans.png)
[英]Powershell using “at-run” parameters to call functions based on which parameter is called
[英]Powershell using parameters to call functions based on which parameter is called
我只是跳入PowerShell,并试图编写一个脚本,该脚本执行我根据调用的参数创建的功能。
示例:send-notification -WhichNotifcation dosomething
示例2:send-notification -WhichNotification dosomethingelse
我现在所拥有的只是第一个函数,而没有第二个。 我究竟做错了什么?
param(
[parameter(Mandatory = $true)]
[ValidateSet("dosomething", "dosomethingelse")]
[ValidateNotNull()]
[string]$WhichNotification
)
#Variables
$mailTo = "user@something.com"
$mailFrom = "user@somethingelse.com"
$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"
}
我也倾向于做的常见错误,您正在做的是:
if ($WhichNotification = "dosomething")
这样做是将变量$WhichNotification
设置为“ $WhichNotification
”-在if块中的结果为$true
。
您要做的是:
if ($WhichNotification -eq "dosomething")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.