-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcheckandfixUNMAP.ps1
More file actions
214 lines (204 loc) · 11.8 KB
/
checkandfixUNMAP.ps1
File metadata and controls
214 lines (204 loc) · 11.8 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
<#
*******Disclaimer:******************************************************
This scripts are offered "as is" with no warranty. While this
scripts is tested and working in my environment, it is recommended that you test
this script in a test lab before using in a production environment. Everyone can
use the scripts/commands provided here without any written permission but I
will not be liable for any damage or loss to the system.
************************************************************************
This script will:
-Check and fix host setting EnableBlockDelete if not enabled
-Check and fix host setting EnableVMFS6Unmap if not enabled
-Check and fix datastore setting ReclaimPriority if not enabled
All change operations are logged to a file.
This can be run directly from PowerCLI or from a standard PowerShell prompt. PowerCLI must be installed on the local host regardless.
Supports:
-vCenter 6.0 and later
-PowerCLI 6.3 R1 or later required
For info, refer to www.codyhosterman.com
#>
write-host "Please choose a directory to store the script log"
function ChooseFolder([string]$Message, [string]$InitialDirectory)
{
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, 0, $InitialDirectory)
$selectedDirectory = $folder.Self.Path
return $selectedDirectory
}
$logfolder = ChooseFolder -Message "Please select a log file directory" -InitialDirectory 'MyComputer'
$logfile = $logfolder + '\' + (Get-Date -Format o |ForEach-Object {$_ -Replace ':', '.'}) + "configureunmap.txt"
write-host "Script result log can be found at $logfile" -ForegroundColor Green
write-host "Checking and setting Automatic UNMAP Configuration for VMware on the ESXi hosts in this vCenter."
write-host "Script log information can be found at $logfile"
add-content $logfile ' __________________________'
add-content $logfile ' /++++++++++++++++++++++++++\'
add-content $logfile ' /++++++++++++++++++++++++++++\'
add-content $logfile ' /++++++++++++++++++++++++++++++\'
add-content $logfile ' /++++++++++++++++++++++++++++++++\'
add-content $logfile ' /++++++++++++++++++++++++++++++++++\'
add-content $logfile ' /++++++++++++/----------\++++++++++++\'
add-content $logfile ' /++++++++++++/ \++++++++++++\'
add-content $logfile ' /++++++++++++/ \++++++++++++\'
add-content $logfile ' /++++++++++++/ \++++++++++++\'
add-content $logfile ' /++++++++++++/ \++++++++++++\'
add-content $logfile ' \++++++++++++\ /++++++++++++/'
add-content $logfile ' \++++++++++++\ /++++++++++++/'
add-content $logfile ' \++++++++++++\ /++++++++++++/'
add-content $logfile ' \++++++++++++\ /++++++++++++/'
add-content $logfile ' \++++++++++++\ /++++++++++++/'
add-content $logfile ' \++++++++++++\'
add-content $logfile ' \++++++++++++\'
add-content $logfile ' \++++++++++++\'
add-content $logfile ' \++++++++++++\'
add-content $logfile ' \------------\'
add-content $logfile 'VMware VMFS UNMAP Settings Script v1.0'
add-content $logfile '----------------------------------------------------------------------------------------------------'
if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
if (Test-Path “C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”)
{
. “C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1” |out-null
}
elseif (Test-Path “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”)
{
. “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1” |out-null
}
if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) )
{
write-host ("PowerCLI not found. Please verify installation and retry.") -BackgroundColor Red
write-host "Terminating Script" -BackgroundColor Red
add-content $logfile ("PowerCLI not found. Please verify installation and retry.")
add-content $logfile "Terminating Script"
return
}
}
set-powercliconfiguration -invalidcertificateaction "ignore" -confirm:$false |out-null
if ((Get-PowerCLIVersion).build -lt 3737840)
{
write-host "This version of PowerCLI is too old, version 6.3 Release 1 or later is required (Build 3737840)" -BackgroundColor Red
write-host "Found the following build number:"
write-host (Get-PowerCLIVersion).build
write-host "Terminating Script" -BackgroundColor Red
write-host "Get it here: https://my.vmware.com/group/vmware/get-download?downloadGroup=PCLI630R1"
add-content $logfile "This version of PowerCLI is too old, version 6.3 Release 1 or later is required (Build 3737840)"
add-content $logfile "Found the following build number:"
add-content $logfile (Get-PowerCLIVersion).build
add-content $logfile "Terminating Script"
add-content $logfile "Get it here: https://my.vmware.com/web/vmware/details?downloadGroup=PCLI650R1&productId=614"
return
}
$vcenter = read-host "Please enter a vCenter IP or FQDN"
$Creds = $Host.ui.PromptForCredential("vCenter Credentials", "Please enter your vCenter username and password.", "","")
try
{
connect-viserver -Server $vcenter -Credential $Creds -ErrorAction Stop |out-null
add-content $logfile ('Connected to vCenter at ' + $vcenter)
add-content $logfile '----------------------------------------------------------------------------------------------------'
}
catch
{
write-host "Failed to connect to vCenter" -BackgroundColor Red
write-host $vcenter
write-host $Error[0]
write-host "Terminating Script" -BackgroundColor Red
add-content $logfile "Failed to connect to vCenter"
add-content $logfile $vcenter
add-content $logfile $Error[0]
add-content $logfile "Terminating Script"
return
}
write-host ""
add-content $logfile '----------------------------------------------------------------------------------------------------'
$hosts= get-vmhost
add-content $logfile "Iterating through all ESXi hosts..."
#Iterating through each host in the vCenter
write-host "Checking hosts for host-wide UNMAP settings. Only listing hosts that need changed. Refer to the log for details."
foreach ($esx in $hosts)
{
add-content $logfile "--------------------------------------------------------------------------------------------------------"
add-content $logfile "********************************************************************************************************"
add-content $logfile "--------------------------------------------------------------------------------------------------------"
add-content $logfile ("Examining the ESXi host named " + $esx.Name + " (" + $esx.ExtensionData.Config.Network.DnsConfig.HostName + '/' + $esx.ExtensionData.Config.Network.DnsConfig.Address + ")")
if ($esx.Version -like "6.*")
{
add-content $logfile ""
$enableblockdelete = $esx | Get-AdvancedSetting -Name VMFS3.EnableBlockDelete
if ($enableblockdelete.Value -eq 0)
{
add-content $logfile " EnableBlockDelete is currently disabled. Enabling..."
$enableblockdelete |Set-AdvancedSetting -Value 1 -Confirm:$false |out-null
add-content $logfile " EnableBlockDelete has been set to enabled."
write-host (" FIXED: EnableBlockDelete is now enabled on " + $esx.Name + " (" + $esx.ExtensionData.Config.Network.DnsConfig.HostName + '/' + $esx.ExtensionData.Config.Network.DnsConfig.Address + ")")
}
else
{
add-content $logfile " EnableBlockDelete for this host is correctly enabled and will not be altered."
}
if ($esx.Version -like "6.5.*")
{
add-content $logfile "Current ESXi is version 6.5"
$autounmap = $esx | Get-AdvancedSetting -Name VMFS3.EnableVMFS6Unmap
if ($autounmap.Value -eq 0)
{
add-content $logfile "EnableVMFS6Unmap is currently disabled. Enabling..."
$autounmap |Set-AdvancedSetting -Value 1 -Confirm:$false |out-null
add-content $logfile " EnableVMFS6Unmap has been set to enabled."
write-host (" FIXED: EnableVMFS6Unmap is now enabled on " + $esx.Name + " (" + $esx.ExtensionData.Config.Network.DnsConfig.HostName + '/' + $esx.ExtensionData.Config.Network.DnsConfig.Address + ")")
}
else
{
add-content $logfile " EnableVMFS6Unmap for this host is correctly enabled and will not be altered."
}
}
}
else
{
add-content $logfile " The current host is not version 6.x. Skipping..."
}
}
add-content $logfile "--------------------------------------------------------------------------------------------------------"
add-content $logfile "**********************Checking VMFS-6 datastores for Automatic UNMAP Setting...*************************"
add-content $logfile "--------------------------------------------------------------------------------------------------------"
write-host ""
write-host "Checking datastores for Automatic UNMAP setting. Only listing datastores that need changed. Refer to the log for details."
$datastores = get-datastore
foreach ($datastore in $datastores)
{
if ($datastore.Type -eq 'VMFS')
{
if ($datastore.ExtensionData.info.vmfs.version -like "6.*")
{
$esx = $datastore | get-vmhost | where-object {($_.version -like '6.5.*')}| where-object {($_.ConnectionState -eq 'Connected')} |Select-Object -last 1
$esxcli=get-esxcli -VMHost $esx -v2
add-content $logfile ""
add-content $logfile ("The VMFS named " + $datastore.name + " is VMFS version six. Checking Automatic UNMAP configuration...")
$unmapargs = $esxcli.storage.vmfs.reclaim.config.get.createargs()
$unmapargs.volumelabel = $datastore.name
$unmapresult = $esxcli.storage.vmfs.reclaim.config.get.invoke($unmapargs)
if ($unmapresult.ReclaimPriority -ne "low")
{
add-content $logfile (" Automatic Space Reclamation is not set to low. It is set to " + $unmapresult.ReclaimPriority)
add-content $logfile " Setting to low..."
$unmapsetargs = $esxcli.storage.vmfs.reclaim.config.set.createargs()
$unmapsetargs.volumelabel = $datastore.name
$unmapsetargs.reclaimpriority = "low"
$esxcli.storage.vmfs.reclaim.config.set.invoke($unmapsetargs) |Out-Null
add-content $logfile ("Automatic UNMAP was enabled on " + $datastore.Name + " via ESXi host " + $esx.Name + " (" + $esx.ExtensionData.Config.Network.DnsConfig.HostName + '/' + $esx.ExtensionData.Config.Network.DnsConfig.Address + ")")
write-host (" FIXED: Automatic UNMAP is now enabled on datastore " + $datastore.Name)
}
elseif ($unmapresult.ReclaimPriority -eq "low")
{
add-content $logfile (" Automatic Space Reclamation is correctly set to low.")
}
}
else
{
add-content $logfile ("The VMFS named " + $datastore.name + " is not VMFS version 6. Skipping...")
}
}
else
{
add-content $logfile ("The datastore named " + $datastore.name + " is not VMFS. Skipping...")
}
}
disconnect-viserver -Server $vcenter -confirm:$false
add-content $logfile "Disconnected vCenter connection"