-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathWindowsFeatures.ps1
More file actions
244 lines (206 loc) · 6.76 KB
/
WindowsFeatures.ps1
File metadata and controls
244 lines (206 loc) · 6.76 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<#
.SYNOPSIS
Process Windows Features
.DESCRIPTION
This script can return a list of online windows features and/or set specific windows features.
.PARAMETER ListFeatures
Return a list of all Windows Features
.PARAMETER Feature
A description of the Feature parameter.
.PARAMETER Setting
A description of the Setting parameter.
.PARAMETER FeaturesFile
Name of the features file that contains a list of features with their corresponding settings for this script to process through. The files resides in the same directory as this script.
.EXAMPLE
Return a list of all available online Windows Features
powershell.exe -executionpolicy bypass -command WindowsFeatures.ps1 -ListFeatures $true
Set one Windows Feature from the command line
powershell.exe -executionpolicy bypass -command WindowsFeatures.ps1 -Feature 'RSATClient-Features' -Setting 'disable'
Set multiple features by reading contents of a text file
powershell.exe -executionpolicy bypass -command WindowsFeatures.ps1 -FeaturesFile 'FeaturesList.txt'
.NOTES
You must use -command instead of -file in the command line because of the use of boolean parameters
An error code 50 means you are trying to enable a feature in which the required parent feature is disabled
I have also included two commented out lines at the bottom of the script as examples if you want to hardcode the features within the script.
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.122
Created on: 5/27/2016 2:46 PM
Created by: Mick Pletcher
Organization:
Filename: WindowsFeatures.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[boolean]$ListFeatures = $false,
[string]$Feature,
[ValidateSet('enable', 'disable')][string]$Setting,
[String]$FeaturesFile
)
function Confirm-Feature {
<#
.SYNOPSIS
Confirm the feature setting
.DESCRIPTION
Confirm the desired change took place for a feature
.PARAMETER FeatureName
Name of the feature
.PARAMETER FeatureState
Desired state of the feature
.EXAMPLE
PS C:\> Confirm-Feature
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([boolean])]
param
(
[ValidateNotNull()][string]$FeatureName,
[ValidateSet('Enable', 'Disable')][string]$FeatureState
)
$WindowsFeatures = Get-WindowsFeaturesList
$WindowsFeature = $WindowsFeatures | Where-Object { $_.Name -eq $FeatureName }
switch ($FeatureState) {
'Enable' {
If (($WindowsFeature.State -eq 'Enabled') -or ($WindowsFeature.State -eq 'Enable Pending')) {
Return $true
} else {
Return $false
}
}
'Disable' {
If (($WindowsFeature.State -eq 'Disabled') -or ($WindowsFeature.State -eq 'Disable Pending')) {
Return $true
} else {
Return $false
}
}
default {
Return $false
}
}
}
function Get-WindowsFeaturesList {
<#
.SYNOPSIS
List Windows Features
.DESCRIPTION
This will list all available online windows features
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param ()
$Temp = dism /online /get-features
$Temp = $Temp | Where-Object { ($_ -like '*Feature Name*') -or ($_ -like '*State*') }
$i = 0
$Features = @()
Do {
$FeatureName = $Temp[$i]
$FeatureName = $FeatureName.Split(':')
$FeatureName = $FeatureName[1].Trim()
$i++
$FeatureState = $Temp[$i]
$FeatureState = $FeatureState.Split(':')
$FeatureState = $FeatureState[1].Trim()
$Feature = New-Object PSObject
$Feature | Add-Member noteproperty Name $FeatureName
$Feature | Add-Member noteproperty State $FeatureState
$Features += $Feature
$i++
} while ($i -lt $Temp.Count)
$Features = $Features | Sort-Object Name
Return $Features
}
function Set-WindowsFeature {
<#
.SYNOPSIS
Configure a Windows Feature
.DESCRIPTION
Enable or disable a windows feature
.PARAMETER Name
Name of the windows feature
.PARAMETER State
Enable or disable windows feature
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$Name,
[Parameter(Mandatory = $true)][ValidateSet('enable', 'disable')][string]$State
)
$EXE = $env:windir + "\system32\dism.exe"
Write-Host $Name"....." -NoNewline
If ($State -eq "enable") {
$Parameters = "/online /enable-feature /norestart /featurename:" + $Name
} else {
$Parameters = "/online /disable-feature /norestart /featurename:" + $Name
}
$ErrCode = (Start-Process -FilePath $EXE -ArgumentList $Parameters -Wait -PassThru -WindowStyle Minimized).ExitCode
If ($ErrCode -eq 0) {
$FeatureChange = Confirm-Feature -FeatureName $Name -FeatureState $State
If ($FeatureChange -eq $true) {
If ($State -eq 'Enable') {
Write-Host "Enabled" -ForegroundColor Yellow
} else {
Write-Host "Disabled" -ForegroundColor Yellow
}
} else {
Write-Host "Failed" -ForegroundColor Red
}
} elseif ($ErrCode -eq 3010) {
$FeatureChange = Confirm-Feature -FeatureName $Name -FeatureState $State
If ($FeatureChange -eq $true) {
If ($State -eq 'Enable') {
Write-Host "Enabled & Pending Reboot" -ForegroundColor Yellow
} else {
Write-Host "Disabled & Pending Reboot" -ForegroundColor Yellow
}
} else {
Write-Host "Failed" -ForegroundColor Red
}
} else {
If ($ErrCode -eq 50) {
Write-Host "Failed. Parent feature needs to be enabled first." -ForegroundColor Red
} else {
Write-Host "Failed with error code "$ErrCode -ForegroundColor Red
}
}
}
function Set-FeaturesFromFile {
<#
.SYNOPSIS
Set multiple features from a text file
.DESCRIPTION
This function reads the comma separated features and values from a text file and executes each feature.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param ()
$RelativePath = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + '\'
$FeaturesFile = $RelativePath + $FeaturesFile
If ((Test-Path $FeaturesFile) -eq $true) {
$FeaturesFile = Get-Content $FeaturesFile
foreach ($Item in $FeaturesFile) {
$Item = $Item.split(',')
Set-WindowsFeature -Name $Item[0] -State $Item[1]
}
}
}
Clear-Host
If ($ListFeatures -eq $true) {
$WindowsFeatures = Get-WindowsFeaturesList
$WindowsFeatures
}
If ($FeaturesFile -ne '') {
Set-FeaturesFromFile
}
If ($Feature -ne '') {
Set-WindowsFeature -Name $Feature -State $Setting
}
#Set-WindowsFeature -Name 'RSATClient-Features' -State 'disable'
#Set-WindowsFeature -Name 'RSATClient-ServerManager' -State 'disable'