-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStart-UNMAP.psm1
99 lines (85 loc) · 3.84 KB
/
Start-UNMAP.psm1
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
function Start-UNMAP {
<#
.SYNOPSIS
Process SCSI UNMAP on VMware Datastores
.DESCRIPTION
This Function will process SCSI UNMAP on VMware Datastores via ESXCLI -V2
.Example
Start-UNMAP -ClusterName myCluster -DSWildcard *RAID5*
.Example
Start-UNMAP -ClusterName myCluster -DSWildcard *RAID5* -Verbose -WhatIf
.Notes
NAME: Start-UNMAP.psm1
AUTHOR: Markus Kraus
LASTEDIT: 23.09.2016
VERSION: 1.0
KEYWORDS: VMware, vSphere, ESXi, SCSI, VAAI, UNMAP
.Link
http://mycloudrevolution.com/
#Requires PS -Version 4.0
#Requires -Modules VMware.VimAutomation.Core, @{ModuleName="VMware.VimAutomation.Core";ModuleVersion="6.3.0.0"}
#>
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact='High')]
param(
[Parameter(Mandatory=$true, Position=0)]
[String]$ClusterName,
[Parameter(Mandatory=$true, Position=1)]
[String]$DSWildcard
)
Process {
$Validate = $true
#region: PowerCLI Session Timeout
Write-Verbose "Set Session Timeout ..."
$initialTimeout = (Get-PowerCLIConfiguration -Scope Session).WebOperationTimeoutSeconds
Set-PowerCLIConfiguration -Scope Session -WebOperationTimeoutSeconds -1 -Confirm:$False | Out-Null
#endregion
#region: Get Cluster
$Cluster = Get-Cluster -Name $ClusterName -ErrorAction SilentlyContinue
Write-Verbose "vSphere Cluster: $Cluster"
if (!$Cluster){Write-Error "No Cluster found!"; $Validate = $false}
#endregion
#region: Get Hosts
$ClusterHosts = $Cluster | Get-VMHost -ErrorAction SilentlyContinue | where {$_.ConnectionState -eq "Connected" -and $_.PowerState -eq "PoweredOn"}
Write-Verbose "vSphere Cluster Hosts: $ClusterHosts"
if (!$ClusterHosts){Write-Error "No Hosts found!"; $Validate = $false}
#endregion
#region: Get Datastores
$ClusterDataStores = $Cluster | Get-Datastore -ErrorAction SilentlyContinue | where {$_.Name -like $DSWildcard -and $_.State -eq "Available" -and $_.Accessible -eq "True"}
Write-Verbose "vSphere Cluster Datastores: $ClusterDataStores"
if (!$ClusterDataStores){Write-Error "No Datastores found!"; $Validate = $false}
#endregion
#region: Process Datastores
if ($Validate -eq $true) {
Write-Verbose "Starting Loop..."
foreach ($ClusterDataStore in $ClusterDataStores) {
Write-Verbose "vSphere Datastore to Process: $ClusterDataStore"
$myHost = $ClusterHosts[(Get-Random -Maximum ($ClusterHosts).count)]
Write-Verbose "vSphere Host to Process: $myHost"
$esxcli2 = $myHost | Get-ESXCLI -V2
$arguments = $esxcli2.storage.vmfs.unmap.CreateArgs()
$arguments.volumelabel = $ClusterDataStore
$arguments.reclaimunit = "256"
if ($PSCmdlet.ShouldProcess( $ClusterDataStore,"Starting UNMAP on $myHost")) {
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
try {
Write-Output "Starting UNMAP for $ClusterDataStore on $myHost..."
$esxcli2.storage.vmfs.unmap.Invoke($arguments)
}
catch {
Write-Output "A Error occured: " "" $error[0] ""
}
$stopwatch.Stop()
Write-Output "UNMAP duration: $($stopwatch.Elapsed.Minutes)"
}
}
}
else {
Write-Error "Validation Failed. Processing Loop Skipped!"
}
#endregion
#region: Revert PowerCLI Session Timeout
Write-Verbose "Revert Session Timeout ..."
Set-PowerCLIConfiguration -Scope Session -WebOperationTimeoutSeconds $initialTimeout -Confirm:$False | Out-Null
#endregion
}
}