SOLVED

Weird problem running simple script between Powershell ISE and Powershell

Iron Contributor

Hi,

 

I'm new to powershell and I've been developing some simple scripts to check stuff in our internal network. Now I want to set a task to run a script every morning at 9am. So I got the script ready and tested, and configured the task schedule pointing to that script and run it, but there is not execution.
So I started to read about possible debug for my case. While reading, I've decided to write a very simple script, just to check if it would run from the task scheduler, and it doesn't run.

 

This is the script:

 

# Write date to a txt file
get-date > C:\Temp\debug.txt
# Open a pop-up window
powershell -WindowStyle hidden -Command "& {[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Teste','WARNING')}"

 

If I run this script from Powershell ISE everything goes well, file is created and the pop-up opens.

If I run this script directly from powershell using "powershell c:\Temp\Test.ps1" it opens the pop-up window, but the file is not created.

If I run this script from task scheduler nothing happens, even though the event says it ran.

So I'm really missing something here.


Can anyone point me out in the correct direction?

Thanks

5 Replies
Added a start-transcript c:\temp\log.txt to the script, this is the logging when running from a Scheduled Task:

Host Application: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe c:\temp\test.ps1
Process ID: 25868
PSVersion: 5.1.22000.588
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22000.588
BuildVersion: 10.0.22000.588
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is c:\temp\log.txt

GAC Version Location
--- ------- --------
True v4.0.30319 C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089...
Exception calling "Show" with "2" argument(s): "Showing a modal dialog box or form when the application is not running
in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to
display a notification from a service application."
At line:1 char:79
+ ... ws.Forms'); [System.Windows.Forms.MessageBox]::Show('Teste','WARNING' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
**********************
Windows PowerShell transcript end
End time: 20220317185229
**********************
Hi,

Thanks. I didn't knew the start-transcript command. Now I've added that into my real powershell script (not this test script), and I've got the following error on the log file.
"PS>TerminatingError(Send-MailMessage): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Unable to read data from the transport connection: net_io_connectionclosed."
Error: Could not send email to Email address removed via smtp.office365.com"

I've searched for this error and it's related to TLS. But can't find a solution to overcome it. Do you have any idea on how to fix this issue?

Thanks
best response confirmed by dmarquesgn (Iron Contributor)
Solution

@dmarquesgn You can use this at the start of the script to set TLS to 1.2 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Cool, now it works just as supposed.
Thanks for the guidance.
No problem, glad it works now!
1 best response

Accepted Solutions
best response confirmed by dmarquesgn (Iron Contributor)
Solution

@dmarquesgn You can use this at the start of the script to set TLS to 1.2 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

View solution in original post