Link to home
Start Free TrialLog in
Avatar of Steven Hoong
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:

 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} 

Open in new window


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
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

I think you have problems using the " "s I've found like 4 or 5 errors in those ones :)
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 )

 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 -filter {SamAccountName -eq $Username}
# -LDAPFilter "(sAMAccountName = $Username)"  this is wrong since to compare you don't use the =,< or >  you use -eq -lt or -gt respectively

if ($validateusername -eq $Null){
    $Username = "$FirstName.substring(0,1) + $Lastname".ToLower()
}
else{
    $Username = "$FirstName.substring(0,2) + $LastName".ToLower()
}

if ($Username -eq "kbryant"){
    Write-Host "Username: $Username"
}
else{
    Write-Host "Username: $validateusername"
} 

Open in new window


I'm doing PowerShell coding on Wednesday and Fridays live on twitch (https://twitch.tv/j0rt)
Avatar of Steven Hoong
Steven Hoong

ASKER

Hi Jose,

Thanks for responding. When I run that, I get a blank return for Username. User generated image
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial