yougoslav
Contributor
Contributor

PowerCLI - Script start VM

Hi everyone.

very new into PowerCLI 6.5, i wanted to make automated VM startup.

I got myself running but it must be a way much more easier that the one I use.

I explain myself, in powershell i can't get the import-module vmware* working... so i must go through powercli

then in powercli, i have to copy the initialization script and adapt it with this commands:

cd \

Set-ExecutionPolicy remotesigned

Connect-VIServer -Server MYvCenter

Start-VM -VM "MyVM"

But this is lauching a lot of importe module and stuff, so i wanted to get it work but with not all of those things...

here is the task

so every morning i start a VM.

i did it with TaskManager on W2k8R2 server.

here is the task action : start a program wtih option :

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

option : -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\powerCLI-autoStartVM.ps1\" $true"

and here is the contain of  powerCLI-autostartVM :

#######################################################################################################################

# This file will be removed when PowerCLI is uninstalled. To make your own scripts run when PowerCLI starts, create a

# file named "Initialize-PowerCLIEnvironment_Custom.ps1" in the same directory as this file, and place your scripts in

# it. The "Initialize-PowerCLIEnvironment_Custom.ps1" is not automatically deleted when PowerCLI is uninstalled.

#######################################################################################################################

param([bool]$promptForCEIP = $false)

# List of modules to be loaded

$moduleList = @(

    "VMware.VimAutomation.Core",

    "VMware.VimAutomation.Vds",

    "VMware.VimAutomation.Cloud",

    "VMware.VimAutomation.PCloud",

    "VMware.VimAutomation.Cis.Core",

    "VMware.VimAutomation.Storage",

    "VMware.VimAutomation.HorizonView",

    "VMware.VimAutomation.HA",

    "VMware.VimAutomation.vROps",

    "VMware.VumAutomation",

    "VMware.DeployAutomation",

    "VMware.ImageBuilder",

    "VMware.VimAutomation.License"

    )

$productName = "PowerCLI"

$productShortName = "PowerCLI"

$loadingActivity = "Loading $productName"

$script:completedActivities = 0

$script:percentComplete = 0

$script:currentActivity = ""

$script:totalActivities = `

   $moduleList.Count + 1

function ReportStartOfActivity($activity) {

   $script:currentActivity = $activity

   Write-Progress -Activity $loadingActivity -CurrentOperation $script:currentActivity -PercentComplete $script:percentComplete

}

function ReportFinishedActivity() {

   $script:completedActivities++

   $script:percentComplete = (100.0 / $totalActivities) * $script:completedActivities

   $script:percentComplete = [Math]::Min(99, $percentComplete)

 

   Write-Progress -Activity $loadingActivity -CurrentOperation $script:currentActivity -PercentComplete $script:percentComplete

}

# Load modules

function LoadModules(){

   ReportStartOfActivity "Searching for $productShortName module components..."

 

   $loaded = Get-Module -Name $moduleList -ErrorAction Ignore | % {$_.Name}

   $registered = Get-Module -Name $moduleList -ListAvailable -ErrorAction Ignore | % {$_.Name}

   $notLoaded = $registered | ? {$loaded -notcontains $_}

 

   ReportFinishedActivity

 

   foreach ($module in $registered) {

      if ($loaded -notcontains $module) {

         ReportStartOfActivity "Loading module $module"

       

         Import-Module $module

       

         ReportFinishedActivity

      }

   }

}

LoadModules

# Update PowerCLI version after snap-in load

$powerCliFriendlyVersion = [VMware.VimAutomation.Sdk.Util10.ProductInfo]::PowerCLIFriendlyVersion

$host.ui.RawUI.WindowTitle = $powerCliFriendlyVersion

# Launch text

write-host "          Welcome to VMware $productName!"

write-host ""

write-host "Log in to a vCenter Server or ESX host:              " -NoNewLine

write-host "Connect-VIServer" -foregroundcolor yellow

write-host "To find out what commands are available, type:       " -NoNewLine

write-host "Get-VICommand" -foregroundcolor yellow

write-host "To show searchable help for all PowerCLI commands:   " -NoNewLine

write-host "Get-PowerCLIHelp" -foregroundcolor yellow

write-host "Once you've connected, display all virtual machines: " -NoNewLine

write-host "Get-VM" -foregroundcolor yellow

write-host "If you need more help, visit the PowerCLI community: " -NoNewLine

write-host "Get-PowerCLICommunity" -foregroundcolor yellow

write-host ""

write-host "       Copyright (C) VMware, Inc. All rights reserved."

write-host ""

write-host ""

# CEIP

Try    {

    $configuration = Get-PowerCLIConfiguration -Scope Session

    if ($promptForCEIP -and

        $configuration.ParticipateInCEIP -eq $null -and `

        [VMware.VimAutomation.Sdk.Util10Ps.CommonUtil]::InInteractiveMode($Host.UI)) {

        # Prompt

        $caption = "Participate in VMware Customer Experience Improvement Program (CEIP)"

        $message = `

            "VMware's Customer Experience Improvement Program (`"CEIP`") provides VMware with information " +

            "that enables VMware to improve its products and services, to fix problems, and to advise you " +

            "on how best to deploy and use our products.  As part of the CEIP, VMware collects technical information " +

            "about your organization’s use of VMware products and services on a regular basis in association " +

            "with your organization’s VMware license key(s).  This information does not personally identify " +

            "any individual." +

            "`n`nFor more details: press Ctrl+C to exit this prompt and type `"help about_ceip`" to see the related help article." +

            "`n`nYou can join or leave the program at any time by executing: Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP `$true or `$false. "

        $acceptLabel = "&Join"

        $choices = (

            (New-Object -TypeName "System.Management.Automation.Host.ChoiceDescription" -ArgumentList $acceptLabel,"Participate in the CEIP"),

            (New-Object -TypeName "System.Management.Automation.Host.ChoiceDescription" -ArgumentList "&Leave","Don't participate")

        )

        $userChoiceIndex = $Host.UI.PromptForChoice($caption, $message, $choices, 0)

      

        $participate = $choices[$userChoiceIndex].Label -eq $acceptLabel

        if ($participate) {

         [VMware.VimAutomation.Sdk.Interop.V1.CoreServiceFactory]::CoreService.CeipService.JoinCeipProgram();

      } else {

         Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false | Out-Null

      }

    }

} Catch {

    # Fail silently

}

# end CEIP

Write-Progress -Activity $loadingActivity -Completed

cd \

Set-ExecutionPolicy remotesigned

Connect-VIServer -Server MYvCenter

Start-VM -VM "MyVM"

So every advice will be appreciate.

thanks

0 Kudos
0 Replies