1

Wonder if you can offer me assistance. I am creating a script for my colleagues to add a brand new user to AD via Powershell. However, I am beginning to encounter some errors at the end of the script.

# Adding the AD PS Module
Import-Module ActiveDirectory -ErrorAction SilentlyContinue

# set default password
$defpassword = (ConvertTo-SecureString "Welcome123" -AsPlainText -force)

# Get Domain DNS suffix
$dnsroot = '@' + (Get-ADDomain).dnsroot

echo "This tool is to be used for creating User Accounts for the RBFT Domain     under Ultima Business Solutions only. If this applies, please hit any key to continue."
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer() 
Write-Host " "
Write-Host " "


# Acquiring unique field data
$GivenName = Read-Host -Prompt "What Is The New User's First Name?"
Write-Host " "

$Initial = Read-Host -Prompt "What Is The New User's First Initial?"
Write-Host " "

$Surname = Read-Host -Prompt "What Is The New User's Last Name?"
Write-Host " "


$DisplayName = $Surname + " " + $GivenName

$Mail = $GivenName + "." + $Surname + "@" + "BLOCKEDEMAIL"

$MailAlias = $GivenName + "." + $Surname + "@" + $DNSRoot2

$Manager = Read-Host -Prompt "Who Is The New User's Manager?"
Write-Host " "

$SAMAccountName = $Surname.Substring(0,7)+$Initial.Substring(0,1)
$SAMAccountLower = $SAMAccountName.ToLower()
$UserPrincipalName = $Mail


start-sleep -s 5

# Create The User

Get-ChildItem
New-ADUser -path "OU=Users,OU=RBFT,DC=rbbh-tr,DC=nhs,DC=uk" -SamAccountName     $SamAccountLower -Name $DisplayName -DisplayName $DisplayName -GivenName     $GivenName -Surname $Surname -EmailAddress $Mail -UserPrincipalName $Mail -Title     $title -Enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires  $false -AccountPassword $defpassword -PassThru

However, this produces the following error

Exception calling "Substring" with "2" argument(s): "Index and length must     refer to a location within the string.
Parameter name: length"
At C:\Users\timmsj\Desktop\AD_User.ps1:42 char:1
+ $SAMAccountName = $Surname.Substring(0,7)+$Initial.Substring(0,1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException

You cannot call a method on a null-valued expression.
At C:\Users\timmsj\Desktop\AD_User.ps1:43 char:1
+ $SAMAccountLower = $SAMAccountName.ToLower()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Is anybody able to offer assistance?

2
  • You will get this error when $Surname (or $Initial) is too short. So you need to add some length checks to your script. Commented May 29, 2018 at 6:12
  • How do i go about doing that @OcasoProtal Commented May 29, 2018 at 7:15

1 Answer 1

1

You need to check if the length of $Surname is less than 7 and if $Initial conatins at least one character:

if ($Surname.length -lt 7) {
  $SAMAccountName = $Surname
} else {
  $SAMAccountName = $Surname.Substring(0,7)
}

if ($Initial.length -ge 1){
  $SAMAccountName = SAMAccountName+$Initial.Substring(0,1)
}

But beware: You also have to check if that generated samAccountName is not yet set in the domain!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.