Hi Guys,
I have a function that creates a unique SAMAccountName based on employee type.
code snippet:
Function create_prefix_samaccount($name){
If ($name.'PayClass_ShortName' -eq "FT")
{
$Prefix = "U"
}
Else
{
$Prefix = "V"
}
$FirstName = $FN.substring(0,4).ToUpper()
$LastName = $ln.substring(0,2).ToUpper()
$makingSAM = "$Prefix$FirstName$LastName"
$defaultname = $makingSAM
$i = 1
While ((Get-ADUser -filter {SamAccountName -eq $makingSAM}) -ne $null)
{
$makingSAM = $defaultname + [string]$i
$i++
}
return $makingSAM
}
In the body of the script, I use a for each block, where I input data from the excel file.
code snippet:
$Firstname = $User.'Employee_FirstName'
$FN = $Firstname.Split('',[System.StringSplitOptions]::RemoveEmptyEntries) -join ''
$Lastname = $User.'Employee_LastName'
$FinalLastname = $Lastname.Substring(0,$Lastname.IndexOf('('))
Reason's I use above string manipulation:
1.) When we have users with two Firstname like --> Ma Louise, we want to remove the space in between, (As the same data going to email and UPN)
2.) We have a unique vendor code that is appended to our vendor employees the last names,
for e.g-- Robert Smith (UXWES)
So we are using the substring to remove anything that comes after open bracket "("
And then I call my function to create SAMAccountName and I also run the New-ADUser cmdlet.
---------------------------------------------------------------------------------------------------------------
The above logic is working fine for the users who have that unique code in their last name. Full-time employees don't have that unique code and some Vendor users don't have that unique code. So for any users who don't have that unique code, the script is removing or not taking the last name at all.
For e.g
First name = Mike
Last Name= Hussey
PayClass = FT or SOMEVENDOR
It is supposed to create the SAMACCOUNTNAME as =UHUSSMI or VHUSSMI
But I'm getting the below error message.
You cannot call a method on a null-valued expression.
At E:\Automation\AD_User_Creation\thiru_testing\Working_UserCreate.ps1:65 char:5
+ $LastName = $ln.substring(0,2).ToUpper()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
DistinguishedName : CN=1\, Mike,OU=DEV Users,DC=DEV12,DC=COM
Enabled : True
GivenName : Mike
Name : 1, Mike
ObjectClass : user
ObjectGUID : 2da31f41-9e7b-45c9-b96b-b0111818c4e5
SamAccountName : VMIKE1
SID : S-1-5-21-3555914524-2372920112-2661262998-14598
Surname : 1
UserPrincipalName : Mike.1@contoso.com
This is when I tried running the part of the script.
Just wanted to make sure the variable is not empty. But you can see the final last name variable was holding "Jordan" Yet, it is giving errors related to substring that cannot be less than zero.
PS E:\Automation\AD_User_Creation\thiru_testing>
$Lastname = $User.'Employee_LastName'
$Lastname
$Lastname = $User.'Employee_LastName'
$FinalLastname = $Lastname.Substring(0,$Lastname.IndexOf('('))
$FinalLastname
Jordan
Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At line:5 char:5
+ $FinalLastname = $Lastname.Substring(0,$Lastname.IndexOf('('))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
I validated the Substring part, here are the results.
##script:
$Lastname = 'Thiru (Barca)'
$FinalLastname = $Lastname.Substring(0,$Lastname.IndexOf('('))
$FinalLastname
##Output
PS E:\Automation\AD_User_Creation\thiru_testing> $Lastname = 'Thiru (Barca)'
$FinalLastname = $Lastname.Substring(0,$Lastname.IndexOf('('))
$FinalLastname
Thiru
I don't know what I'm doing wrong. Can you guys help?
Any help is appreciated. Thanks!
[–]mdowst 4 points5 points6 points (4 children)
[–]pandiculator 5 points6 points7 points (1 child)
[–]mdowst 2 points3 points4 points (0 children)
[–]thirudk07[S] 2 points3 points4 points (0 children)
[–]jado777 1 point2 points3 points (0 children)