Node red Exec Powershell doesn't work

I'm having a problem executing a Powershell file.
When I execute the Powershell file in the CMD everything works correctly. if I want to run the same file with node red, the application runs dead.

I have 2 scripts, one of which I send in a Webex chat that works

and the other script is supposed to send an email that doesn't work.

Can someone help me ???

How are you running Node-RED ? Are you running it in the power shell environment ?

Hello,
I call the Powershell Script in an Exec node

Well this worked:

image

Try using the shortcut to the powershell executable, your long version probably needs quotes around it. Also check whether the E: drive is actually accessible from Node-RED.

Hello,
pwsh -c "write-host hello"
that does not work. Error -4058 is displayed.

In the Task Manager I see Outlook and Powershell open.
But it doesn't go any further at this point.

I have other Powershell scripts on drive E that work, only when Outlook is called it doesn't work.
On my home PC, it just doesn't work on the company computer?

I am at a loss

here is my flow
I create the Powershell file completely inNode-red

[{"id":"b632128e.f06da","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"c0046030.63221","type":"function","z":"b632128e.f06da","name":"Powershell E-Mail Versand","func":"var input= msg.payload;\n\n\n\nvar send='param($To = \"chris71@test.com\",$Subject =\"Test Node Red\",$Importance=\"1\")' + \"\\n\" + \n'$htmlBody = \"<h5>'+ input +'Hello\"' + \"\\n\" +\n'Function Send-Mail($Importance, $Subject, $Body, $To, $CC, $Source, $BoolAttachments, [Array]$Dateien) '  + \"\\n\" + \n'{'  + \"\\n\" + \n'Try {'+ \"\\n\" +\n'$o = New-Object -ComObject Outlook.Application' + \"\\n\" + \n\n        '$mail = $o.CreateItem(0)'+ \"\\n\" + \n        '$mail.importance = \"$Importance\"'   + \"\\n\" +    \n        '$mail.subject = \"$Subject\"'    + \"\\n\" + \n        '$mail.HTMLbody = \"$Body\"'+ \"\\n\" + \n        '$mail.To = \"$To\"'        + \"\\n\" +               \n        '$mail.CC = \"$CC\"'     + \"\\n\" +                  \n        '$mail.Send()' + \"\\n\" + \n'Start-Sleep 5'+ \"\\n\" + \n'$o.Quit()'+ \"\\n\" + \n'}'+ \"\\n\" +\n'catch {'+ \"\\n\" +\n'$temp = $_.Exception'+ \"\\n\" +\n'$invocationInfo = $_.InvocationInfo.PositionMessage'+ \"\\n\" +\n'if ($_.InvocationInfo -ne $null) {'+ \"\\n\" +\n'write-host $temp'+ \"\\n\" +\n'}}}'+ \"\\n\"+\n\n'function GetFiles( $files )'+ \"\\n\" +\n'{'+ \"\\n\" +\n    '$outfileName = $files[$i].Name'+ \"\\n\" +\n    '$outfileNameFullname = $files[$i].FullName'+ \"\\n\" +\n    '$outfileNameExtension = $files[$i].Extension'+ \"\\n\" +\n \n    'Foreach ( $f in $Dateien) {'+ \"\\n\" +\n        'if($outfileName -eq $f)'+ \"\\n\" +\n        '{'+ \"\\n\" +\n            '$mail.Attachments.Add($outfileNameFullname);'+ \"\\n\" +\n        '}}}'+ \"\\n\" +\n        \n'Send-Mail   -Importance $Importance `'+ \"\\n\" +\n            '-Subject $Subject `'+ \"\\n\" +\n            '-Body $htmlBody `'+ \"\\n\" +\n            '-To $To `'+ \"\\n\" +\n            '-CC $cc `'+ \"\\n\" +\n            '-Source $FilePath `'+ \"\\n\" +\n            '-BoolAttachments $BoolAttachments `'+ \"\\n\" +\n            '-Dateien @($Files)'+ \"\\n\" \n\n\n\n\n\nmsg.payload=send;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":460,"wires":[["ae95f9c5.4e7348"]]},{"id":"ae95f9c5.4e7348","type":"file","z":"b632128e.f06da","name":"","filename":"E:\\WebexBot\\Mehrfach\\MehrfachLack3.ps1","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":950,"y":400,"wires":[[]]},{"id":"11e26c2b.cfb3b4","type":"exec","z":"b632128e.f06da","command":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -executionpolicy bypass \"C:\\Users\\weblack\\Desktop\\MehrfachLack3.ps1\"","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":640,"y":260,"wires":[["40db19e1.b24408"],["40db19e1.b24408"],["40db19e1.b24408"]]},{"id":"547f44df.602bdc","type":"inject","z":"b632128e.f06da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"732946G","payloadType":"str","x":260,"y":480,"wires":[["c0046030.63221"]]},{"id":"9bfdd37.b0ec63","type":"inject","z":"b632128e.f06da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":180,"wires":[["11e26c2b.cfb3b4"]]},{"id":"40db19e1.b24408","type":"debug","z":"b632128e.f06da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1170,"y":100,"wires":[]}]

