Public/Get-vSANInfo.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function Get-vSANInfo {
    <#
     .SYNOPSIS
       Get basic vSAN Cluster information
     .DESCRIPTION
       Will get inventory information for a vSAN Cluster
       The following is gathered:
       vSAN Cluster Name, Cluster Type, Disk Claim Mode, Dedupe & Compression Enabled, Stretched Cluster Enabled,
       Oldest Disk Format Version, Total Disks, Total Disk Groups, vSAN Capacity GB
     .NOTES
       File Name : Get-vSANInfo.ps1
       Author : Graham Barker - @VirtualG_UK
       Contributor : Edgar Sanchez - @edmsanchez13
       Contributor : Ariel Sanchez - @arielsanchezmor
       Version : 2.4.3
     .Link
       https://github.com/arielsanchezmora/vDocumentation
     .INPUTS
       No inputs required
     .OUTPUTS
       CSV file
       Excel file
     .PARAMETER cluster
       The name(s) of the vSphere Cluster(s)
     .EXAMPLE
       Get-vSANInfo -cluster production-cluster
     .PARAMETER ExportCSV
       Switch to export all data to CSV file. File is saved to the current user directory from where the script was executed. Use -folderPath parameter to specify a alternate location
     .EXAMPLE
       Get-vSANInfo -cluster production-cluster -ExportCSV
     .PARAMETER ExportExcel
       Switch to export all data to Excel file (No need to have Excel Installed). This relies on ImportExcel Module to be installed.
       ImportExcel Module can be installed directly from the PowerShell Gallery. See https://github.com/dfinke/ImportExcel for more information
       File is saved to the current user directory from where the script was executed. Use -folderPath parameter to specify a alternate location
     .EXAMPLE
       Get-vSANInfo -cluster production-cluster -ExportExcel
     .PARAMETER folderPath
       Specify an alternate folder path where the exported data should be saved.
     .EXAMPLE
       Get-vSANInfo -ExportExcel -folderPath C:\temp
     .PARAMETER PassThru
       Switch to return object to command line
     .EXAMPLE
       Get-vSANInfo -cluster production-cluster
    #>
 
    
    <#
     ----------------------------------------------------------[Declarations]----------------------------------------------------------
    #>

    [CmdletBinding()]
    param (
        $cluster,
        [switch]$ExportCSV,
        [switch]$ExportExcel,
        [switch]$PassThru,
        $folderPath
    )
    
    $configurationCollection = @()
    $skipCollection = @()
    $vSANClusterList = @()
    $returnCollection = @()
    $date = Get-Date -format s
    $date = $date -replace ":", "-"
    $outputFile = "vSAN-info" + $date

    <#
     ----------------------------------------------------------[Execution]----------------------------------------------------------
    #>

  
    <#
      Query PowerCLI and vDocumentation versions if
      running Verbose
    #>

    if ($VerbosePreference -eq "continue") {
        Write-Verbose -Message ((Get-Date -Format G) + "`tPowerCLI Version:")
        Get-Module -Name VMware.* | Select-Object -Property Name, Version | Format-Table -AutoSize
        Write-Verbose -Message ((Get-Date -Format G) + "`tvDocumentation Version:")
        Get-Module -Name vDocumentation | Select-Object -Property Name, Version | Format-Table -AutoSize
    } #END if

    <#
      Check for an active connection to a VIServer
      needs to be vCenter Server 6.5+
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate connection to a vCenter server and that it's running at least v6.5.0")
    if ($Global:DefaultViServers.Count -gt 0 -and $Global:DefaultViServers.Version -ge "6.5.0") {
        $vCenterVersion = $Global:DefaultViServers.Version
        Write-Host "`tConnected to vCenter: $Global:DefaultViServers, v$vCenterVersion" -ForegroundColor Green
    }
    else {
        Write-Error -Message "You must be connected to a vCenter Server running at least v6.5.0 server before running this Cmdlet."
        break
    } #END if/else
    
    <#
      Validate parameter (-cluster) and gather cluster list.
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate parameters used")
    if ([string]::IsNullOrWhiteSpace($cluster) ) {
        Write-Verbose -Message ((Get-Date -Format G) + "`tA parameter (-cluster) was not specified. Will gather all clusters")
        Write-Host "`tGathering all clusters from the following vCenter(s): " $Global:DefaultViServers
        $vSANClusterList = Get-Cluster | Sort-Object -Property Name
    }
    else {
        Write-Verbose -Message ((Get-Date -Format G) + "`tExecuting Cmdlet using cluster parameter")
        Write-Host "`tGathering cluster list..."
        foreach ($vClusterName in $cluster) {
            $tempList = Get-Cluster -Name $vClusterName.Trim() -ErrorAction SilentlyContinue
            if ([string]::IsNullOrWhiteSpace($tempList)) {
                Write-Warning -Message "`tCluster with name $vClusterName was not found in $Global:DefaultViServers"
            }
            else {
                $vSANClusterList += $tempList | Sort-Object -Property Name
            } #END if/else
        } #END foreach
    } #END if/else
    
    <#
      Validate export switches,
      folder path and dependencies
    #>

    Write-Verbose -Message ((Get-Date -Format G) + "`tValidate export switches and folder path")
    if ($ExportCSV -or $ExportExcel) {
        $currentLocation = (Get-Location).Path
        if ([string]::IsNullOrWhiteSpace($folderPath)) {
            Write-Verbose -Message ((Get-Date -Format G) + "`t-folderPath parameter is Null or Empty")
            Write-Warning -Message "`tFolder Path (-folderPath) was not specified for saving exported data. The current location: '$currentLocation' will be used"
            $outputFile = $currentLocation + "\" + $outputFile
        }
        else {
            if (Test-Path $folderPath) {
                Write-Verbose -Message ((Get-Date -Format G) + "`t'$folderPath' path found")
                $lastCharsOfFolderPath = $folderPath.Substring($folderPath.Length - 1)
                if ($lastCharsOfFolderPath -eq "\" -or $lastCharsOfFolderPath -eq "/") {
                    $outputFile = $folderPath + $outputFile
                }
                else {
                    $outputFile = $folderPath + "\" + $outputFile
                } #END if/else
                Write-Verbose -Message ((Get-Date -Format G) + "`t$outputFile")
            }
            else {
                Write-Warning -Message "`t'$folderPath' path not found. The current location: '$currentLocation' will be used instead"
                $outputFile = $currentLocation + "\" + $outputFile
            } #END if/else
        } #END if/else
    } #END if
    if ($ExportExcel) {
        if (Get-Module -ListAvailable -Name ImportExcel) {
            Write-Verbose -Message ((Get-Date -Format G) + "`tImportExcel Module available")
        }
        else {
            Write-Warning -Message "`tImportExcel Module missing. Will export data to CSV file instead"
            Write-Warning -Message "`tImportExcel Module can be installed directly from the PowerShell Gallery"
            Write-Warning -Message "`tSee https://github.com/dfinke/ImportExcel for more information"
            $ExportExcel = $false
            $ExportCSV = $true
        } #END if/else
    } #END if
       
    <#
      Main code execution
    #>

    foreach ($vSAN in $vSANClusterList) {
  
        <#
          Skip if cluster is not vSAN enabled
        #>

        if ($vSAN.VsanEnabled -eq $true) {
            <#
              Do nothing - Cluster is vSAN Enabled
            #>

        }
        else {
            <#
              Use a custom object to keep track of skipped
              clusters and continue to the next foreach loop
            #>

            $skipCollection += [pscustomobject]@{
                'Cluster' = $vSAN.Name
                'Status'  = "Not vSAN Enabled"
            } #END [PSCustomObject]
            continue
        } #END if/else

        <#
          Get vSAN oldest system version
        #>

        Write-Host "`tGathering configuration details from vSAN Cluster: $vSAN ..."
        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering claimed disks configuration...")
        $clusterMoRef = $vSAN.ExtensionData.MoRef
        $vSanClusterHealth = Get-VSANView -Id "VsanVcClusterHealthSystem-vsan-cluster-health-system"
        $vSanSystemVersions = $vSanClusterHealth.VsanVcClusterQueryVerifyHealthSystemVersions($clusterMoRef)
        $oldestvSanSystemVersion = $vSanSystemVersions.HostResults | Select-Object -ExpandProperty Version | Sort-Object -Descending | Select-Object -Last 1

        <#
          Get vSAN configuration details
        #>

        $vSAN = $vSAN | Get-VsanClusterConfiguration
        $vSanDiskGroups = Get-VsanDiskGroup -Cluster $vSAN.name
        $vSanDisks = Get-VsanDisk -vSANDiskGroup $vSanDiskGroups
        $numberDisks = $vSanDisks.Count
        $vSanView = $vSAN | Get-View

        <#
          Get vSAN oldest disk format version
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering disk format Configuration...")
        $oldestDiskFormatVersion = $vSanDisks.DiskFormatVersion | Sort-Object -Unique | Select-Object -First 1
                   
        <#
          Get number of disk groups
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering disk group configuration...")
        $numberDiskGroups = $vSanDiskGroups.Count
    
        <#
          Get vSAN cluster type
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering cluster type Configuration...")
        $magneticDiskCounter = ($vSanDisks | Where-Object {$_.IsSsd -eq $true}).Count
        if ($magneticDiskCounter -gt 0) {
            $clusterType = "Hybrid"
        }
        else {
            $clusterType = "Flash"
        } #END if/else

        <#
          Get disk claim mode
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering disk claim mode configuration...")
        $diskClaimMode = $vSAN.VsanDiskClaimMode

        <#
          Get deduplication & compression
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering deduplication & compression configuration...")
        $deduplicationCompression = $vSAN.SpaceEfficiencyEnabled

        <#
          Get stretched cluster
        #>

        Write-Verbose -Message ((Get-Date -Format G) + "`tGathering stretched cluster configuration...")
        $stretchedCluster = $vSAN.StretchedClusterEnabled
            
        <#
          Get vSAN Capacity
        #>

        $vSanDS = Get-View $vSanView.Datastore
        $vSanDsCapacityGB = [math]::round(($vSanDS.Summary.Capacity) / 1GB, 2)
        $vSanProvisionedGB = [math]::round(($vSanDS.Summary.Capacity - $vSanDS.Summary.FreeSpace + $vSanDS.Summary.Uncommitted) / 1GB, 2)
        $vSanDsFreeGB = [math]::round(($vSanDS.Summary.FreeSpace) / 1GB, 2)

        <#
          Get vSAN Storage Policy
        #>

        $vSanPolicy = (Get-SpbmStoragePolicy -Name $vSAN.StoragePolicy).AnyOfRuleSets.allofrules
        $vSanFailureToTolerate = $vSanPolicy | Where-Object {$_.Capability -like "VSAN.hostFailuresToTolerate"} | Select-Object -ExpandProperty Value
        $vSanStripeWidth = $vSanPolicy | Where-Object {$_.Capability -like "VSAN.stripeWidth"} | Select-Object -ExpandProperty Value

        <#
          Use a custom object to store collected data
        #>

        $configurationCollection += [PSCustomObject]@{
            'vSAN Cluster Name'                   = $vSAN.Name
            'Effective Hosts'                     = $vSanView.Summary.NumEffectiveHosts
            'Oldest vSAN Version'                 = $oldestvSanSystemVersion
            'Oldest Disk Format'                  = $oldestDiskFormatVersion
            'Cluster Type'                        = $clusterType
            'Disk Claim Mode'                     = $diskClaimMode
            'Deduplication & Compression Enabled' = $deduplicationCompression
            'Stretched Cluster Enabled'           = $stretchedCluster
            'Host Failures To Tolerate'           = $vSanFailureToTolerate
            'vSAN Stripe Width'                   = $vSanStripeWidth
            'Total vSAN Claimed Disks'            = $numberDisks
            'Total Disk Groups'                   = $numberDiskGroups
            'Total Capacity (GB)'                 = $vSanDsCapacityGB
            'Provisioned Space (GB)'              = $vSanProvisionedGB
            'Free Space (GB)'                     = $vSanDsFreeGB
        } #END [PSCustomObject]
    } #END foreach
    Write-Verbose -Message ((Get-Date -Format G) + "`tMain code execution completed")

    <#
      Display skipped clusters and their vSAN status
    #>

    If ($skipCollection) {
        Write-Host "`n"
        Write-Warning -Message "`tCheck vSAN configuration or cluster name"
        Write-Warning -Message "`tSkipped cluster(s):"
        $skipCollection | Format-Table -AutoSize
    } #END if

    <#
      Validate output arrays
    #>

    if ($configurationCollection) {
        Write-Verbose -Message ((Get-Date -Format G) + "`tInformation gathered")
    }
    else {
        Write-Verbose -Message ((Get-Date -Format G) + "`tNo information gathered")
    } #END if/else
    
    <#
      Output to screen
      Export data to CSV, Excel
    #>
   
    if ($configurationCollection) {
        Write-Host "`n" "vSAN Configuration:" -ForegroundColor Green
        if ($ExportCSV) {
            $configurationCollection | Export-Csv ($outputFile + "Configuration.csv") -NoTypeInformation
            Write-Host "`tData exported to" ($outputFile + "Configuration.csv") "file" -ForegroundColor Green
        }
        elseif ($ExportExcel) {
            $configurationCollection | Export-Excel ($outputFile + ".xlsx") -WorkSheetname vSAN_Configuration -NoNumberConversion * -BoldTopRow
            Write-Host "`tData exported to" ($outputFile + ".xlsx") "file" -ForegroundColor Green
        }
        elseif ($PassThru) {
            $returnCollection += $configurationCollection
            $returnCollection  
        }
        else {
            $configurationCollection | Format-List
        }#END if/else
    } #END if
} #END function