Du you use the same User in task and interaktive Mode?
You can try start-transcript to dig into the Problem during task execution.
I assume that you are using PowerCLI 6.5.1, and that the PowerCLI modules are in folder that is in the $env:PSModulePath?
Just in case try adding the following at the beginning
Get-Module -Name VMware* -ListAvailable | Import-Module
If that doesn't change anything, try recording the session in a transcript log.
Start-Transcript -Path C:\Temp\script.log
# Your script
Oh, and you do run powershell.exe with the NoProfile option in the task scheduler?Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
Apparently modules aren't automatically imported when running non-interactively? As far as running as a user, I'm using a @vsphere.local user to take away the windows authentication portion of the script. After I put the line to explicitly import modules, the script worked.
Is that by design?
And yes, I'm using PowerCLI 6.5.1
In PowerShell (since v3) there is the concept of module autoloading.
If your module's parent folder is in one of the folders in $env:PSModulePath and you haven't disabled module autoloading (through the $PSModuleAutoloadingPreference variable), the PS engine will load all exported cmdlets from the module.
But that is where the problem is with PowerCLI, it doesn't play nice, and hence module autoloading does not work for (some of) the PowerCLI modules.Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
As well, when I tried to connect to multiple vCenters at once, I can't get the script logic to run, if I separate by N of vCenters, it processes. Just passing along, i got it working but was a little confused
So two things:
- You seem to have figured it out, but probably best to include import-module in your script. Issues always seem to arise when you don't especially with scheduled tasks.
- In your scheduled task arguments, be sure to include "-ExecutionPolicy Bypass", not having this has bitten me in the butt more times than I care to remember.
Agreed on 1), strongly disagree on 2) I'm afraid.
Doing 2) makes your script source a serious attack vector.
My advice, for production at least, sign those scripts, and let the ExecutionPolicy do it's thing (preferably with AllSigned)Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz