We help IT Professionals succeed at work.

Help a Noob with Powershell Script!

Medium Priority
162 Views
Last Modified: 2019-01-25
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
Comment
Watch Question

Jose Gabriel Ortega CastroCEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
CERTIFIED EXPERT
Awarded 2018
Distinguished Expert 2018

Commented:
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)
Steven HoongSystems Administrator

Author

Commented:
Hi Jose,

Thanks for responding. When I run that, I get a blank return for Username. Screen-Shot-2019-01-25-at-9.38.52-AM.png
CEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
CERTIFIED EXPERT
Awarded 2018
Distinguished Expert 2018
Commented:
Ok bud, let me check about it :)
Done...
Try this one:

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){
    $Username = ($FirstName.substring(0,2) + $LastName).ToLower()
}


if ($Username -eq "kbryant"){
    Write-Host "Username: $Username"
}
else{
    Write-Host -ForegroundColor Red "Username: $validateusername"
    Write-host -ForegroundColor Green "New Username var $Username"
} 

Open in new window


Results

if user doens't exists:
Results of the script if user DONT EXISTS
If user exists:
Results of the script if user DO EXISTS

Explore More ContentExplore courses, solutions, and other research materials related to this topic.