function Enable-PowerCLI

function Enable-PowerCLI

function Enable-PowerCLI ()

{

    Write-Verbose ("Entered function : {0} " -f $MyInvocation.MyCommand)

    $ModuleName = "VMware.PowerCLI"

    if(($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSEdition -ne "Core"))

    { 

        Write-Verbose "PowerShell version greater/equal to 5 installed" 

        if (Get-Module -Name $ModuleName){Write-Verbose "PowerShell $ModuleName Module already loaded"; return $True}

        else

        {

   

            if (!(Get-Module -Name $ModuleName) -and (Get-Module -ListAvailable -Name $ModuleName))

            {

                try

                {

                    Write-Verbose "loading the PowerShell $ModuleName Module..." 

                    Import-Module -Name $ModuleName -ErrorAction Stop

                    Write-Verbose ("Exiting function : {0} " -f $MyInvocation.MyCommand)

                }

                catch # Try to install if loading fails

                {

                    Write-Host "Failed initial loading of PowerShell $ModuleName Module..." 

                    Try

                    {

                        Write-Verbose "Checking/Installing NuGet package provider" 

                        if(!(Get-PackageProvider -ListAvailable -Name "NuGet")){Get-PackageProvider -Name NuGet -Force}

                        Write-Verbose "Setting PowerShell Gallery as trusted Repository" 

                        Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

                        Write-Verbose "Installing and loading the PowerShell $ModuleName Module..." 

                        Install-Module $ModuleName -scope CurrentUser -Force -ErrorAction Stop

                        Import-Module -Name $ModuleName -ErrorAction Stop

                    }

                    catch{Write-Host "ERROR: Cannot load or install the VMware PowerCLI Module. Please install manually." - ForegroundColor Red;return $false;exit 1}

                }

                Write-Verbose ("Exiting function : {0} " -f $MyInvocation.MyCommand)

                return $True

            } # End if (!(Get-Module -Name "VMware.PowerCLI") -and (Get-Module -ListAvailable -Name "VMware.PowerCLI"))

        }

    } # End if(($PSVersionTable.PSVersion.Major -ge 5) -and ($PSVersionTable.PSEdition -ne "Core"))

    elseif(($PSVersionTable.PSVersion.Major -ge 3)-and ($PSVersionTable.PSVersion.Major -lt 5))

    {

        Write-Verbose "PowerShell version greater/equal to 3 but less than 5 installed" 

        $ModuleList = @(

            "VMware.VimAutomation.Core",

            "VMware.VimAutomation.Vds",

            "VMware.VimAutomation.Cloud",

            "VMware.VimAutomation.PCloud",

            "VMware.VimAutomation.Storage",

            "VMware.VimAutomation.HA",

            "VMware.VimAutomation.vROps",

            "VMware.VumAutomation",

            "VMware.VimAutomation.License",

            "VMware.VimAutomation.Cis.Core"

            )

           

        foreach($Module in $ModuleList)

        {

            if ((!(Get-Module -Name $Module)) -and (Get-Module -ListAvailable -Name $Module))

            { 

                Write-Verbose "loading the $Module Module..." 

                try

                {

                    Import-Module -Name $Module -ErrorAction Stop

                    $Loaded = $True 

                }

                catch {Write-Host "ERROR: Cannot load the $Module Module. Is VMware PowerCLI installed?" - ForegroundColor Red;return $false;exit 1} # Error out if loading fails

            } # End if ((!(Get-Module -Name $Module)) -and (Get-Module -ListAvailable -Name $Module)) 

            elseif ((!(Get-PSSnapin -Name $Module -ErrorAction SilentlyContinue)) -and (!(Get-Module -Name $Module)) -and ($Loaded -ne $True))

            { 

                Write-Verbose "loading the $Module Snapin..." 

                Try {Add-PSSnapin -PassThru $Module -ErrorAction Stop}

                catch {Write-Host "ERROR: Cannot load the $Module Snapin or Module. Is VMware PowerCLI installed?" - ForegroundColor Red;return $false;exit 1} # Error out if loading fails

            }

        } # End foreach($Module in $ModuleList)

        Write-Verbose ("Exiting function : {0} " -f $MyInvocation.MyCommand)

        return $True

    }

    else{Write-Host "PowerShell version less than 3 installed!" - ForegroundColor Red;return $false;exit 1 }

    Write-Verbose ("Exiting function : {0} " -f $MyInvocation.MyCommand)

} # End function Enable-PowerCLI

Version history
Revision #:
1 of 1
Last update:
‎04-26-2018 03:25 AM
Updated by: