VMware Cloud Community
ricky73
Hot Shot
Hot Shot
Jump to solution

create PowerCLI script - Windows task scheduler

I read some doc about how to run my PowerCLI script to get my reports.

I understood It's necessary to initialize PowerShell environment to load required components and if you want to load all these ones,  you have to run the script Initialize-PowerCLIEnvironment.ps1. It's right?

If I open the link, which VMware Power CLI created during installation, it shows:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1

How Can I run my script (test.ps) by the previous line loading all components?

What do you suggest me please?

25 Replies
LucD
Leadership
Leadership
Jump to solution

See my remark about the Change Log


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

ricky73
Hot Shot
Hot Shot
Jump to solution

I read about change log and next week I will update PowerCLI to latest version.

I accepted suggestions to keep 6.x in previous thread but your word have elucidated how and what I have to do.

In light of what you've pointed, It's no more necessary to run this stament for my script? That is, there is not the Scripts\Initialize-PowerCLIEnvironment.ps1.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, there is no more need to run any kind of init script to load the PowerCLI modules.

They are loaded through the module autoload features.

Since the installation process has changed since PowerCLI is available through the PowerShell Gallery, I suggest to first read

Welcome PowerCLI to the PowerShell Gallery – Install Process Updates

and

Updating PowerCLI through the PowerShell Gallery


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

ricky73
Hot Shot
Hot Shot
Jump to solution

I installed 10.11 PowerCLI according to Vmware recommendation.

I created link to desktop as suggested by run %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noe -c "Import-Module VMware.PowerCLI"

So, backing to initial question If I want to schedule task (e.g. I'll create myscript.ps which create report), can I put the previous command into myscript.ps with the other commands?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You don't need the Import-Module in there, remember module autoload will do that for you.

And you need to add some extra parameters on the powershell.exe for running a script as Scheduled Task.


You also need to tackle the connection to the vSphere server.

You can provide credentials inline, or you can use something like the entries created by New-VICredentialStoreItem.

And there are other possibilities.

I use PowerShell to create my Scheduled Tasks, including the ones with PowerCLI.

Something like this

$psExe = "$($PSHOME)\powershell.exe"

$psArg = '-ExecutionPolicy Bypass -NonInteractive -NoLogo -NoProfile -Command'

$user = 'domain\user'

$runDate = Get-Date

$runInterval = New-TimeSpan -Days 7

$runFinish = New-TimeSpan -Days 1


$taskName = "PowerCLI Script"


Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false


$psScript = 'C:\Scripts\MyScript.ps1'


$fullArg = "$($psArg) ""& '$($psScript)'"""

$action = New-ScheduledTaskAction -Execute $psExe -Argument $fullArg

$trigger = New-ScheduledTaskTrigger -Once -At $runDate -RepetitionInterval $runInterval -RepetitionDuration $runFinish

$settings = New-ScheduledTaskSettingsSet -DisallowDemandStart:$false -RunOnlyIfNetworkAvailable -StartWhenAvailable

$principal = New-ScheduledTaskPrincipal -UserID $user -LogonType ServiceAccount -RunLevel Highest


$task = New-ScheduledTask -Action $action -Description $taskName -Trigger $trigger -Settings $settings -Principal $principal

Register-ScheduledTask -InputObject $task -TaskName $taskName -User $user


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

ricky73
Hot Shot
Hot Shot
Jump to solution

Thanks, your answer solved my problem.

Reply
0 Kudos