Skip to content

Commit dee1fc6

Browse files
author
AchimASR
committed
Add script New-ADUser.ps1 #1 @frankkresse
1 parent d597dcc commit dee1fc6

File tree

1 file changed

+238
-0
lines changed

1 file changed

+238
-0
lines changed

ActiveDirectory/User/New-ADUser.ps1

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
#Requires -Version 4.0
2+
#Requires -Modules ActiveDirectory
3+
4+
<#
5+
.SYNOPSIS
6+
Creates a user in the OU path
7+
8+
.DESCRIPTION
9+
10+
.NOTES
11+
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
12+
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
13+
The terms of use for ScriptRunner do not apply to this script. In particular, AppSphere AG assumes no liability for the function,
14+
the use and the consequences of the use of this freely available script.
15+
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of AppSphere AG.
16+
© AppSphere AG
17+
18+
.COMPONENT
19+
Requires Module ActiveDirectory
20+
21+
.LINK
22+
https://github.com/scriptrunner/ActionPacks/tree/master/ActiveDirectory/Users
23+
24+
.Parameter OUPath
25+
Specifies the AD path
26+
27+
.Parameter GivenName
28+
Specifies the user's given name
29+
30+
.Parameter Surname
31+
Specifies the user's last name or surname
32+
33+
.Parameter Password
34+
Specifies a new password value for an account
35+
36+
.Parameter DomainAccount
37+
Active Directory Credential for remote execution without CredSSP
38+
39+
.Parameter SAMAccountName
40+
Specifies the Security Account Manager (SAM) account name of the user
41+
42+
.Parameter UserPrincipalname
43+
Specifies the user principal name (UPN) in the format <user>@<DNS-domain-name>
44+
45+
.Parameter UserName
46+
Specifies the name of the new user
47+
48+
.Parameter DisplayName
49+
Specifies the display name of the user
50+
51+
.Parameter Description
52+
Specifies a description of the user
53+
54+
.Parameter EmailAddress
55+
Specifies the user's e-mail address
56+
57+
.Parameter ChangePasswordAtLogon
58+
Specifies whether a password must be changed during the next logon attempt
59+
60+
.Parameter CannotChangePassword
61+
Specifies whether the account password can be changed
62+
63+
.Parameter PasswordNeverExpires
64+
Specifies whether the password of an account can expire
65+
66+
.Parameter Department
67+
Specifies the user's department
68+
69+
.Parameter Company
70+
Specifies the user's company
71+
72+
.Parameter PostalCode
73+
Specifies the user's postal code or zip code
74+
75+
.Parameter City
76+
Specifies the user's town or city
77+
78+
.Parameter Street
79+
Specifies the user's street address
80+
81+
.Parameter DomainName
82+
Name of Active Directory Domain
83+
84+
.Parameter AuthType
85+
Specifies the authentication method to use
86+
#>
87+
88+
param(
89+
[Parameter(Mandatory = $true,ParameterSetName = "Local or Remote DC")]
90+
[Parameter(Mandatory = $true,ParameterSetName = "Remote Jumphost")]
91+
[string]$OUPath,
92+
[Parameter(Mandatory = $true,ParameterSetName = "Local or Remote DC")]
93+
[Parameter(Mandatory = $true,ParameterSetName = "Remote Jumphost")]
94+
[string]$GivenName,
95+
[Parameter(Mandatory = $true,ParameterSetName = "Local or Remote DC")]
96+
[Parameter(Mandatory = $true,ParameterSetName = "Remote Jumphost")]
97+
[string]$Surname,
98+
[Parameter(Mandatory = $true,ParameterSetName = "Local or Remote DC")]
99+
[Parameter(Mandatory = $true,ParameterSetName = "Remote Jumphost")]
100+
[string]$Password,
101+
[Parameter(Mandatory = $true,ParameterSetName = "Remote Jumphost")]
102+
[PSCredential]$DomainAccount,
103+
[Parameter(ParameterSetName = "Local or Remote DC")]
104+
[Parameter(ParameterSetName = "Remote Jumphost")]
105+
[string]$SAMAccountName,
106+
[Parameter(ParameterSetName = "Local or Remote DC")]
107+
[Parameter(ParameterSetName = "Remote Jumphost")]
108+
[string]$UserPrincipalname,
109+
[Parameter(ParameterSetName = "Local or Remote DC")]
110+
[Parameter(ParameterSetName = "Remote Jumphost")]
111+
[string]$Username,
112+
[Parameter(ParameterSetName = "Local or Remote DC")]
113+
[Parameter(ParameterSetName = "Remote Jumphost")]
114+
[string]$DisplayName,
115+
[Parameter(ParameterSetName = "Local or Remote DC")]
116+
[Parameter(ParameterSetName = "Remote Jumphost")]
117+
[string]$Description,
118+
[Parameter(ParameterSetName = "Local or Remote DC")]
119+
[Parameter(ParameterSetName = "Remote Jumphost")]
120+
[string]$EmailAddress,
121+
[Parameter(ParameterSetName = "Local or Remote DC")]
122+
[Parameter(ParameterSetName = "Remote Jumphost")]
123+
[switch]$ChangePasswordAtLogon,
124+
[Parameter(ParameterSetName = "Local or Remote DC")]
125+
[Parameter(ParameterSetName = "Remote Jumphost")]
126+
[switch]$CannotChangePassword,
127+
[Parameter(ParameterSetName = "Local or Remote DC")]
128+
[Parameter(ParameterSetName = "Remote Jumphost")]
129+
[switch]$PasswordNeverExpires,
130+
[Parameter(ParameterSetName = "Local or Remote DC")]
131+
[Parameter(ParameterSetName = "Remote Jumphost")]
132+
[string]$Department,
133+
[Parameter(ParameterSetName = "Local or Remote DC")]
134+
[Parameter(ParameterSetName = "Remote Jumphost")]
135+
[string]$Company,
136+
[Parameter(ParameterSetName = "Local or Remote DC")]
137+
[Parameter(ParameterSetName = "Remote Jumphost")]
138+
[string]$PostalCode,
139+
[Parameter(ParameterSetName = "Local or Remote DC")]
140+
[Parameter(ParameterSetName = "Remote Jumphost")]
141+
[string]$City,
142+
[Parameter(ParameterSetName = "Local or Remote DC")]
143+
[Parameter(ParameterSetName = "Remote Jumphost")]
144+
[string]$Street,
145+
[Parameter(ParameterSetName = "Local or Remote DC")]
146+
[Parameter(ParameterSetName = "Remote Jumphost")]
147+
[string]$DomainName,
148+
[Parameter(ParameterSetName = "Local or Remote DC")]
149+
[Parameter(ParameterSetName = "Remote Jumphost")]
150+
[ValidateSet('Basic', 'Negotiate')]
151+
[string]$AuthType="Negotiate"
152+
)
153+
154+
Import-Module ActiveDirectory
155+
156+
try{
157+
$Script:Pwd = ConvertTo-SecureString $Password -AsPlainText -Force
158+
$Script:User
159+
$Script:Domain
160+
$Script:Properties =@('GivenName','Surname','SAMAccountName','UserPrincipalname','Name','DisplayName','Description','EmailAddress', 'CannotChangePassword','PasswordNeverExpires' `
161+
,'Department','Company','PostalCode','City','StreetAddress','DistinguishedName')
162+
163+
if([System.String]::IsNullOrWhiteSpace($SAMAccountName)){
164+
$SAMAccountName= $GivenName + '.' + $Surname
165+
}
166+
if([System.String]::IsNullOrWhiteSpace($Username)){
167+
$Username= $GivenName + '_' + $Surname
168+
}
169+
if([System.String]::IsNullOrWhiteSpace($DisplayName)){
170+
$DisplayName= $GivenName + ', ' + $Surname
171+
}
172+
if($UserPrincipalname.StartsWith('@')){
173+
$UserPrincipalname = $GivenName + '.' + $Surname + $UserPrincipalname
174+
}
175+
if($EmailAddress.StartsWith('@')){
176+
$EmailAddress = $GivenName + '.' + $Surname + $EmailAddress
177+
}
178+
if($PSCmdlet.ParameterSetName -eq "Remote Jumphost"){
179+
if([System.String]::IsNullOrWhiteSpace($DomainName)){
180+
$Script:Domain = Get-ADDomain -Current LocalComputer -AuthType $AuthType -Credential $DomainAccount -ErrorAction Stop
181+
}
182+
else{
183+
$Script:Domain = Get-ADDomain -Identity $DomainName -AuthType $AuthType -Credential $DomainAccount -ErrorAction Stop
184+
}
185+
}
186+
else{
187+
if([System.String]::IsNullOrWhiteSpace($DomainName)){
188+
$Script:Domain = Get-ADDomain -Current LocalComputer -AuthType $AuthType -ErrorAction Stop
189+
}
190+
else{
191+
$Script:Domain = Get-ADDomain -Identity $DomainName -AuthType $AuthType -ErrorAction Stop
192+
}
193+
}
194+
if($PSCmdlet.ParameterSetName -eq "Remote Jumphost"){
195+
$Script:User = New-ADUser -Credential $DomainAccount -Server $Script:Domain.PDCEmulator -Name $UserName -Path $OUPath -Confirm:$false -AuthType $AuthType `
196+
-Description $Description -DisplayName $DisplayName -SamAccountName $SAMAccountName -GivenName $GivenName -Surname $Surname `
197+
-AccountPassword $Pwd -EmailAddress $EmailAddress -Department $Department -Company $Company -City $City -PostalCode $PostalCode `
198+
-ChangePasswordAtLogon $ChangePasswordAtLogon.ToBool() -PasswordNeverExpires $PasswordNeverExpires.ToBool() -CannotChangePassword $CannotChangePassword.ToBool() `
199+
-UserPrincipalName $UserPrincipalname -StreetAddress $Street -Enable $true -PassThru -ErrorAction Stop
200+
}
201+
else {
202+
$Script:User = New-ADUser -Server $Script:Domain.PDCEmulator -Name $UserName -Path $OUPath -Confirm:$false -AuthType $AuthType `
203+
-Description $Description -DisplayName $DisplayName -SamAccountName $SAMAccountName -GivenName $GivenName -Surname $Surname `
204+
-AccountPassword $Pwd -EmailAddress $EmailAddress -Department $Department -Company $Company -City $City -PostalCode $PostalCode `
205+
-ChangePasswordAtLogon $ChangePasswordAtLogon.ToBool() -PasswordNeverExpires $PasswordNeverExpires.ToBool() -CannotChangePassword $CannotChangePassword.ToBool() `
206+
-UserPrincipalName $UserPrincipalname -StreetAddress $Street -Enable $true -PassThru -ErrorAction Stop
207+
}
208+
if($Script:User){
209+
Start-Sleep -Seconds 5 # wait
210+
if($PSCmdlet.ParameterSetName -eq "Remote Jumphost"){
211+
$Script:User = Get-ADUser -Identity $SAMAccountName -Properties $Script:Properties -Credential $DomainAccount -AuthType $AuthType -Server $Script:Domain.PDCEmulator
212+
}
213+
else{
214+
$Script:User = Get-ADUser -Identity $SAMAccountName -Properties $Script:Properties -AuthType $AuthType -Server $Script:Domain.PDCEmulator
215+
} $res=New-Object 'System.Collections.Generic.Dictionary[string,string]'
216+
$tmp=($Script:User.DistinguishedName -split ",",2)[1]
217+
$res.Add('Path:', $tmp)
218+
foreach($item in $Script:Properties){
219+
if(-not [System.String]::IsNullOrWhiteSpace($Script:User[$item])){
220+
$res.Add($item + ':', $Script:User[$item])
221+
}
222+
}
223+
$Out =@()
224+
$Out +="User $($GivenName) $($Surname) with follow properties created:"
225+
$Out +=$res | Format-Table -HideTableHeaders
226+
if($SRXEnv) {
227+
$SRXEnv.ResultMessage = $Out
228+
}
229+
else {
230+
Write-Output $Out
231+
}
232+
}
233+
}
234+
catch{
235+
throw
236+
}
237+
finally{
238+
}

0 commit comments

Comments
 (0)