baffled....The user executing the task has Logon as a Batch, is a local Administrator. I'm using the latest download from
I run this interactively, it works. I run in a windows task scheduler, zero machines are returned.
Connect-viserver -server vcenter.ss.local -user user@vsphere.local -password deleteme | out-null
start-sleep -s 10
$vmlist = Get-Folder -Name VRMDeleted | Get-vm
Add-Content -path "e:\temp\log.txt" -value $vmlist.count
foreach($vm in $vmlist)
{
Try
{
Add-Content -path "e:\temp\vm.txt" -value $vm.name
Remove-VM -DeletePermanently -VM $vm -confirm:$false
}
catch
{
Add-Content -path "e:\temp\errors.txt" -value $error
}
}
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
Stop-Transcript
Oh, and you do run powershell.exe with the NoProfile option in the task scheduler?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Stop-Transcript
Oh, and you do run powershell.exe with the NoProfile option in the task scheduler?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Kind of :smileygrin:
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: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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:
Reference:
How to run PowerShell scripts from Task Scheduler
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: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
awww, no fun LucD
but, yes, agreed, that is an assumed.