forked from vmware-archive/PowerCLI-Example-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate_PowerCLI_Scripts.ps1
More file actions
228 lines (177 loc) · 8.98 KB
/
Update_PowerCLI_Scripts.ps1
File metadata and controls
228 lines (177 loc) · 8.98 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
function Get-PowerCLIInitialization {
<#
.SYNOPSIS
Gathers information on PowerShell resources which refer to the old PowerCLI Initialization Script
.DESCRIPTION
Will provide an inventory of scripts, modules, etc refering to the old vSphere PowerCLI directory
.NOTES
Author: Kyle Ruddy, @kmruddy, thatcouldbeaproblem.com
.PARAMETER Path
Directory path to be searched
.EXAMPLE
Get-PowerCLIInitialization -Path C:\Temp\Scripts
Gathers information from the 'C:\Temp\Scripts\' directory
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false,Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$Path
)
Process {
#Validate whether a path was passed as a parameter or not
if (!$Path) {$FullName = (Get-Location).ProviderPath}
else {
#Validate whether the path passed as a parameter was a literal path or not, then establish the FullName of the desired path
if ((Test-Path -Path $Path -ErrorAction SilentlyContinue) -eq $true -and $Path -is [System.IO.DirectoryInfo]) {$FullName = (Get-Item -Path $Path).FullName}
elseif ((Test-Path -Path $Path -ErrorAction SilentlyContinue) -eq $true -and $Path -isnot [System.IO.DirectoryInfo]) {$FullName = (Get-Item -LiteralPath $Path).FullName}
else {
$currdir = (Get-Location).ProviderPath
Write-Warning "No valid path found at - $currdir\$Path"
}
}
if ($FullName) {
#Gather scripts using ps1 extension and have a string matching "\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
$scripts = Get-ChildItem -Path $FullName -Recurse -Filter *.ps1 -File -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | Select-String -Pattern "\\VMware\\Infrastructure\\vSphere PowerCLI\\Scripts\\Initialize-PowerCLIEnvironment.ps1"
#Create a report of neccessary output based on the scripts gathered above
$scriptreport = @()
foreach ($script in $scripts) {
$singleitem = New-Object System.Object
$singleitem | Add-Member -Type NoteProperty -Name Filename -Value $script.Filename
$singleitem | Add-Member -Type NoteProperty -Name LineNumber -Value $script.LineNumber
$singleitem | Add-Member -Type NoteProperty -Name Path -Value $script.Path.TrimEnd($script.Filename)
$singleitem | Add-Member -Type NoteProperty -Name FullPath -Value $script.Path
$scriptreport += $singleitem
}
return $scriptreport
}
} # End of process
} # End of function
function Get-PowerCLISnapinUse {
<#
.SYNOPSIS
Gathers information on PowerShell resources which refer to the old PowerCLI Snapins
.DESCRIPTION
Will provide an inventory of scripts, modules, etc refering to the old PowerCLI Snapins
.NOTES
Author: Kyle Ruddy, @kmruddy, thatcouldbeaproblem.com
.PARAMETER Path
Directory path to be searched
.EXAMPLE
Get-PowerCLISnapinUse -Path C:\Temp\Scripts
Gathers information from the 'C:\Temp\Scripts\' directory
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false,Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$Path
)
Process {
#Validate whether a path was passed as a parameter or not
if (!$Path) {$FullName = (Get-Location).ProviderPath}
else {
#Validate whether the path passed as a parameter was a literal path or not, then establish the FullName of the desired path
if ((Test-Path -Path $Path -ErrorAction SilentlyContinue) -eq $true -and $Path -is [System.IO.DirectoryInfo]) {$FullName = (Get-Item -Path $Path).FullName}
elseif ((Test-Path -Path $Path -ErrorAction SilentlyContinue) -eq $true -and $Path -isnot [System.IO.DirectoryInfo]) {$FullName = (Get-Item -LiteralPath $Path).FullName}
else {
$currdir = (Get-Location).ProviderPath
Write-Warning "No valid path found at - $currdir\$Path"
}
}
if ($FullName) {
#Gather scripts using ps1 extension and have a string matching "add-pssnapin" and "VMware." on the same line
$scripts = Get-ChildItem -Path $FullName -Recurse -Filter *.ps1 -File -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | Select-String -Pattern "add-pssnapin" | Where-Object {$_ | Select-String -Pattern "VMware."}
#Create a report of neccessary output based on the scripts gathered above
$scriptreport = @()
foreach ($script in $scripts) {
$singleitem = New-Object System.Object
$singleitem | Add-Member -Type NoteProperty -Name Filename -Value $script.Filename
$singleitem | Add-Member -Type NoteProperty -Name LineNumber -Value $script.LineNumber
$singleitem | Add-Member -Type NoteProperty -Name Path -Value $script.Path.TrimEnd($script.Filename)
$singleitem | Add-Member -Type NoteProperty -Name FullPath -Value $script.Path
$scriptreport += $singleitem
}
return $scriptreport
}
} # End of process
} # End of function
function Update-PowerCLIInitialization {
<#
.SYNOPSIS
Updates the information in PowerShell resources which refer to the old PowerCLI Initialization Script
.DESCRIPTION
Will update scripts, modules, etc refering to the old vSphere PowerCLI directory
.NOTES
Author: Kyle Ruddy, @kmruddy, thatcouldbeaproblem.com
.PARAMETER Path
Directory path to be searched
.EXAMPLE
Update-PowerCLIInitialization -Path C:\Temp\Scripts
Gathers information from the 'C:\Temp\Scripts\' directory
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$false,Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$Path
)
Process {
#Gather scripts using ps1 extension and have a string matching "\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
$scripts = Get-PowerCLIInitialization -Path $Path
#Check to see if any scripts are found
if (!$scripts) {Write-Warning "No PowerShell resources found requiring update within $Path"}
else {
foreach ($script in $scripts) {
#Finds and updates the string to the new location of the Initialize-PowerCLIEnvironment.ps1 script
(Get-Content $script.FullPath).Replace("\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1","\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1") | Set-Content $script.FullPath
}
}
} # End of process
} # End of function
function Update-PowerCLISnapinUse {
<#
.SYNOPSIS
Updates the information in PowerShell resources which refer to the old PowerCLI Initialization Script
.DESCRIPTION
Will update scripts, modules, etc refering to the old vSphere PowerCLI directory
.NOTES
Author: Kyle Ruddy, @kmruddy, thatcouldbeaproblem.com
.PARAMETER Path
Directory path to be searched
.EXAMPLE
Update-PowerCLISnapinUse -Path C:\Temp\Scripts
Gathers information from the 'C:\Temp\Scripts\' directory
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory=$false,Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$Path
)
Process {
#Gather scripts using ps1 extension and have a string matching "\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
$scripts = Get-PowerCLISnapinUse -Path $Path | select -Unique FullPath
#Check to see if any scripts are found
if (!$scripts) {Write-Warning "No PowerShell resources found requiring update within $Path"}
else {
foreach ($script in $scripts) {
#Finds and updates the string to import the PowerCLI modules
$scriptcontent = (Get-Content $script.FullPath)
$newoutput = @()
[int]$counter = 0
foreach ($line in $scriptcontent) {
#Checks to see if the line includes adding in the VMware PSSnapins and, if so, comments it out
#On first discovery, adds the invokation of get-module for the PowerCLI modules
if ($line -like "Add-PSSnapin*VMware*" -and $counter -eq 0) {
$newoutput += "Get-Module -ListAvailable VMware* | Import-Module"
$newoutput += $line.Insert(0,'#')
$counter = 1
}
elseif ($line -like "Add-PSSnapin*VMware*" -and $counter -eq 1) {
$newoutput += $line.Insert(0,'#')
}
else {$newoutput += $line}
}
#Updates the script
Set-Content -Value $newoutput -Path $script.FullPath
}
}
} # End of process
} # End of function