-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnable-PowerCLI.ps1
More file actions
153 lines (140 loc) · 4.63 KB
/
Enable-PowerCLI.ps1
File metadata and controls
153 lines (140 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<#PSScriptInfo
.DESCRIPTION
Autoloader script for VMware PowerCLI
.VERSION
1.0.0
.GUID
47bef1e2-4587-449b-a336-64c42979c9cc
.AUTHOR
Luc Dekens @LucD22
.TAGS
VMware PowerCLI
.PROJECTURI
https://github.com/lucdekens/Scripts/blob/master/Enable-PowerCLI.ps1
#>
Function Enable-PowerCLI{
<#
.SYNOPSIS
Load PowerCLI modules and PSSnapins
.DESCRIPTION
This function will load all requested PowerCLI
modules and PSSnapins.
The function will, depending on the installed PowerCLI version,
determine what needs to be loaded.
.NOTES
Author: Luc Dekens
.PARAMETER Cloud
Switch to indicate if the Cloud related cmdlets shall be loaded
.PARAMETER InitScript
The PowerCLI PSSnapin have associated initialisation scripts.
This switch will indicate if that script needs to be executed or not.
.EXAMPLE
PS> Enable-PowerCLI
.EXAMPLE
PS> Enable-PowerCLI -Cloud
#>
[CmdletBinding()]
param(
[Switch]$Cloud,
[Switch]$InitScript
)
$PcliPssnapin = @{
'VMware.VimAutomation.License' = @(2548067)
'VMware.DeployAutomation' =@(2548067,3056836,3205540,3737840)
'VMware.ImageBuilder' = @(2548067,3056836,3205540,3737840)
}
$PcliModule = @{
'VMware.VimAutomation.Core' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.Vds' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.Cloud' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.PCloud' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.Cis.Core' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.Storage' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.HA' = @(2548067,3056836,3205540,3737840)
'VMware.VimAutomation.vROps' = @(3056836,3205540,3737840)
'VMware.VumAutomation' = @(3056836,3205540,3737840)
'VMware.VimAutomation.License' = @(3056836,3205540,3737840)
}
# 32- or 64-bit process
$procArch = (Get-Process -Id $pid).StartInfo.EnvironmentVariables["PROCESSOR_ARCHITECTURE"]
if($procArch -eq 'x86'){
$regPath = 'HKLM:\Software\VMware, Inc.\VMware vSphere PowerCLI'
}
else{
$regPath = 'HKLM:\Software\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
}
# Check if PowerCLI (regular or Tenant) is installed
if(!(Test-Path -Path $regPath))
{
$regPath = $regPath.Replace('VMware vSphere PowerCLI','VMware vSphere PowerCLI for Tenants')
if(!(Test-Path -Path $regPath))
{
Throw 'Can not find a PowerCLI installation!'
}
}
# Get build
$buildKey = 'InstalledBuild'
Try{
$pcliBuild = Get-ItemProperty -Path $regPath -Name $buildKey |
Select -ExpandProperty $buildKey -ErrorAction Stop
}
Catch{
Throw "PowerCLI doesn't seem to be installed on this system!"
}
# Get installation path
$installPathKey = 'InstallPath'
Try{
$pcliInstallPath = Get-ItemProperty -Path $regPath -Name $installPathKey |
Select -ExpandProperty $installPathKey -ErrorAction Stop
}
Catch{
Throw "PowerCLI doesn't seem to be installed on this system!"
}
# Load modules
if($pcliBuild -ge 2548067)
{
$loadedModule = Get-Module -Name VMware* -ErrorAction SilentlyContinue | %{$_.Name}
if($loadedModule -and $pcliBuild -ge 3737840)
{
$loadedModule = $loadedModule | where{$_ -notmatch 'Common$|SDK$'}
}
$targetModule = $PcliModule.GetEnumerator() | where{$_.Value -contains $pcliBuild} | %{$_.Key}
$targetModule = $targetModule | where{$loadedModule -notcontains $_}
if(!$Cloud)
{
$targetModule = $targetModule | where{$_ -notmatch 'Cloud'}
}
if($targetModule)
{
$targetModule | where{$loadedModule -notcontains $_.Name} | %{
Import-Module -Name $_ -Verbose:$false
}
}
}
# Load PSSnapin
$loadedSnap = Get-PSSnapin -Name VMware* -ErrorAction SilentlyContinue | %{$_.Name}
if($pcliBuild -ge 3737840)
{
$loadedSnap = $loadedSnap | where{$_ -notmatch 'Core$'}
}
$targetSnap = $PcliPssnapin.GetEnumerator() | where{$_.Value -contains $pcliBuild} | %{$_.Key}
$targetSnap = $targetSnap | where{$loadedSnap -notcontains $_}
if(!$Cloud)
{
$targetSnap = $targetSnap | where{$_ -notmatch 'Cloud'}
}
if($targetSnap)
{
$targetSnap | where{$loadedSnap -notcontains $_} | %{
Add-PSSnapin -Name $_ -Verbose:$false
# Run initialisation script
if($InitScript)
{
$filePath = "{0}Scripts\Initialize-{1}.ps1" -f $pcliInstallPath,$_.ToString().Replace(".", "_")
if (Test-Path $filePath) {
& $filePath
}
}
}
}
}