Jump to content

Recommended Posts

Posted (edited)

TL;DR - Powershell won't run from Scheduled Task

 

Not sure if this is a Server issue or purely a coding problem.

 

I have a number of VMs which for various reasons I have now decided to shut down during the evenings and over the weekend.

 

This is obviously easy to accomplish as all our physical machines have a GPO that creates a scheduled task, so once that is applied it works as it should.

 

The problem is how to start the hem all up. I'm not sure if there's any other way to do this, but a search didn't turn up very much (which is understandable as there aren't many reasons to bother shutting down VMs).

 

One of the few results was a Powershell script and once I'd tested it in a Powershell instance, I saved it as a .ps1 file and created a scheduled task to run it.

 

All the usual settings run as admin, logged in or not, highest privileges, etc. I've tried moving the script file, adding/removing quote marks and all sorts of 'file', 'execution policy', 'noprofile', arguments, full path to Powershell... everything I could find.

 

Nothing in event viewer, sometimes the task runs and finishes, sometimes it just runs/hangs, but it never seems to get to starting the VM.

 

I don't really have any experience of running Powershell from scheduled tasks, it's usually bat, cmd, so troubleshooting this is beyond me.

 

Any suggestions on where to start, or any other way to accomplish the job?

Edited by Koldov
Posted
TL;DR - Powershell won't run from Scheduled Task

 

Not sure if this is a Server issue or purely a coding problem.

 

I have a number of VMs which for various reasons I have now decided to shut down during the evenings and over the weekend.

 

This is obviously easy to accomplish as all our physical machines have a GPO that creates a scheduled task, so once she that is applied it works as it should.

 

The problem is how to start the hem all up. I'm not sure if there's any other way to do this, but a search didn't turn up very much (which is understandable as there aren't many reasons to bother shutting down VMs).

 

One of the few results was a Powershell script and once I'd tested it in a Powershell instance, I saved it as a .ps1 file and created a scheduled task to run it.

 

All the usual settings run as admin, logged in or not, highest privileges, etc. I've tried moving the script file, adding/removing quote marks and all sorts of 'file', 'execution policy', 'noprofile', arguments, full path to Powershell... everything I could find.

 

Nothing in event viewer, sometimes the task runs and finishes, sometimes it just runs/hangs, but it never seems to get to starting the VM.

 

I don't really have any experience of running Powershell from scheduled tasks, it's usually bat, cmd, so troubleshooting this is beyond me.

 

Any suggestions on where to start, or any other way to accomplish the job?

 

Task3.png

 

Are you launching Powershell and linking to the script itself? You can’t just link the script like a cmd.

  • Thanks 1
Posted (edited)

Yeah, that's pretty much it, just like the screenshot!

 

Are you launching Powershell and linking to the script itself? You can’t just link the script like a cmd.

 

 

I didn't see anything in my research that said you couldn't.... just that when it wouldn't work they needed to put the full Powershell path, or different arguments (no profile, execution policy etc.), or quotes (or not), then it worked and I tried all those things.

Edited by Koldov
Posted
Possibly rather more clunky, but could you try "cmd" in the program / script field and "/C powershell -File pathToScriptGoesHere"?
  • Thanks 1
Posted (edited)

What exactly do you have set? I just tested it and works fine here

 

Using runas: system

Highest Priv

Configure for: Server 2019

 

Program: powershell

Arguments: -File "C:\test\StartVM.ps1"

(which just has Start-VM -Name VMNAME in it)

 

Obviously it may just be your user account if you're setting it to a specific user, do they have HyperV admin rights set?

 

Steve

Edited by Steve21
  • Thanks 1
Posted

If it's that simple I'm going to be kicking myself.... I spent 2 hours on it (in between doing other thing on the server) last night!

 

Maybe it was the script as it looked slightly more complicated than that...

 

I was using domain\admin, but I think I tried 'system' too (most of the other scheduled tasks are setup as 'system' though I must admit).

 

Strange thing though was every time I set the task up (using domain\admin), when I went back in to edit it, it was running as just ' admin' (local I presume) and I had to re-enter domain\admin every time, couldn't seem to get it to stick. Might be just a display thing but I couldn't then be sure if it was running under domain or local admin...

Posted (edited)

Consider myself kicked... I tried the 'simple' approach and it worked...

 

Here is the script in case anyone knows what was wrong (apart from being too complicated), I think it was part of a large set-up where they wanted to do multiple/individual VMs (hence the 'notes' on each 'prod' server instead of a list of names)...

 

[cmdletbinding()]
Param($vmhost = ‘HOST-SRV')
$VMtoStart = Get-VM -ComputerName $vmhost| where notes -contains 'WINDOWS-10-VM'
foreach ($cn in $VMtoStart)
{Start-VM $cn.name -asjob}

Edited by Koldov
Posted
Unless it's just your formatting on forum, is that two different ' `'s on the HOST-SRV? (Before looking into it more :p)

 

Steve

 

Looks like - the code tags stop ‘smart’ formatting. I think you have ye olde backtick and apostrophe together.

Posted
Unless it's just your formatting on forum, is that two different ' `'s on the HOST-SRV? (Before looking into it more :p)

 

Steve

 

Looks like - the code tags stop ‘smart’ formatting. I think you have ye olde backtick and apostrophe together.

 

Yeah, sorry I was out and answering on my phone. Had to edit the host name to redact....

 

It's correct in the script (as it works through pasting it into a PS instance or PS edit window).

Posted (edited)

Is that all the code? Now I am on my phone so could be missing something here but it looks like it starts with [cmdletbinding()]

 

If that is true then in my mind that will fail as that declaration forms part of function verb-noun.... and will error, running scripts interactively and none interactively will alter things in sutal ways. That said running that chunck of code should produce an error either way round.

 

If you could run just that code and paste the ps output or dig out the whole of that function that would be useful.

Edited by HPlum78
Posted (edited)

So just running that code as is now I am in front of a box gives me the following error:

 

Unexpected attribute 'cmdletbinding'....

 

Now this can be made to continue with caveats, and depends on if this is a terminating or none terminating error, and what $ErrorActionPreferance is set to.

 

The upshot is that running that interactively and I guess that you have $ErrorActionPreferance set to silentlycontinue? Then the script will run. However when you run it as a schedule task it fails as that erroraction will be set to stop.

 

In any case ditch the [cmdletbinding()] and put in some logging (see my previous posts on this topic) and don't forget you can debug PS scripts and mimic them being run none interactively. TBH I would functionlise this moving forward but initially do it dirty and get it working.

Edited by HPlum78
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...