# Node red Exec Powershell doesn't work

**URL:** https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998
**Category:** General
**Created:** [16 November 2020 12:08 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998 "2020-11-16T12:08:30Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [16 November 2020 12:08 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/1 "2020-11-16T12:08:30Z")

</div>

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 ???

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [16 November 2020 12:31 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/2 "2020-11-16T12:31:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [16 November 2020 16:27 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/3 "2020-11-16T16:27:05Z")

</div>

Hello,  
I call the Powershell Script in an Exec node

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [16 November 2020 16:28 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/4 "2020-11-16T16:28:34Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4f8e06a4b0be6b2d4b0f9c91e2d0befd122f42f.png)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 November 2020 00:10 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/5 "2020-11-17T00:10:00Z")

</div>

Well this worked:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f70969b2b420b309e8a395b7474a5dbc11a9b6f.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/0878250d17ebb0c9fa6c910646f30a93f94ff1ef.png)

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.

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [17 November 2020 07:27 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/6 "2020-11-17T07:27:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [17 November 2020 07:55 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/7 "2020-11-17T07:55:54Z")

</div>

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 November 2020 08:22 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/8 "2020-11-17T08:22:37Z")

</div>

> [@ChrisWeb71](#):
>
> and the other script is supposed to send an email that doesn't work.

Why not simply use the [node-red-node-email](https://flows.nodered.org/node/node-red-node-email) - it works very well - no spawning powershell tasks.

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [17 November 2020 09:52 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/9 "2020-11-17T09:52:58Z")

</div>

> [@Steve-Mcl](#):
>
> Why not simply use the [node-red-node-email](https://flows.nodered.org/node/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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 November 2020 09:56 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/10 "2020-11-17T09:56:28Z")

</div>

> [@ChrisWeb71](#):
>
> I cannot run any other e-mail program on my company PC

[node-red-node-email](https://flows.nodered.org/node/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.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 November 2020 16:50 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/11 "2020-11-17T16:50:08Z")

</div>

> [@ChrisWeb71](#):
>
> pwsh -c "write-host hello"  
> that does not work. Error -4058 is displayed.

`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.

> [@ChrisWeb71](#):
>
> I see Outlook and Powershell open

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?

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [18 November 2020 06:41 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/12 "2020-11-18T06:41:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [18 November 2020 06:54 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/13 "2020-11-18T06:54:19Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 November 2020 09:21 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/14 "2020-11-18T09:21:41Z")

</div>

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:

```auto
<# 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 🙂

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [18 November 2020 11:08 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/15 "2020-11-18T11:08:35Z")

</div>

It works with Office 365.  
only not with Office 2016. 😭

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 November 2020 13:27 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/16 "2020-11-18T13:27:48Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ChrisWeb71](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@ChrisWeb71](https://discourse.nodered.org/u/ChrisWeb71)
#### Post date: [18 November 2020 15:45 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/17 "2020-11-18T15:45:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 January 2021 15:45 UTC](https://discourse.nodered.org/t/node-red-exec-powershell-doesnt-work/35998/18 "2021-01-17T15:45:22Z")

</div>

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