-
Notifications
You must be signed in to change notification settings - Fork 12
/
Set-ServiceCreds.ps1
73 lines (61 loc) · 2.31 KB
/
Set-ServiceCreds.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
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
<#
.SYNOPSIS
This script will set the logon account for the specified service.
.DESCRIPTION
Using this script, an administrator can set the logon account for the specified service.
.PARAMETER ComputerName
This parameter is used to define the name of the Computer to run Set-ServiceCreds on.
.PARAMETER Account
The account you will be setting the service to use. EX: Domain\User
.PARAMETER Password
The password for the account.
.PARAMETER Servicename
The service to set the credentials for. EX: Spooler, etc.
.NOTES
Author: Alex Lutz
Email: alexinslc@gmail.com
# Command and Parameters:
.\Set-ServiceCreds.ps1 -ComputerName "YOURCOMPUTER" -Account "DOMAIN\USER" -Password "YOURPASSWORD" -Servicename "YOURSERVICE"
#>
function Set-ServiceCreds() {
param(
[string]$ComputerName = $env:ComputerName,
[Parameter(Mandatory=$true)][string]$Account,
[Parameter(Mandatory=$true)][string]$Password,
[Parameter(Mandatory=$true)][string]$ServiceName
)
try {
#
$scriptblock = {
param(
$ComputerName, $Account, $Password, $ServiceName
)
if ( Get-Service $ServiceName | Where Status -eq 'Running' )
{
Write-Warning "$ServiceName is currently running, stopping service."
$service="name='$ServiceName'"
$svc=gwmi win32_service -filter $service
Stop-Service $ServiceName
$svc.change($null,$null,$null,$null,$null,$null,$Account,$Password,$null,$null,$null)
Write-Warning "$ServiceName has been set to use $Account to logon."
Start-Service $ServiceName
Write-Warning "$ServiceName starting..."
if (Get-Service $ServiceName | Where Status -eq 'Running')
{
Write-Warning "$ServiceName started."
}
}
else {
$service="name='$ServiceName'"
$svc=gwmi win32_service -filter $service
$svc.change($null,$null,$null,$null,$null,$null,$Account,$Password,$null,$null,$null)
Write-Warning "$ServiceName has been set to use $Account to logon."
}
}
Invoke-Command -ComputerName $ComputerName -ScriptBlock $scriptblock -ArgumentList @($ComputerName, $Account, $Password, $ServiceName)
}
catch {
Write-Warning "Error Occured on set-servicecreds.ps1"
Write-Host "$_"
}
}