-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoppedAutoStartServices.psm1
240 lines (181 loc) · 8.3 KB
/
StoppedAutoStartServices.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
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
function Get-StoppedAutoStartService
{
<#
.SYNOPSIS
Get the list of stopped services which are of type AutoStart and can also restart them at the same time.
.DESCRIPTION
The cmdlet gets the list of stopped services which are of type AutoStart.
It can also restart them at the same time if requested so.
.PARAMETER ComputerName
The computer of which you want to check the service list.
.PARAMETER AsJob
Start the check and the service restart as a job.
.PARAMETER StartService
Use this parameter if you want to restart all stopped services.
.PARAMETER IncludeInShortName
A regular expression pattern to compare to the list of service short names.
.PARAMETER IncludeInDisplayName
A regular expression pattern to compare to the list of service display names.
.PARAMETER ExcludeInShortName
A regular expression pattern to compare to the list of service short names.
.PARAMETER ExcludeInDisplayName
A regular expression pattern to compare to the list of service display names.
.PARAMETER Credential
Alternate credential with Administrator access to the target computer.
.EXAMPLE
The list of computers is piped to the cmdlet and some services are filtered out by displayname.
Get-Content -Path C:\Temp\ServerList.txt | Get-StoppedAutoStartService -ExcludeInDisplayName 'Remote registry','windows Update','software protection'
.EXAMPLE
All services including the word 'Remote' in the displayname are checked on the Comp1 computer and an alternate credential is used.
Get-StoppedAutoStartService -ComputerName Comp1 -IncludeInDisplayname 'Remote' -Credential MyDomain\JohnDo
.EXAMPLE
All automatic services which are stopped are restarted on the current computer.
Get-StoppedAutoStartService -StartService
.EXAMPLE
The list of computers is piped to the cmdlet and all computers are processed as jobs.
Get-Content -Path C:\Temp\ServerList.txt | Get-StoppedAutoStartService -AsJob
#>
[Alias('gsas')]
[CmdletBinding()]
[OutputType('System.Array' , ParameterSetName = '__AllParameterSets')]
[OutputType('System.Management.Automation.Job', ParameterSetName = 'AsJob')]
Param(
[parameter(
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 0
)]
[ValidateNotNullOrEmpty()]
[string[]]$ComputerName = '.',
[Parameter(ParameterSetName = 'AsJob')]
[switch]$AsJob,
[switch]$StartService,
[ValidateNotNullOrEmpty()]
[string[]]$IncludeInShortName,
[ValidateNotNullOrEmpty()]
[string[]]$IncludeInDisplayName,
[ValidateNotNullOrEmpty()]
[string[]]$ExcludeInShortName,
[ValidateNotNullOrEmpty()]
[string[]]$ExcludeInDisplayName,
[pscredential]$Credential
)
Begin
{
If ($PSBoundParameters['Debug'])
{$DebugPreference = 'Continue'}
$BasicFilter = "-Filter `"state='stopped' and startmode='auto'`""
$PropertyList = '-Property ProcessId,Name,StartMode,State,Status,Exitcode,DisplayName'
$ScriptBlockString = 'Get-CimInstance -ClassName Win32_Service {0} {1}' -f $BasicFilter, $PropertyList
$InclusionFilterScriptString = ''
$ExclusionFilterScriptString = ''
#region Inclusions
If ($PSBoundParameters['IncludeInShortName'])
{
$ShortNameInclusionString = ''
#Create a regular expression with a pipe between Inclusions
foreach ($Inclusion in $IncludeInShortName)
{
$ShortNameInclusionString = '{0}|{1}' -f $ShortNameInclusionString, $Inclusion
}
#Remove the pipe at the beginning
$ShortNameInclusionString = $ShortNameInclusionString -replace '^\|', ''
#Add the regular expression to the InclusionFilterScriptString
$InclusionFilterScriptString = "(`$_.Name -Match '{0}')" -f $ShortNameInclusionString
}
If ($PSBoundParameters['IncludeInDisplayName'])
{
$DisplayNameInclusionString = ''
#Create a regular expression with a pipe between Inclusions
foreach ($Inclusion in $IncludeInDisplayName)
{
$DisplayNameInclusionString = '{0}|{1}' -f $DisplayNameInclusionString, $Inclusion
}
#Remove the pipe at the beginning
$DisplayNameInclusionString = $DisplayNameInclusionString -replace '^\|', ''
#Add the regular expression to the InclusionFilterScriptString
If ($InclusionFilterScriptString)
{$InclusionFilterScriptString = "({0}) -or (`$_.DisplayName -Match '{1}')" -f $InclusionFilterScriptString, $DisplayNameInclusionString}
Else {$InclusionFilterScriptString = "`$_.DisplayName -Match '{0}'" -f $DisplayNameInclusionString}
}
#endregion
#region Exclusions
If ($PSBoundParameters['ExcludeInShortName'])
{
$ShortNameExclusionString = ''
#Create a regular expression with a pipe between exclusions
foreach ($Exclusion in $ExcludeInShortName)
{
$ShortNameExclusionString = '{0}|{1}' -f $ShortNameExclusionString, $Exclusion
}
#Remove the pipe at the beginning
$ShortNameExclusionString = $ShortNameExclusionString -replace '^\|', ''
#Add the regular expression to the ExclusionFilterScriptString
$ExclusionFilterScriptString = "(`$_.Name -NotMatch '{0}')" -f $ShortNameExclusionString
}
If ($PSBoundParameters['ExcludeInDisplayName'])
{
$DisplayNameExclusionString = ''
#Create a regular expression with a pipe between exclusions
foreach ($Exclusion in $ExcludeInDisplayName)
{
$DisplayNameExclusionString = '{0}|{1}' -f $DisplayNameExclusionString, $Exclusion
}
#Remove the pipe at the beginning
$DisplayNameExclusionString = $DisplayNameExclusionString -replace '^\|', ''
#Add the regular expression to the ExclusionFilterScriptString
If ($ExclusionFilterScriptString)
{$ExclusionFilterScriptString = "({0}) -and (`$_.DisplayName -NotMatch '{1}')" -f $ExclusionFilterScriptString, $DisplayNameExclusionString}
Else {$ExclusionFilterScriptString = "`$_.DisplayName -NotMatch '{0}'" -f $DisplayNameExclusionString}
}
#endregion
#region Combine Inclusion and Exclusion
$CombinedFilterScriptString = ''
If ($InclusionFilterScriptString -and $ExclusionFilterScriptString)
{
$CombinedFilterScriptString = "($InclusionFilterScriptString) -and ($ExclusionFilterScriptString)"
}
elseif ($InclusionFilterScriptString)
{
$CombinedFilterScriptString = $InclusionFilterScriptString
}
elseif ($ExclusionFilterScriptString)
{
$CombinedFilterScriptString = $ExclusionFilterScriptString
}
If ($CombinedFilterScriptString)
{
$CombinedFilterScriptString = "`{$CombinedFilterScriptString`}"
$ScriptBlockString = '{0} | Where-Object -FilterScript {1}' -f $ScriptBlockString, $CombinedFilterScriptString
}
#endregion
If ($PSBoundParameters['StartService'])
{
$ScriptBlockString = '{0} |Start-Service' -f $ScriptBlockString
If ($PSBoundParameters['Verbose'])
{$ScriptBlockString = '{0} -Verbose' -f $ScriptBlockString}
}
$Parameters = @{
ComputerName = ''
ScriptBlock = [scriptblock]::Create($ScriptBlockString)
}
$Parameters.ScriptBlock | Out-String | Write-Debug
If ($PSBoundParameters['AsJob'])
{
$Parameters.Add('AsJob', $true)
$Parameters.Add('JobName', 'GetStoppedAutoStartService')
}
If ($PSBoundParameters['Credential'])
{$Parameters.Add('Credential', $Credential)}
}
Process
{
foreach ($Computer in $ComputerName)
{
Write-Verbose -Message "Workging on $Computer..."
$Parameters.ComputerName = $Computer
$Parameters | Out-String | Write-Debug
Invoke-Command @Parameters
}
}
}