Problem with powershell script in API

Have this little function that i use everywhere… i’ve tested this code from a remote session to the server and it works but every time in run it from the api it returns the following:
OperationStopped:
Line |
9 | Send-MailMessage -SmtpServer $smtp -To $to -From $from -Subje …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Object reference not set to an instance of an object.

i’ve verified using wait-debugger that all of the variables are popultated at the time that the send-mailmessage command is issued and if i copy and paste that line of coding during a debugging session it works fine.

The code used is below:
function emailnotice($access, $userid){
$smtp = “mailservername.domain.com
#$to = $id
$to = “recipient@domain.com
$from = “sender@domain.com
$subject = “New access has been granted to: $userid”
$emailbody = “Instructions for use will be provided at a later time.” + $access
Send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $emailbody
}
$id = “username”
$newAccess = “GeneratedAccess”
emailnotice $newAccess $id

Anyone have any thoughts or suggestions?

Figured it out… in version 7 of powershell send-mailmessage is depreciated and generates a warning message… using the “-WarningAction SilentlyContinue” parameter got it working.

1 Like