none
Task Manager problems running powershell script as admin RRS feed

  • Question

  • Hi all,

    I have a powershell script that checks if a VM in hyper-V is off and if it is it starts it (it's an RDP server that the boss accidentally shutsdown all the time).  If I run powershell as an administrator and execute the script from within powershell it works perfectly.

    In Task Scheduler I've created a basic task to run at a certain time of day, I've set 'Run whether user is logged on or not' true and 'Run with highest privileges' true. I save it and enter my password. However, when I right click it and choose run, the task goes to a state of 'running' and stays there until I end it, during which time it doesn't actually work.

    The script is literally a text file with a name startvmscript.ps1 with the following line in it:

    if (get-vm | where {$_.Name -eq 'RDP-Server' -and $_.State -eq 'Off'}) {start-vm -name RDP-Server}

    Where am I going wrong?

    Friday, March 8, 2019 12:09 AM

Answers

  • In the task actions, set "program/script" to "powershell.exe". In "Add arguments (optional)" use "-NonInteractive -noprofile c:\yourfolder\yourscript.ps1"

    If that works, then I would recommend that you try running the task as "system". That way if the password for the account that you are using changes, you won't have to go back and fix the task settings. 

    Friday, March 8, 2019 4:13 PM

All replies

  • This is a scripting form.  For issues with Task scheduler you need to post in the forum for your OS.  They will help you troubleshoot task scheduler issues.

    Also you code would be better like this:

    get-vm 'RDP-Server' | Where-Object { $_.State -eq 'Off' } | start-vm


    \_(ツ)_/

    Friday, March 8, 2019 12:54 AM
  • In the task actions, set "program/script" to "powershell.exe". In "Add arguments (optional)" use "-NonInteractive -noprofile c:\yourfolder\yourscript.ps1"

    If that works, then I would recommend that you try running the task as "system". That way if the password for the account that you are using changes, you won't have to go back and fix the task settings. 

    Friday, March 8, 2019 4:13 PM
  • In the task actions, set "program/script" to "powershell.exe". In "Add arguments (optional)" use "-NonInteractive -noprofile c:\yourfolder\yourscript.ps1"

    If that works, then I would recommend that you try running the task as "system". That way if the password for the account that you are using changes, you won't have to go back and fix the task settings. 

    Thanks, that makes sense. Cheers.
    Wednesday, March 13, 2019 12:55 AM