Last active
March 22, 2017 15:09
-
-
Save vScripter/b27a62cc5710a154db87fa2da3870fca to your computer and use it in GitHub Desktop.
PowerCLI 'loader' function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Start-PowerCLI { | |
<# | |
.NOTES | |
Loads PowerCLI, depending on what version you have installed. Set the default -Version parameter, depending in your environment. | |
#> | |
[cmdletbinding()] | |
param ( | |
[parameter(Mandatory = $false, Position = 0)] | |
[ValidateSet('5', '6')] | |
[System.Int32]$Version = 6 | |
) | |
BEGIN { | |
} # end BEGIN block | |
PROCESS { | |
switch ($Version) { | |
5 { | |
$powerCliSnapins = $null | |
$powerCliSnapins = (Get-PSSnapin -Registered | Where-Object { $_.Name -like '*vmware*' }).Name | |
foreach ($visnapin in $powerCLI) { | |
try { | |
Write-Verbose -Message "Adding '$visnapin' PSSnapin" | |
Add-PSSnapin -Name $visnapin | |
} catch { | |
Write-Warning -Message "Could not add VMware PSSnapin '$visnapin'" | |
} # end try/catch | |
} # end foreach $visnapin | |
} # end 5 | |
6 { | |
$powerCliSnapins = $null | |
$powerCliSnapins = (Get-PSSnapin -Registered | Where-Object { $_.Name -like '*vmware*' }).Name | |
foreach ($visnapin in $powerCliSnapins) { | |
try { | |
Write-Verbose "Adding '$visnapin' PSSnapin" | |
Add-PSSnapin -Name $visnapin | |
} catch { | |
Write-Warning -Message "Could not add VMware PSSnapin '$visnapin'" | |
} # end try/catch | |
} # end foreach $visnapin | |
$powerCliModules = $null | |
$powerCliModules = (Get-Module -List | Where-Object { $_.Name -like '*vmware*' }).Name | |
foreach ($vimodule in $powerCliModules) { | |
try { | |
Write-Verbose "Importing '$vimodule' PS Module" | |
Import-Module -Name $vimodule | |
} catch { | |
Write-Warning -Message "Could not import PS Module '$vimodule'" | |
} # end try/catch | |
} # end foreach $vimodule | |
} # end 6 | |
} # end switch | |
} # end PROCESS block | |
END { | |
} # end END block | |
} # end function Start-PowerCLI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment