VMware Cloud Community
SteveCSchofield
Enthusiast
Enthusiast
Jump to solution

Baffled, runs interactively, doesn't run in windows task scheduler (win 2012 r2)

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

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
8 Replies
vMarkusK1985
Expert
Expert
Jump to solution

Du you use the same User in task and interaktive Mode?

You can try start-transcript to dig into the Problem during task execution.

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
SteveCSchofield
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
SteveCSchofield
Enthusiast
Enthusiast
Jump to solution

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 Smiley Happy 

Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

So two things:

  1. 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.
  2. 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.

Reference:

How to run PowerShell scripts from Task Scheduler

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

awww, no fun LucD

but, yes, agreed, that is an assumed.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos