patron
asked on
Powershell script to create AD User
>Should contain only lower-case characters and numbers and the only special characters allowed are "_" and ".". So: ("[a-z0-9_.]")
> If there is no "." in the username, the script should ask for confirmation before creation of the user. The message should be:
"The username does not contain a dot, are you sure you want to continue?"
>Maximum amount of characters = 31
> Minimum amount of characters = 3
and how to create user in specific CN [Built in OU] with Manuel entry?
> If there is no "." in the username, the script should ask for confirmation before creation of the user. The message should be:
"The username does not contain a dot, are you sure you want to continue?"
>Maximum amount of characters = 31
> Minimum amount of characters = 3
and how to create user in specific CN [Built in OU] with Manuel entry?
Try this script
$Regex1 = "^[a-z0-9_.]{3,31}$"
$username = "dee_."
if($username -match $Regex1)
{
if($username -notmatch ".")
{
Write-host "Username doesnot contains a ., do you want to continue? (Default is No)" -ForegroundColor Yellow
$Readhost = Read-Host " ( y / n ) "
Switch ($ReadHost)
{
Y {<#your logic#>}
N {Write-Host "No, creating user $username failed"}
Default {Write-Host "Default, creating user $username failed" }
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Then it should not create user and ask to give name in valid manner like above
ASKER
user name should be abc_xyz or abc.xyz or if none from both it should ask before creating any user like admin [without.,/]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
currently i am using like..
$validUser ="([a-z]){3}[_]\d{4}"
If ($SamAccountName –notmatch $validUser) {
Write-Error "User name is not in valid format. Please enter user name in followin format `r`n ABC_1234"
pls help to update this according to need in question
$validUser ="([a-z]){3}[_]\d{4}"
If ($SamAccountName –notmatch $validUser) {
Write-Error "User name is not in valid format. Please enter user name in followin format `r`n ABC_1234"
pls help to update this according to need in question
You really should be able to do that yourself. What do you have issues with exactly? The pattern you use in your last comment and the one you showed in the origirnal question are too different to tell the desired rule.
ASKER
Thanks a lot for all your support on this
ASKER
Thanks
New-ADUser `
-Name "TestUser" `
-Path "OU=TestOU,DC=TestDomain,D
-SamAccountName "TestUser" `
-DisplayName "Test User" `
-AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
-ChangePasswordAtLogon $true `
-Enabled $true
Add-ADGroupMember "Domain Admins" "TestUser";