Background / Goal
I am attempting to create a scheduled task that will:
- Run at the specified time (once)
- Run a PowerShell script that:
- Logs all of its output as a transcript
- Connects to my vCenter server using encrypted, stored credentials
- Shuts down the Guest OSes of all VMs in a certain folder
Problem
The script executes perfectly from the console but doesn't even begin to execute as a task.
The Code
#Stop an error from occurring when a transcript is already stopped
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
#Reset the error level before starting the transcript
$ErrorActionPreference="Continue"
Start-Transcript -path C:\temp\Shutdown_NonProductionVMs.log -append
#Add the VMWare Snap-in
Add-PSSnapin -Name VMWare.VimAutomation.Core
#Get the Credentials
$creds = Get-VICredentialStoreItem -file C:\temp\pscreds.creds
#Connect to the server using the credentials file
Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password
#Get all VMs within the non-production VMs folder and then shut them down
#NOTE the -WhatIf tag, which will stop it from shutting down the VMs until we're sure the script is good.
Get-Folder -Name "03. Non-Production VMs" | Get-VM | Shutdown-VMGuest -WhatIf
#Clean Up
Disconnect-VIServer -Force -Confirm:$false
Remove-PSSnapin -Name VMWare.VimAutomation.Core
Stop-Transcript
What I've Checked/Tried so far and Other Leads
- Again, this script runs perfectly fine in a console when the console is opened as an admin.
- I have set a machinelevel execution policy of "Unrestricted" to see if that would help.
- I am running the task as the same user I created the credentials file with
- I am running the task with highest privileges.
- I am running the task with stored credentials (my user credentials -- the same ones that work from the console).
- I tried commenting out the transcript-related code to see if that did it; no dice.
- The task just continues to run perpetually. I don't get any result and it doesn't terminate.
Any ideas? Thanks in advance for any help you can give!