r/PowerShell • • Jul 04 '18

Solved Why does my powershell script stop immediantely after being opened...?

New thread is here: https://www.reddit.com/r/PowerShell/comments/8wceh3/why_does_my_script_work_in_iseps_command_but_not/

It used to work but now it suddently terminates less than a second after being opened. I set the execution policy to unrestricted & re-installed Windows yet it still does not work...

The powershell shows up for 1 second in task manager before windows security health service when it's run using .vbs & then disappears: https://i.imgur.com/VNX7NKx.png

When I paste the script code into PowerShell ISE it works, why is this?

Just to clarify a few things:

The .ps1 file worked with task scheduler (it would load a console window before the message showing up) & when I clicked on the .ps1 file it would also load a console window before the message showed up. When I moved to VB it worked when I clicked on the VB file & when I used the VB file with task scheduler; both without the console window popping up before the message showed.

It still works with VB but only if I create a new user account then after like a day or two it stops working...

Here's what the properties of the task look like (when it worked): https://i.imgur.com/bBIS3od.png

0 Upvotes

78 comments sorted by

View all comments

Show parent comments

1

u/Level-Impression Jul 04 '18 edited Jul 05 '18

Script just loads notification messages, here is an example:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$notifyobj = New-Object System.Windows.Forms.NotifyIcon
$notifyobj.icon = "c:/users/work/Pictures/icon.ico"
$notifyobj.BalloonTipTitle = "New Message"
$notifyobj.BalloonTipText = "C"
$notifyobj.Visible = $True
$notifyobj.ShowBalloonTip(1000)
$notifyobj.Dispose()

0

u/Lee_Dailey [grin] Jul 04 '18

howdy Level-Impression,

what happens if you open the ISE, load the script, and then run it?

take care,
lee

2

u/Level-Impression Jul 04 '18

It works in ISE but not if I try clicking the .ps1 file.

2

u/Lee_Dailey [grin] Jul 05 '18

howdy Level-Impression,

ok! so that means the script itself is fine. [grin]

clicking on a .ps1 file does not normally RUN a .ps1 file. i presume you are talking about using the VBScript file to bypass that limit.

what happens if you add a pause at the end of the .ps1 file?

take care,
lee

2

u/Level-Impression Jul 05 '18 edited Jul 05 '18

How do I add a pause? And what do you mean by bypassing the "limit"? Is there a limit to how many times I can run a .ps1? That probably explains why it randomly stopped working.

2

u/MaIakai Jul 05 '18

start-sleep -seconds 300

that at the end of the script gives you a decent pause

2

u/Level-Impression Jul 05 '18

I've added it to the end of the Add Arguments box & put it at the end of my script. Both ways don't work....

1

u/Lee_Dailey [grin] Jul 05 '18

howdy Level-Impression,

open the PoSh file and add pause on a line all by itself at the very end of the script. save it, and re-run JUST the script to make sure that it actually pauses. [grin]

then try it with your VBScript.
then try it thru task scheduler.

step by step debugging! wheee! [grin]

take care,
lee

2

u/Level-Impression Jul 05 '18 edited Jul 05 '18

If I open the .ps1 file then I get a PS window that says press enter & when I do the window closes & I don't get a notification.

If I open the .ps1 using my VBScript nothing happens. (this is probably because I changed the default way of opening PS from notepad to powershell via Computer\HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command)

If I run the script through task scheduler with the settings below I get a PS window that opens for a milisecond & quickly closes:

Arguments: -WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File "c:\users\work\desktop\script.ps1"

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

1

u/Lee_Dailey [grin] Jul 05 '18

howdy Level-Impression,

thank you for the feedback! [grin]

i would not expect things to work with -NonInteractive. however, i have NOT tested that, so i can very easily be wrong.

i tried your PoSh script on my win7x64, ps5.1 setup. the script runs without errors after i replaced the icon call with one that exists on my system. [grin]

however, no balloon tip ever shows. [frown]

so i went searching & found this ...

Correctly Showing Balloon Tips
— http://www.powertheshell.com/balloontip/

it DOES work on my system. you may want to try it out. copy it into the ISE, hit the green "run" arrow to load the function into memory, and then do something like this ...

Show-BalloonTip -Text 'This is some message text.' -Title 'Title Text, This Is!'

see if that works as expected.

if it does work, then you may want to start looking at differences between your code and what it uses. or you may want to switch to using the tested function.

take care,
lee

2

u/Level-Impression Jul 05 '18

I used this but it doesn't work in a .ps1 though it works in ISE: https://pastebin.com/PTbqnRUZ

→ More replies (0)