/
15_2.ps1
99 lines (98 loc) · 4.25 KB
/
15_2.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
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
function Set-TMServiceLogon {
<#
.SYNOPSIS
Sets service login name and password.
.DESCRIPTION
This command uses either CIM (default) or WMI to
set the service password, and optionally the logon
user name, for a service, which can be running on
one or more remote machines. You must run this command
as a user who has permission to peform this task,
remotely, on the computers involved.
.PARAMETER ServiceName
The name of the service. Query the Win32_Service class
to verify that you know the correct name.
.PARAMETER ComputerName
One or more computer names. Using IP addresses will
fail with CIM; they will work with WMI. CIM is always
attempted first.
.PARAMETER NewPassword
A plain-text string of the new password.
.PARAMETER NewUser
Optional; the new logon user name, in DOMAIN\USER
format.
.PARAMETER ErrorLogFilePath
If provided, this is a path and filename of a text
file where failed computer names will be logged.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,
ValueFromPipelineByPropertyName=$True)]
[string]$ServiceName,
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[string[]]$ComputerName,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[string]$NewPassword,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[string]$NewUser,
[string]$ErrorLogFilePath
)
BEGIN{}
PROCESS{
ForEach ($computer in $ComputerName) {
Do {
Write-Verbose "Connect to $computer on WS-MAN"
$protocol = "Wsman"
Try {
$option = New-CimSessionOption -Protocol $protocol
$session = New-CimSession -SessionOption $option `
-ComputerName $Computer `
-ErrorAction Stop
If ($PSBoundParameters.ContainsKey('NewUser')) {
$args = @{'StartName'=$NewUser
'StartPassword'=$NewPassword}
} Else {
$args = @{'StartPassword'=$NewPassword}
Write-Warning "Not setting a new user name"
}
Write-Verbose "Setting $servicename on $computer"
$params = @{'CimSession'=$session
'MethodName'='Change'
'Query'="SELECT * FROM Win32_Service " +
"WHERE Name = '$ServiceName'"
'Arguments'=$args}
$ret = Invoke-CimMethod @params
switch ($ret.ReturnValue) {
0 { $status = "Success" }
22 { $status = "Invalid Account" }
Default { $status = "Failed: $($ret.ReturnValue)" }
}
$props = @{'ComputerName'=$computer
'Status'=$status}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
Write-Verbose "Closing connection to $computer"
$session | Remove-CimSession
} Catch {
# change protocol - if we've tried both
# and logging was specified, log the computer
Switch ($protocol) {
'Wsman' { $protocol = 'Dcom' }
'Dcom' {
$protocol = 'Stop'
if ($PSBoundParameters.ContainsKey('ErrorLogFilePath')) {
Write-Warning "$computer failed; logged to
➥ $ErrorLogFilePath"
$computer | Out-File $ErrorLogFilePath -Append
} # if logging
}
} #switch
} # try/catch
} Until ($protocol -eq 'Stop')
} #foreach
} #PROCESS
END{}
} #function