-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnable-Service.ps1
36 lines (33 loc) · 1.38 KB
/
Enable-Service.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
function Enable-Service {
<#
.SYNOPSIS
Function to start and and set to auto service
by Steven Wight
.DESCRIPTION
Enable-Service -Hostnames <Computername>, SVC <Service Name> -Domain <Domain> Default = POSHYT
.EXAMPLE
Enable-Service Computer0454S Spooler, Get-content c:\temp\hostnames.csv (or.txt) | Enable-Service Spooler
.NOTES
run against one machine = Enable-Service Computer0454S Spooler or a list = Get-content c:\temp\hostnames.csv (or.txt) | Enable-Service Spooler
#>
[cmdletbinding()]
param(
[Parameter(mandatory=$true , ValueFromPipeline=$true)]
[string[]] $Hostnames,
[Parameter()] [String] [ValidateNotNullOrEmpty()] $SVC,
[Parameter()] [String] [ValidateNotNullOrEmpty()] $Domain = "POSHYT"
)
process{
#check hostname (or if list is piped)
foreach($hostname in $hostnames){
$computer = Get-ADComputer -Identity $Hostname -server $Domain
#Set to Auto and start service
invoke-command -computerName $computer.dnshostname -scriptblock {
Set-Service -Name $using:SVC -StartupType Automatic
Start-Service -Name $using:SVC
Start-Sleep 5
Get-Service -Name $using:SVC
}# end of scriptblock
} #end of foreach
} #end of process
} #end of function