-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start-RequiredServices.ps1
46 lines (38 loc) · 1.17 KB
/
Start-RequiredServices.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
function Start-Services()
{
param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $DisplayName,
[Parameter(Mandatory=$true, Position=1)]
[int] $Retries
)
$services_array = Get-Service -DisplayName $DisplayName
foreach ($service in $services_array)
{
$service_started = $false
if ($service.Status -eq 'Running')
{
Write-Host "$($service.DisplayName) already active!"
continue;
}
for ($x = 1; $x -le $Retries; $x++)
{
Write-Host "trying to start $($service.DisplayName)"
Start-Service -Name $service.Name
Start-Sleep -Seconds 1
$service.Refresh()
if ($service.Status -eq 'Running')
{
$service_started = $true;
Write-Host "$($service.DisplayName) successfully started!"
break
}
}
if ($service_started -eq $false)
{
"$($service.DisplayName) could not be started"
}
}
}
Start-Services -DisplayName "*Spool*" -Retries 5