Reputation: 57
I've got a really simple PS script i wrote to open programs when i start my day.
Clear
Write-Host " "
Write-Host " "
Write-Host "Starting Outlook"
Start-Process "C:\Program Files (x86)\Microsoft
Office\root\Office16\OUTLOOK.EXE"
Write-Host `n "Pausing for 5 seconds while Outlook starts."
Start-Sleep 5
Write-Host " "
Write-Host "Starting IE"
Start-Process "C:\Program Files (x86)\Internet Explorer\iexplore.exe"
Write-Host `n "Pausing for 5 seconds while IE starts."
Start-Sleep 5
Write-Host " "
Write-Host "Starting Chrome"
Start-Process "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Write-Host `n "Pausing for 5 seconds while Chrome starts."
Start-Sleep 5
Write-Host " "
Write-Host "Starting Skype"
Start-Process "C:\Program Files (x86)\Microsoft Office\root\Office16\lync.exe"
Write-Host `n "Pausing for 5 seconds while Skype starts."
Start-Sleep 5
Write-Host " "
Write-Host "Starting Config Manager"
Start-Process "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\Microsoft.ConfigurationManagement.exe"
Write-Host `n "Pausing for 5 seconds while Config Manager starts."
Start-Sleep 5
Write-Host " "
Write-Host "Starting Query Tool"
Start-Process "C:\QueryTool\QueryForms.exe"
Write-Host " "
Write-Host "All programs started"
Write-Host " "
Start-Sleep 2
Write-Host "Exiting, have a good day!"
Start-Sleep 2
Exit
Problem is if i run this from my desktop it fails. The PS window opens flashes red text and closes again faster than i can tell what happened. I've seen other posts for this saying to add the -noexit switch, or add a Start-Sleep at the end, or Read-Host -Prompt "Press any key to continue", but none of those work. The error apparently is happening before the script starts. I've set PS and PS (ISE) to run as admin anytime they open, running that script doesn't prompt for admin so it's not being opened as an admin, which is what i'm guessing is wrong.
I don't see any options in the properties to set it to run as admin, or in the right click menu. Right clicking and opening with PS has the same result. Opening PS and running the script works fine.
Ideas?
Oh and i'm on Windows 10 x64, if that matters.
Upvotes: 0
Views: 2076
Reputation: 1
I dunno if you have figured it out yet, but...
Ensure there is NO space in name of file AND folder. Otherwise, PowerShell 1.0v will refuse to work.
Find a path to script then record that.
C:\Windows\System32> Start C:\[PathToScript].ps1 -ArgumentList "-noexit"
It is strongly recommended to put a script file in C:\ then leave it there be.
Start
is alias forStart-Process
which starts or opens a file. As everyone keeping talk about-noexit
, it is a switch used in-ArgumentList
, it prevents script from exiting suddenly when started. There must be quote between-noexit
, otherwise PowerShell will become confused and think that-noexit
isn't in-ArgumentList
.
That way, you can watch script working on it until its job is done and exits. According to your script, that is.
You do not need admin's rights to use Start-Process and Start-Sleep. Ensure that excutionpolicy is set to bypass or unrestricted. Otherwise, it will FAIL to start.
Set-Executionpolicy -Executionpolicy [Unrestricted/Bypass]
Upvotes: 0
Reputation: 11
I had this same problem and my fix was to ensure there were no 'spaces' in the path to my script. Ran fine from the console but double click or right click execution would fail. C:\My Path\myScript.ps1 would fail C:\My_Path\myScript.ps1 would run fine.
Upvotes: 0
Reputation: 57
finally figured this out.
Previously i had gotten tired of clicking on PS1 files to run them and having them open in Notepad. So i changed the default program to PowerShell. Once i changed that back to Notepad i can now right click the file and choose Run with Powershell and it runs just fine.
Upvotes: 0
Reputation: 1113
Option 1: Add this to the top of your code to turn the error into a terminating error. The reason for the error may be because the error is non-terminating, so it prints the error, then, reaches Exit
and closes the window.
$ErrorPreference = "Stop"
Option 2: Remove Exit
, and I'd recommend removing Clear
from the start, too. In this case, it may be better to run this script in the Powershell ISE (for versions 4+ I think).
Option 3: Launch the PS file directly. Perhaps adding Read-Host 'Press any key to continue'
at the end will then be useful. If the error comes from launching the script, aka outside the script, then this should prevent that error. Find the .ps1 file and select "Run with Powershell"
Upvotes: 1
Reputation: 1562
You don't specify how you are running the script but the reason it is exiting is because of the exit you have at the end. Remove it and you will be able to see the errors.
Upvotes: 0