Automation

 View Only
Expand all | Collapse all

create PowerCLI script - Windows task scheduler

  • 1.  create PowerCLI script - Windows task scheduler

    Posted Jun 28, 2018 11:53 AM

    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?



  • 2.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 07:03 AM

    Which PowerCLI version are you using?

    In more recent versions you don't have to run this init script anymore, all loading is done through module auto loading (since PowerCLI 6.5.1, which is NOT 6.5 R1).



  • 3.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 07:48 AM

    6.3 release 1 because vcenter is 6.0.0 so I'd like to keep version not too updated



  • 4.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 07:57 AM

    Not too sure why you need to stick on the older 6.3, that is no prereq for vCenter 6.

    You're missing out on some major improvements.

    One of the important ones is the move to all modules, no more PSSnapin.



  • 5.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 10:02 AM

    so what version do you suggest me for vcenter 6.0.0 ?



  • 6.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 10:47 AM

    I would go for the latest 10.1.1, you can always return to the one you are using currently.

    See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates for instructions on how to do the update.

    But first check what PowerShell version you are currently using.

    What is in $PSVersionTable.

    Is that showing PowerShell 5.1? Or an earlier version?



  • 7.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 12:45 PM

    It's the latest, that is 5.1 which I updated just before PowerCli installation.

    I believed latest PowerCli 10.x uses enhanced features which are not available for my 6.0..0 vCenter so I prefer to keep just steps before 10.x

    What do you think ? 6.5 or 10.x with vCenter 6.0.0 ?



  • 8.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 12:50 PM

    I'm afraid you interpret that incorrectly, it's not because PowerCLI 10.1.1 supports new features in 6.5 or 6.7, that you can't use that version with vSphere 6.

    Those new features just will not work in 6.0.
    It's not that the internal workings of PowerCLI cmdlet are different, you just have additional ones, or new parameters on existing cmdlets.


    Btw, if you know that you are in vSphere 6, you shouldn't be calling newer features.


    And that is also why there is a compatibility matrix!



  • 9.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 02:44 PM

    LucD

    if you know that you are in vSphere 6, you shouldn't be calling newer features.

    That's right! infact this is main reason to not install last version, so I'm sure I'll get almost all features for my vCenter version.



  • 10.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 02:58 PM

    You don't seem to understand that this cumulative.

    There is no reason to stay on an older PowerCLI version.
    If the compatibility matrix shows that a version is compatible with your vSphere version , you will be able to use all features of that version.



  • 11.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 03:52 PM

    I'll try to explain in different way.

    If I installed vCenter PowerCLI 10.x and for example It uses function "K1" which is characteristic for only vCenter 6.5/6.7 , I will not able to use it because it's not provided inside vCenter 6.0.

    If I use manual of PowerCLI 10.x,  I can make mistake and confuse myself because I don't know what is command for my vCenter;  if I have to create new script and I have to verify what function of Powercli are available for my older vCenter.

    I understood that latest PowerCLI to use all features of previous version.



  • 12.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 03:54 PM

    I give up!



  • 13.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 03:58 PM
    Ok I have a similar problem, Powercli 6.5, I need to run a script from CMD and it's so hard ?! I don't know how this thing is called powerCli ?! what's powerful in it since it's not easy to trigger a script


  • 14.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 04:58 PM

    That remark is a bit misplaced imho.
    Your problem is with Jenkins, and you insist on using an older PowerCLI version, with all the complications that version brings.

    I pointed to a solution in your thread, did you even look at it, or even better, try it?

    Btw, the 'power' in the name comes from the link with PowerShell.



  • 15.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 08:06 PM

    LucD

    look at the following thread PowerCLI on vCenter

    to see if I explained better what I wanted to say.



  • 16.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 08:43 PM

    No, I still see the same misconception about using an up to date PowerCLI version.

    Btw, a reply that agrees to install PowerCLI on a vCenter would make me very wary about everything else that person says.

    A newer version not only introduces new features, but also improvements and bug fixes.

    Just have a look at the Release Notes.

    But it's your choice, and like I said I give up on this thread.

    I don't even have 6.3 anymore.



  • 17.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 29, 2018 09:44 PM

    I'd like to take a second to echo what LucD has been saying.

    The official VMware statement is to always be on the latest version of PowerCLI.

    This is said for many reasons, most of which were outlined in the form of a blog post from the other thread you linked to: Which Version of PowerCLI is Right For Me? - VMware PowerCLI Blog - VMware Blogs

    Basically:

    • There's no dependency or relation between vSphere and PowerCLI. Any dependency which may have existed, was removed many years ago.
    • PowerCLI is backwards compatible to any supported version of vSphere, therefore 10.1.1 works back to vSphere 5.5.
    • There were some major performance improvements that were made in the transition from 6.3R1 up to PowerCLI 6.5.1.
      • In your case, that means your commands and scripts will perform faster.
    • Each version of PowerCLI also includes bug fixes, therefore any issues you may be experiencing in 6.3R1 may be fixed in newer versions of PowerCLI.
      • So much so that if you create a support request concerning PowerCLI, support will ensure you're on the latest version of PowerCLI before moving forward with the request.


  • 18.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 30, 2018 07:45 AM

    Ok, according to all suggestions, I'll migrate to latest version 10.x

    But what powerCLI reference guide document I should have to use? I think for 10.x.

    Let's suppose I need new function for my script and I found it in 10.x manual, how I detect if this feature is implemented or no in vCenter 6.0? Is there matrix which list for every function in what vCenter is available ?

    I'm not saying about exist scripts because I know is guaranteed backward compatibility.



  • 19.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 30, 2018 09:37 AM

    The PowerCLI information is available in 2 formats.

    1. The Get-Help cmdlet, which is the standard PowerShell way of providing help for a cmdlet.
      1. Note that there are also about_ help topics, which discuss principles instead of a specific cmdlet. See for example Get-Help -Name about_obn
    2. The online documentation at VMware{code}. For the latest version see PowerCLI 10.1.1 In there are multiple links:
      1. On online version of the Get-Help content at Cmdlet Reference
      2. The Change Log. In there you will find changes and additions for each PowerCLI release, including notes on which feature was added/changed for which vSphere version.


  • 20.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 30, 2018 09:53 AM

    I'm sorry but we are speaking 2 different languages, I'm not saying where I can find documentation about PowerCli 10.x

    My question is another one, I'll try again to explain:

    Let's suppose I need new function for my script and I found it in 10.x manual, how I detect if this feature is implemented or no in vCenter 6.0? Is there matrix which list for every function in what vCenter is available ?

    PowerCLI 10.x could be provide function "A" for only vCenter 6.7 so if I used (unawares) this A function in vCenter 6.0.0, I'll get error or no result, right? I'd like to avoid it.

    How can I verify functions which I'll use in PowerCLI 10.x, are good for vCenter 6.0 ?  There will be dedicated enhanced functions for only 6.5/6.7 vCenter, right?

    I'm saying about new script which I'll create in future, not existing script which are compatible to new PowerCLI version.



  • 21.  RE: create PowerCLI script - Windows task scheduler

    Posted Jun 30, 2018 10:02 AM

    See my remark about the Change Log



  • 22.  RE: create PowerCLI script - Windows task scheduler

    Posted Jul 01, 2018 05:48 AM

    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



  • 23.  RE: create PowerCLI script - Windows task scheduler
    Best Answer

    Posted Jul 01, 2018 05:54 AM

    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



  • 24.  RE: create PowerCLI script - Windows task scheduler

    Posted Jul 02, 2018 09:56 AM

    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?



  • 25.  RE: create PowerCLI script - Windows task scheduler

    Posted Jul 02, 2018 10:04 AM

    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



  • 26.  RE: create PowerCLI script - Windows task scheduler

    Posted Jul 02, 2018 10:13 AM

    Thanks, your answer solved my problem.