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

6

u/Ta11ow Jul 04 '18

Why are you running it with vbs?

-5

u/Level-Impression Jul 04 '18

So I can use it with task scheduler. It doesn't run if I just try to open the .ps1 file.

5

u/freelusi0n Jul 05 '18

Here's how to call a PowerShell script from task scheduler:

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

Add arguments (optional): -ExecutionPolicy Unrestricted -File "C:\complete\path\to\your\script.ps1"

2

u/Level-Impression Jul 05 '18

I've done that & it doesn't work hence why I started using VB...

5

u/dontridemywave Jul 05 '18

How were you trying to call it? try this?

-7

u/Level-Impression Jul 05 '18

I tried almost every way to call it. None of them work.

7

u/[deleted] Jul 05 '18

Did you try the linked way? Ive used it for ~20 different task and it never fails.

3

u/biganthony Jul 05 '18

Put the absolute path of powershell in the text box

Example: c:\program files\powershell.exe (not sure since I'm on mobile)

Then put the command line arguments

Example: -file (insert absolute path to script here)

This works on Windows server 2016 and windows 10

Let me know if this doesn't work. I'll help you out. Whatever it takes to end the use of VB

2

u/Level-Impression Jul 05 '18

I tried that & it doesn't work hence why I moved to VB.

3

u/biganthony Jul 05 '18

So it worked before using task scheduler by itself then stopped working? Then you moved to VB and it worked but stopped working just recently ?

2

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

It worked with task scheduler (it would load a console window before the message showing up) & when I clicked on the .ps1 file but 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...

4

u/biganthony Jul 05 '18

Okay I did some testing with the code you posted. You are running the task with the logged on user correct? You might be sending the notification to a different user session.

In the task settings under security "Run only when user is logged on" is that checked?

Also your username is set under "When running the task, use the following user account:" ?

2

u/Level-Impression Jul 05 '18

I use this: https://i.imgur.com/bBIS3od.png

It's what worked before so I don't think that is the problem...

0

u/[deleted] Jul 04 '18

[deleted]

-12

u/Level-Impression Jul 05 '18

I did & it didn't work.

4

u/Theratchetnclank Jul 05 '18

Asks for help. Doesn't like the answer so shuts it down.

Love it.

0

u/Level-Impression Jul 05 '18

Shuts down what?

2

u/[deleted] Jul 05 '18

then you did it wrong. i have done this hundreds of times. see below for a snippet of the exported task in XML.

<Actions Context="Author">

<Exec>

<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>

<Arguments>-WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -File "C:\Scripts\Script.ps1"</Arguments>

</Exec>

</Actions>

0

u/Level-Impression Jul 05 '18

I tried that & it does not work...

2

u/[deleted] Jul 05 '18

You're doing something wrong then.

0

u/Level-Impression Jul 05 '18

It worked before so clearly I was doing something right...

2

u/[deleted] Jul 05 '18

can you post the sanitized version XML export of the task?

3

u/devperez Jul 05 '18

I've done it multiple times. There was something I had to do, but I forget exactly. But I remember Googling it and it worked out easily enough.

0

u/[deleted] Jul 05 '18

[deleted]

1

u/Level-Impression Jul 05 '18

It didn't but now I did & it still doesn't work. It used to work before so that can't be the problem.

0

u/[deleted] Jul 05 '18

[deleted]

1

u/Level-Impression Jul 05 '18

How do I check? I'm new to PS so thanks for being patient.

0

u/[deleted] Jul 05 '18

[deleted]

2

u/Level-Impression Jul 05 '18

It shows the time in which the task was scheduled to run.

→ More replies (0)

4

u/jpedlow Jul 05 '18

I’m reading through this and I’m wondering if OP is relatively new to PS and just needs to be shown the light (chaining vbs for example)

1

u/Zenmaster28 Jul 04 '18

Impossible to say without seeing the script.

2

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

Here's what is in the VBS script:

Dim objShell,objFSO,objFile
Set objShell=CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")
'enter the path for your PowerShell Script
strPath="C:\Users\work\Desktop\script.PS1"
'verify file exists
If objFSO.FileExists(strPath) Then
'return short path name
set objFile=objFSO.GetFile(strPath)
strCMD="powershell -nologo -command " & Chr(34) & "&{" &_
objFile.ShortPath & "}" & Chr(34)
'Uncomment next line for debugging
'WScript.Echo strCMD
'use 0 to hide window
objShell.Run strCMD,0
Else
'Display error message
WScript.Echo "Failed to find " & strPath
WScript.Quit
End If

3

u/Pyprohly Jul 05 '18

It works for me.

Maybe try using the normal path instead of the short path. objFile.ShortPath —> objFile.Path

2

u/Level-Impression Jul 05 '18

Doesn't work...

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()

3

u/MaIakai Jul 05 '18

This code works fine on latest Windows 10 run through scheduled task.

1

u/Level-Impression Jul 05 '18

It used to work fine but not anymore...

6

u/Lee_Dailey [grin] Jul 04 '18

howdy Level-Impression,

it looks like you used the New.Reddit.com Inline Code button. it's the 4th 5th from the left & looks like </>.

on Old.Reddit.com, the above does NOT line wrap, nor does it side-scroll.

for long-ish single lines OR for multiline code, please, use the Code Block button. it's the 11th 12th one from the left, & is just to the left of the ... more menu.

that will give you fully functional code formatting, from what i can tell so far. [grin]

take care,
lee

7

u/Lee_Dailey [grin] Jul 05 '18

howdy anonymous coward downvoter,

four downvotes for pointing out that the poster used the wrong code button when posting code. the button that makes it seriously difficult to read the code in question on anything other than w-i-d-e screens or in New.Reddit ... what a delightful indicator that you are still alive! [grin]

there are times when i wonder if perhaps i have made a serious error in a downvoted post. then i note the extra no-reason-given downvotes and realize that you are still alive.

it is good to know that you need someone to annoy to give some slight meaning to your shallow, nearly pointless life - and that i am the one you can't stop pestering. if you weren't pestering me then you might be hassling others ...

bad cess to you! [grin]

lee

3

u/arnoldpalmerlemonade Jul 05 '18

I got your back Lee, upvote.

1

u/Lee_Dailey [grin] Jul 05 '18

howdy arnoldpalmerlemonade,

thanks! [grin]

the downvotes themselves aint a bother. it's the no-reason part that makes me wonder what i got wrong - and how can i fix that error?

then i found out that i was being stalked because of my posting style. [frown] that is freaky, but it gives the stalker something to do that causes me only minor annoyance. that's better than what some of the nasty stalkers do to others, so ... i decided to turn it around and enjoy it.

the stalker may not care, but it gives me a few grins! [grin]

take care,
lee

2

u/[deleted] Jul 05 '18

Dont take offense, probly some russian bots mixed up a phrase in your explanation and it triggered them to dv you. Stay for the cheers ingore the jeers.

1

u/Lee_Dailey [grin] Jul 05 '18

howdy azdood85,

you may well be correct that some are glitches. however, i have rcvd a number of posts that were quite virulently nasty concerning my posting style. very strange ... [frown]

so i started posting back. [grin] it amuses me - and occasionally gets one of the serious downvoters to tell me why the post was down voted.

thank you for the kind concern! [grin]

take care,
lee

2

u/[deleted] Jul 05 '18

[Thumbs up]

1

u/Lee_Dailey [grin] Jul 05 '18

[grin]

2

u/skypeforbiz Jul 05 '18

Lee, you are my favorite poster.

1

u/Lee_Dailey [grin] Jul 05 '18

howdy skypeforbiz,

thanks! [grin] my fave posters here vary from day to day. mostly folks who post nifty blog links or who go deep into the guts of PoSh. fun stuff that is well beyond my skill level - but still enjoyable to read.

take care,
lee

1

u/TheIncorrigible1 Jul 05 '18

I see a score of zero. Did you have +4? Reddit doesn't show you accurate scores on mobile, I've noticed.

1

u/Lee_Dailey [grin] Jul 05 '18

howdy TheIncorrigible1,

it was at -4 for a while yesterday eve. i figure it's my stalker from /r/programming seeking to vent a bit. that got rather nasty and i don't post there any more.

you are correct that reddit scores are inaccurate. from what i have read, that is by design - to reduce karma bots and lessen the "first post" effect that tends to bury later posts.

once i figured out that it wasn't "you effed up on this!", i realized it was likely another stalker event. that was a relief in that i hadn't given someone a seriously wrong bit of advice. [grin]

take care,
lee

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)