forked from 15below/Ensconce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userManagement.ps1
142 lines (120 loc) · 4.22 KB
/
userManagement.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
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
# ADS_USER_FLAG_ENUM Enumeration
# http://msdn.microsoft.com/en-us/library/aa772300(VS.85).aspx
$ADS_UF_SCRIPT = 1 # 0x1
$ADS_UF_ACCOUNTDISABLE = 2 # 0x2
$ADS_UF_HOMEDIR_REQUIRED = 8 # 0x8
$ADS_UF_LOCKOUT = 16 # 0x10
$ADS_UF_PASSWD_NOTREQD = 32 # 0x20
$ADS_UF_PASSWD_CANT_CHANGE = 64 # 0x40
$ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 128 # 0x80
$ADS_UF_TEMP_DUPLICATE_ACCOUNT = 256 # 0x100
$ADS_UF_NORMAL_ACCOUNT = 512 # 0x200
$ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 2048 # 0x800
$ADS_UF_WORKSTATION_TRUST_ACCOUNT = 4096 # 0x1000
$ADS_UF_SERVER_TRUST_ACCOUNT = 8192 # 0x2000
$ADS_UF_DONT_EXPIRE_PASSWD = 65536 # 0x10000
$ADS_UF_MNS_LOGON_ACCOUNT = 131072 # 0x20000
$ADS_UF_SMARTCARD_REQUIRED = 262144 # 0x40000
$ADS_UF_TRUSTED_FOR_DELEGATION = 524288 # 0x80000
$ADS_UF_NOT_DELEGATED = 1048576 # 0x100000
$ADS_UF_USE_DES_KEY_ONLY = 2097152 # 0x200000
$ADS_UF_DONT_REQUIRE_PREAUTH = 4194304 # 0x400000
$ADS_UF_PASSWORD_EXPIRED = 8388608 # 0x800000
$ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 16777216 # 0x1000000
Function AddUser([string]$name, [string]$password)
{
$computer = [ADSI]"WinNT://$env:computername,computer"
$newuser = $computer.Create("user", $name)
$newuser.SetPassword($password)
$newuser.SetInfo()
$newuser.UserFlags = $ADS_UF_DONT_EXPIRE_PASSWD
$newuser.CommitChanges()
}
Function AddUserToGroup([string]$name, [string]$group)
{
"AddUserToGroup: $name, $group" | Write-Host
$objComputer = [ADSI]("WinNT://$env:computername,computer")
$colUsers = $objComputer.psbase.children |
Where-Object {$_.psBase.schemaClassName -eq "User" -and $_.psBase.Name -eq $name}
if($colUsers -eq $null)
{
"Could not locate user: $user" | Write-Host
}
else
{
$searchgroup = ($objComputer.psbase.children |
Where {$_.psbase.schemaClassName -eq "group" -and $_.psbase.Name -eq $group} |
Select-Object -First 1)
if($searchgroup -eq $null)
{
"Could not locate group: $group" | Write-Host
}
else
{
$blnFound = $False
$members = @($searchgroup.psbase.Invoke("Members"))
ForEach ($Member In $Members)
{
$class = $member.GetType().InvokeMember("Class", 'GetProperty', $Null, $member, $Null)
$username = $member.GetType().InvokeMember("Name", 'GetProperty', $Null, $member, $Null)
if ($class -eq "User" -and $username -eq $name)
{
$blnFound = $True
"User already added to group: $name, $group" | Write-Host
}
}
if ($blnFound -eq $False) {
try {
net localgroup $group $name /add
}
catch [Exception] {
"Error caught" | Write-Host
}
}
}
}
}
Function CheckAndCreateServiceAccount([string]$name, [string]$password)
{
$objComputer = [ADSI]("WinNT://$env:computername,computer")
$colUsers = ($objComputer.psbase.children |
Where-Object {$_.psBase.schemaClassName -eq "User"} |
Select-Object -expand Name)
$blnFound = $colUsers -contains $name
if ($blnFound -eq $False) {
AddUser $name $password
AddUserToGroup $name "administrators"
}
$exe = "$deployToolsDir\Grant.exe"
$userName = "$env:computername\$name"
&$exe ADD SeServiceLogonRight $userName
}
Function CheckAndCreateUserAccount([string]$name, [string]$password)
{
$objComputer = [ADSI]("WinNT://$env:computername,computer")
$colUsers = ($objComputer.psbase.children |
Where-Object {$_.psBase.schemaClassName -eq "User"} |
Select-Object -expand Name)
$blnFound = $colUsers -contains $name
if ($blnFound -eq $False) {
AddUser $name $password
net localgroup Users $name /add
}
}
Function SetServiceAccount([string]$serviceName, [string]$account, [string]$password)
{
$svc = gwmi win32_service -filter ("Name=""$serviceName""")
if ($svc -eq $null)
{
"Could not locate service $serviceName" | Write-Host
}
else
{
CheckAndCreateServiceAccount $account $password
$inParams = $svc.psbase.getMethodParameters("Change")
$inParams["StartName"] = ".\$account"
$inParams["StartPassword"] = $password
$svc.invokeMethod("Change", $inParams, $null)
"Service $serviceName set to use account $account" | Write-Host
}
}