-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvCollect-Products-AllvCenter.ps1
204 lines (189 loc) · 9.66 KB
/
vCollect-Products-AllvCenter.ps1
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
##=================================================================================
## Project: vCollect Products for All vCenter
## AUTHOR: SADDAM ZEMMALI
## eMail: [email protected]
## CREATED: 14.07.2018 02:03:01
## REVISION: --
## Version: 1.0 ¯\_(ツ)_/¯
## Repository: https://github.com/szemmali/vCollect-SDDC-SZ
## Task: vCollect Products for All vCenter
## FILE: vCollect-Products-AllvCenter.ps1
## Description: vCollect Products for All vCenter
## Requirement: --
## Note: Connect With USERNAME/PASSWORD Credential
## BUGS: Set-ExecutionPolicy -ExecutionPolicy Bypass
##=================================================================================
#################################
# vCollect Targeting Variables #
#################################
$StartTime = Get-Date
if (-not (Test-Path '.\vCollect-Reports')) { New-Item -Path '.\vCollect-Reports' -ItemType Directory -Force | Out-Null }
$report= ".\vCollect-Reports"
$export="vCollect-Products"
$dateF = Get-Date -UFormat "%d-%b-%Y_%H-%M-%S"
##############################
# Check the required modules #
##############################
function check-Module ($m) {
# If module is imported say that and do nothing
if (Get-Module | Where-Object {$_.Name -eq $m}) {
write-host "Module $m " -f Magenta -NoNewLine
write-host "is already imported." -f Green
} else {
# If module is not imported, but available on disk then import
Write-Warning "Module $m is NOT imported (must be installed before starting)."
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
Import-Module $m -Verbose
} else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
Import-Module $m -Verbose
} else {
# If module is not imported, not available and not in online gallery then abort
Write-Warning "Module $m not imported, not available and not in online gallery, exiting."
EXIT 1
}
}
}
}
check-Module "CredentialManager"
check-Module "VMware.PowerCLI"
#####################################
# vCollect Targeting Report Folder #
#####################################
function vcollect-check-folder ($dir) {
if(!(Test-Path -Path $report\$dir )){
New-Item -ItemType directory -Path $report\$dir
Write-Host "New $dir folder created" -f Magenta
New-Item -Path $report\$dir -Name $dateF -ItemType "directory"
Write-Host "New Work folder created" -f Magenta
}
else{
Write-Host "$dir Folder already exists" -f Green
New-Item -Path $report\$dir -Name $dateF -ItemType "directory"
Write-Host "New Work folder created" -f Magenta
}
}
vcollect-check-folder $export
#################################
# vSphere Targeting Variables #
#################################
$vCenterList = Get-Content "vCenter.txt"
$username = Read-Host 'Enter The vCenter Username'
$password = Read-Host 'Enter The vCenter Password' -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($username, $password)
#################################
# vCheck Targeting Variables #
#################################
$ReportInfo="All Products per vCenter Info Report"
#################################
# LOG INFO #
#################################
$PathH = "$report\$export\$dateF"
$DCReport = @()
# XLSX Reports
$ReportXls = "vCollect_All_Products_Report_All-vCenter_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") +".xlsx"
# CVS Reports
$ReportCSV = "_vCollect_All_Products_Report_By-vCenter_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"
$ReportCsvAll = "vCollect_All_Products_Report_All-vCenter_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"
######################################################################
######################################################################
$TotalVcCount = $vCenterList.count
Write-Host "There are $TotalVcCount vCenter" -Foregroundcolor "Cyan"
$h=0
#################################
# Start vCollect By vCenter #
#################################
$AllDCProducts=0
Disconnect-VIServer -Force -confirm:$false -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
foreach ($vCenter in $vCenterList){
$h++
Write-Host "Connecting to $vCenter..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $vCenter -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
If($? -Eq $True){
Write-Host "Connected" -Foregroundcolor "Green"
Write-Progress -Activity "vCollecting vCenter" -Status ("vCenter: {0}" -f $vCenter) -PercentComplete ($h/$TotalVcCount*100) -Id 0
##############################
# Gathering information #
##############################
Write-Host "Gathering Products Informations From vCenter: $vCenter"
$VMs = Get-VM * | where { $_.ExtensionData.Summary.Config.Product.Name -ne $null }
$AllvProducts=$VMs.count
$AllDCProducts=$AllDCProducts+$AllvProducts
Write-Host "# There are $AllProducts Products in vCenter: $vCenter" -Foregroundcolor "Yellow"
###################################
# Gathering Products informations #
###################################
Write-Host "Gathering Products Informations" -Foregroundcolor "Cyan"
$i=0
$Report = @()
foreach ($VM in $VMs) {
$i++
Write-Progress -Activity "vCollecting Products" -Status ("VM: {0}" -f $VM.Name) -PercentComplete ($i/$AllvProducts*100) -Id 1 -ParentId 0
#Datacenter info
$datacenter = $VM | Get-Datacenter | Select-Object -ExpandProperty name
#Cluster info
$cluster = $VM | Get-Cluster | Select-Object -ExpandProperty name
#vCenter Server
$vCenter = $VM.ExtensionData.Client.ServiceUrl.Split('/')[2].trimend(":443")
$Product = New-Object PSObject
$Product | add-member -MemberType NoteProperty -Name "Data Center" -Value $datacenter
$Product | add-member -MemberType NoteProperty -Name "vCenter" -Value $vCenter
$Product | add-member -MemberType NoteProperty -Name "Cluster" -Value $cluster
$Product | add-member -MemberType NoteProperty -Name "Host" -Value $VM.VMHost
$Product | add-member -MemberType NoteProperty -Name "VM Name" -Value $VM.name;
$Product | add-member -MemberType NoteProperty -Name "Product Name" -Value $VM.extensiondata.summary.config.product.name;
$Product | add-member -MemberType NoteProperty -Name "Version" -Value $VM.extensiondata.summary.config.product.version;
$Product | add-member -MemberType NoteProperty -Name "Full Version" -Value $VM.extensiondata.summary.config.product.fullversion;
$Product | add-member -MemberType NoteProperty -Name "Product URL" -Value $VM.extensiondata.summary.config.product.producturl;
$Product | add-member -MemberType NoteProperty -Name "App URL" -Value $VM.extensiondata.summary.config.product.appurl
$report += $Product
}
########################################
# END Collect HW Info by vCenter #
########################################
Write-Host "Export $ReportInfo from vcenter: $vCenter" -Foregroundcolor "Green"
$Report | Export-Csv $PathH\$vCenter$ReportCSV -NoTypeInformation -UseCulture
$Report | Export-Excel -Path $PathH\$ReportXls -WorkSheetname "$vCenter"
#Invoke-Item $PathS\$vCenter$ReportCSV
$DCReport +=$Report
}
Else{
Write-Host "Error in Connecting to $vCenter; Try Again with correct user name & password!" -Foregroundcolor "Red"
}
########################################
# END Collect HW Info from All vCenter #
########################################
Write-Host "Export All $ReportInfo from All vCenter" -Foregroundcolor "Green"
$DCReport | Export-Csv $PathH\$ReportCsvAll -NoTypeInformation -UseCulture
$DCReport | Export-Excel -Path $PathH\$ReportXls -WorkSheetname "All vCenter"
##############################
# Disconnect session from VC #
##############################
Write-Host "Disconnect to $vCenter..." -Foregroundcolor "Yellow" -NoNewLine
$disconnection =Disconnect-VIServer -Server $vCenter -Force -confirm:$false -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
If($? -Eq $True){
Write-Host "Disconnected" -Foregroundcolor "Green"
Write-Host "#####################################" -Foregroundcolor "Blue"
}
Else{
Write-Host "Error in Disconnecting to $vCenter" -Foregroundcolor "Red"
}
}
#################################
# End vCollect By vCenter #
#################################
#Invoke-Item $PathH \$ReportXls
##############################
# End of Script #
##############################
$EndTime = Get-Date
$duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)
Write-Host "================================"
Write-Host "vCollect All Products for each vCenter Completed!"
Write-Host "There are $AllDCProducts VM Products in $TotalVcCount vCenter" -Foregroundcolor "Cyan"
Write-Host "StartTime: $StartTime"
Write-Host " EndTime: $EndTime"
Write-Host " Duration: $duration minutes"
Write-Host "================================"