Why not simply use the node-red-node-email - it works very well - no spawning powershell tasks.

I cannot run any other e-mail program on my company PC, it does not work because of the firewall.
It's only Outlook.

node-red-node-email is not an email program. It is a node (just like the exec node or the inject node) that you add to node-red that permits you to sends emails to your email server without having to automate outlook.

Ask your IT dept for the possibility of sending automated Emails via SMTP they may or may not already have it setup & permit you to use it.

pwsh requires PowerShell Core v7 to be installed. I think that you can just use powershell to run PS v5 that is installed with Windows 10.

I think that you missed telling us some important information previously. When working with Office, there are many more things that could be wrong. You might need to share the PS script (minus any sensitive info of course).

Have you even got Outlook installed on your home PC? Is it configured the same as your work PC?

You should be getting some kind of error returned? You might need to adjust the script to output errors to a file.

Also, what method are you using to try to send the email, some of them only work with Office 365 accounts?

I have just seen I use Outlook 2016 on the company PC, and Office 365 at home.

I only get an error when I finish the Outlook task. Then there is an error message:

System.Management.Automation.MethodInvocationException: Exception when calling "CreateItem" with 1 argument (s): "The remote procedure call failed. (Exception from HRESULT: 0x800706BE)" ---> System.Runtime.InteropServices.COMException: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
at Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem (OlItemType ItemType)
at CallSite.Target (Closure, CallSite, Object, Int32)
--- End of internal exception batch monitoring ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference (FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run (InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run (InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run (InterpretedFrame frame)

There is an easy way to send an email when using O365 - you may need to do something to detect what environment you are using:

<# SMTP Authenticated Email Submission
   Send email is via SMTP Client Submission or SMTP Authenticated Submission. 
   Using PowerShell’s Send-MailMessage cmdlet, we can provide all of the parameters 
   we need to send email through Office 365.
   Requires an Office 365 account and Exchange Online mailbox for that account
   @see https://adamtheautomator.com/office-365-direct-send-smtp/
#>

# Get the credential
$credential = Get-Credential

## Define the Send-MailMessage parameters
$mailParams = @{
    SmtpServer                 = 'smtp.office365.com'
    Port                       = '587' # or '25' if not using TLS
    UseSSL                     = $true
    Credential                 = $credential
    From                       = 'someone@something.com'
    To                         = 'recipient@yourdomain.com', 'recipient@NotYourDomain.com'
    Subject                    = "SMTP Client Submission - $(Get-Date -Format g)"
    Body                       = 'This is a test email using SMTP Client Submission'
    DeliveryNotificationOption = 'OnFailure', 'OnSuccess'
}

## Send the message
Send-MailMessage @mailParams

Oddly enough, I had cause to test that yesterday :slight_smile:

It works with Office 365.
only not with Office 2016. :sob:

Yes, your script needs to detect what environment it is in and process accordingly.

An Exchange server is used in the company.
I have no idea how to do it.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.