Steven Hoong
asked on
Help a Noob with Powershell Script!
So I'm trying to write a script to create users in AD more efficiently. Naming scheme is first initial + last name, however, if that's not available, I want it to take the first two letter of the first name + last name. This is what I have so far:
When I try to change the if state "If ($username -eq kbryant01)" it won't take the first two letters.
First name: Kobe
Last name: Bryant
is what i'm using to test
param(
[Parameter(Mandatory = $true)]
[string]$FirstName,
[Parameter(Mandatory = $true)]
[string]$LastName,
[Parameter(Mandatory = $true)]
[string]$Title,
[Parameter(Mandatory = $true)]
[string]$Location,
[Parameter(Mandatory = $true)]
[string]$Department,
[Parameter(Mandatory = $true)]
[string]$TicketNumber
)
$DomainDn = (Get-AdDomain).DistinguishedName
$Username = $FirstName.substring(0,1) + $LastName
$validateusername = Get-AdUser -LDAPFilter "(sAMAccountName = $Username)"
If ($validateusername -eq $Null) {"$Username = $FirstName.substring(0,1) + $Lastname"}
Else {$Username = $FirstName.substring(0,2) + $LastName}
$Username = $Username.ToLower()
If ($Username.ToLower() -eq "kbryant")
{Write-Host "Username:"$Username}
Else
{Write-Host "Username:"$validateusername}
When I try to change the if state "If ($username -eq kbryant01)" it won't take the first two letters.
First name: Kobe
Last name: Bryant
is what i'm using to test
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use this: "Username $variable" without issues.
the only issue is when the object has a property that you want to be print on the screen, for example, $var has a property called "name", so to get it we use $var.name
but when you want to print it on the screen you use it like this:
Write-host -foreground Cyan "Name $($var.Name)"
So basically you use $( var dot property )
Open in new window
I'm doing PowerShell coding on Wednesday and Fridays live on twitch (https://twitch.tv/j0rt)