0

I am just jumping into PowerShell and am trying to write a script that executes functions I have created based on which parameter is called.

Example: send-notification -WhichNotifcation dosomething

Example2: send-notification -WhichNotification dosomethingelse

What I have now only calls the first function, but never the second. What am I doing wrong?

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"

}

1 Answer 1

4

Common mistake which I also tend to do, what you are doing is this:

if ($WhichNotification = "dosomething") 

What this does is to set the variable $WhichNotification to "dosomething" - something that evaluates to $true in an if-block.

What you want to do is this:

if ($WhichNotification -eq "dosomething") 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.