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 05 '18

1

u/[deleted] Jul 05 '18

that looks good. you are referencing a script inside a user profile, maybe the task doesn't have permission to access the file?

you can also try running the command from a command prompt manually see if you get an error.

1

u/Level-Impression Jul 05 '18

It worked before when I run it with user profile so that can't be the issue. If I run it from the PS command it works.

1

u/[deleted] Jul 05 '18

can you enable verbose logging in the script?

can you start a session transcript?

something like this at the top of the script:

$LogName = $PSCommandPath -replace ".ps1", "-"
Start-Transcript -Path $($LogName + (Get-Date -Format "yyyyMMdd") + ".log")

1

u/Level-Impression Jul 05 '18

1

u/[deleted] Jul 05 '18

Can post a sanitized version of the script?

1

u/Level-Impression Jul 05 '18

How do I do that?

1

u/[deleted] Jul 05 '18

Take out anything that identifies the company you are using this at or anything sensitive like passwords and server names. Then post on pastebin.com or similar service.

1

u/Level-Impression Jul 05 '18

I don't think there is any sensitive info in my script, what do you want me to post?

1

u/[deleted] Jul 05 '18

The whole script. Im curious where the "$global:?" Is coming from

→ More replies (0)