VMware Cloud Community
bmccool
Enthusiast
Enthusiast
Jump to solution

Running Powershell script with ESXCLI commands via a scheduled task in Windows 2012 doesn't work.

I'm trying to run a powershell script from a scheduled task in Windows 2012. The script calls the ESXCLI commands and completes successfully (exports to a CSV file) but the file has no data in it.  If I run the script manually without the scheduled task the file has all the output I would expect.

I'm wondering if there is a setting with in Powershell or a PSSnapin I need to add to the script for it to run via a scheduled task.  Has anyone done this before with the ESXCLI commands?

Reply
0 Kudos
1 Solution

Accepted Solutions
bmccool
Enthusiast
Enthusiast
Jump to solution

panhvr - you may be on to something.  The process account I'm using to run this script only has read-only access in vCenter.  I logged in to the server I'm running these reports on with the process ID and tried to run the script.  It gave me errors stating permission to perform the esxcli operations was denied.

I think we found the culprit!  I'll have to up the rights of this process ID in vCenter.

Thanks everyone!

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

A scheduled task with PowerCLI cmdlets in there, has some specific requirements.

Did you take all of these into account ?

  • Does the account under which the scheduled task runs, have access to vCenter
  • Do you load the PowerCLI pssnapin(s) in the script in the scheduled task
  • Do you connect to a vSphere server, with the correct credentials

Try writing some verbose messages in your script, that might give an indication where the script fails


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
a_p_
Leadership
Leadership
Jump to solution

To ensure it's not Windows which runs the task with limited permissions, try to run it with "Run with highest privileges" enabled.

André

Reply
0 Kudos
bmccool
Enthusiast
Enthusiast
Jump to solution

Thanks for the responses!

I have the scheduled task running under a process ID that has read-only access to vCenter.  It is assigned to run with the highest privileges.  The script loads the "VMware.VimAutomation.Core" PSSnapin, but I didn't know if there was another one that needed to be loaded to run ESXCLI commands.

I have other scheduled tasks running that call powershell scripts that work, the only ones I'm having trouble with are ones that use the ESXCLI commands.

Reply
0 Kudos
bmccool
Enthusiast
Enthusiast
Jump to solution

I should also mention - the script (as well as the scheduled task) doesn't actually fail.  It completes successfully and spits out a CSV file, the file just has no data in it.  So the scheduled task is calling the script just fine and reading it because it gets to the end where it writes out to a CSV. 

The problem seems to be with the code in between which uses the ESXCLI commands.  The script works fine running it manually, which is why I was suspecting I may be missing a PSSnapin, but wasn't sure.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Perhaps it would be useful to include the code you are trying to schedule ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
bmccool
Enthusiast
Enthusiast
Jump to solution

Sure,

Here is one of them.  Just a simple script to check the software acceptance level for each host.

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer <vCentere Server>

$report = @()

$hosts = Get-VMHost | Where {$_.Version -eq "5.0.0"}

foreach ($esx in $hosts)

{

  $esxcli = Get-EsxCli -VMHost $esx

  $line = "" | Select Host, AcceptanceLevel

  $line.Host = $esx.Name

  $line.AcceptanceLevel = $esxcli.software.acceptance.get()

  $report += $line

}

$filename = "C:\HostSoftwareAcceptanceLevel.csv"

$report | Export-Csv $filename -NoTypeInformation

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you run the script in 32-bit mode PowerShell ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
bmccool
Enthusiast
Enthusiast
Jump to solution

I'm using the executable in the system32\Windows Powershell\v1.0 directory, not the sysWOW64\Windows Powershell\v1.0 directory.  Should I be using the other executable?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, the 32-bit version should be ok.

And the "core" PSSnapin should be the only one you need for this cmdlet.

You mentioned earlier that the script runs but there is no output in the CSV file it produces.

The ESXi hostname should be in there though, that has nothing to do with the Get-EsxCli part.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
bmccool
Enthusiast
Enthusiast
Jump to solution

Yes you're right, I misspoke a little bit.  The hostnames do output to the CSV file, just nothing for the software acceptance level.

Reply
0 Kudos
panhvr
Contributor
Contributor
Jump to solution

Even I see the same issue .If I fire the batch file manually on the server which works  fine . If I schedule  the same via schedule task and set it to run user is logged in or not then it says inconnect function .

When I change the setting to run when user logged in and run the task -which works  fine

When I change the account to run to SYSTEM the task runs fine but the report is incomplete or keeps running for long time  .

bmccool
Enthusiast
Enthusiast
Jump to solution

panhvr - you may be on to something.  The process account I'm using to run this script only has read-only access in vCenter.  I logged in to the server I'm running these reports on with the process ID and tried to run the script.  It gave me errors stating permission to perform the esxcli operations was denied.

I think we found the culprit!  I'll have to up the rights of this process ID in vCenter.

Thanks everyone!

Reply
0 Kudos
bmccool
Enthusiast
Enthusiast
Jump to solution

For those wanting to know the only permission I needed to assign to the process ID in vCenter in order to run a scheduled task for a powershell script using ESXCLI commands was Global --> Settings.

Reply
0 Kudos
panhvr
Contributor
Contributor
Jump to solution

In my case this looks something else. As I am running script to login to all VC servers and gather performance report of  VC's like memory/cpu/lastuptime/ etc when I set the task to "run when user logged" in and schedule or fire it immediately it works  like Gem .If I schedule it to " run when user is logged in or not " task completes with exit error incorrect function .

As I need to login to remote servers to collect the data setting the task to run using system account will result incomplete report.

Below is how I have set the configuration on the task.

Program/script -C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments --PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -Command  ". 'E:\PS_Scripts\vSphere\CPU_MEM_VCs.ps1'" -NoLogo -NoProfile -Noninteractive

Reply
0 Kudos
panhvr
Contributor
Contributor
Jump to solution

This is just blocking my scripts to go live on windows 2012 servers !! Any help would be great

Reply
0 Kudos
panhvr
Contributor
Contributor
Jump to solution

You know what !! if I keep my scripts on System Drive which is C drive the tasks runs  fine ....But not on E drive with more privileges than C drive.

Reply
0 Kudos