Help selecting a field

Hi
I have a dropdown box in which I have put some user types:

#Creae staff, contractor fields
$InputBox8 = New-Object System.Windows.Forms.ComboBox
$InputBox8.Location = New-Object System.Drawing.Size(100,280)
$InputBox8.Size = New-Object System.Drawing.Size(230,80)
$Form.Controls.Add($InputBox8)
$Label9 = New-Object System.Windows.Forms.Label
$Label9.Text = “Type :”
$Label9.AutoSize = $True
$Label9.Location = New-Object System.Drawing.Size(15,280)
$Form.Controls.Add($Label9)

$stafftype = @(“Associate”,“Admin/Secondary”,“Bots”,“Business Partner”,“Contractor”,“Generic/Shared”,“Service”)
$stafftype | ForEach-Object { [void] $InputBox8.Items.Add($_)}

This works fine but I want to translate sae “Associate” to just A … and “Admin” to Just B for when I import into AD

What command can I put that changes $stafftype into say Associate=A, Admin/Secretary=B ?

Any help woulld be apprecited

perky
Welcome to the forums.

When you post code or error messages or sample data or console output you should format it as code using the code posting tag on the formatting bar of the post editor “</>”.
Thanks in advance.

You may consider a hashtable:

$StaffTypeHashtable = @{
    'Associate'        = 'A'
    'Admin/Secondary'  = 'B'
    'Bots'             = 'C'
    'Business Partner' = 'D'
    'Contractor'       = 'E'
    'Generic/Shared'   = 'F'
    'Service'          = 'G'
}

And you could access it with the “dot notation

$StaffTypeHashtable.'Business Partner'

Hi Thanks for the message

It is not giving an error as such, just doesnt do what I want it to do which is annoying :frowning:

I have a section of the script which I am trying to enter

set-aduser -Identity $user.samaccountname -add @{extensionattribute14=$stafftype} -Server domaincontroller

So it should be looking into what I put for the and instead of putting “Associate” which the selects if should put A … and for Admin/Secretary put a B etc…

Hope that makes sense

Again: Please format your code as code.

If you created the hashtable like I suggested and your variable $stafftype contains the string Associate from your choice you use the dot notation:

$StaffTypeHashtable.$stafftype

Regardless of that - why don’t you simply put the full name of the stafftype into the AD attribute? Putting there only codes makes it harder to get to the full names of the stafftype when you read the AD for reports or something like this.



<# Import required assemblys #>

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

<# Load Brand Infomation From CSV File #>
$PracticeInfoPath = $PSScriptRoot +  "\brandinfo.csv"
$PracticeInfo = Import-csv $PracticeInfoPath

<# Set Up GUI form #>
#Create Form  
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$Form.Text = "Account Tool"   
$Form.Size = New-Object System.Drawing.Size(420,420)  
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
 
#Title Label
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = New-Object System.Drawing.Font("Calibri",12,[System.Drawing.FontStyle]::Bold)
$Label1.Text = "User Account Creator"
$Label1.AutoSize = $True
$Label1.Location = New-Object System.Drawing.Size(10,0) 
$Form.Controls.Add($Label1)

#Create Instructions Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Font = New-Object System.Drawing.Font("Calibri",10,[System.Drawing.FontStyle]::Bold)
$Label2.Text = "Fill in the details below and then click Create."
$Label2.AutoSize = $True
$Label2.Location = New-Object System.Drawing.Size(15,40) 
$Form.Controls.Add($Label2)
 
#Create First Name edit and label
$InputBox1 = New-Object System.Windows.Forms.TextBox 
$InputBox1.Location = New-Object System.Drawing.Size(100,70) 
$InputBox1.Size = New-Object System.Drawing.Size(230,80) 
$Form.Controls.Add($InputBox1)
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Text = "First Name:"
$Label3.AutoSize = $True
$Label3.Location = New-Object System.Drawing.Size(15,73) 
$Form.Controls.Add($Label3)

#Create Surname edit and label
$InputBox2 = New-Object System.Windows.Forms.TextBox 
$InputBox2.Location = New-Object System.Drawing.Size(100,100) 
$InputBox2.Size = New-Object System.Drawing.Size(230,100) 
$Form.Controls.Add($InputBox2)
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Text = "Surname :"
$Label4.AutoSize = $True
$Label4.Location = New-Object System.Drawing.Size(15,103) 
$Form.Controls.Add($Label4)

#Create Job title edit and label
$InputBox3 = New-Object System.Windows.Forms.TextBox 
$InputBox3.Location = New-Object System.Drawing.Size(100,130) 
$InputBox3.Size = New-Object System.Drawing.Size(230,80) 
$Form.Controls.Add($InputBox3)
$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = "Job Title:"
$Label5.AutoSize = $True
$Label5.Location = New-Object System.Drawing.Size(15,133) 
$Form.Controls.Add($Label5)

#Create Qualifications edit and label
$InputBox4 = New-Object System.Windows.Forms.TextBox 
$InputBox4.Location = New-Object System.Drawing.Size(100,160) 
$InputBox4.Size = New-Object System.Drawing.Size(230,80) 
$Form.Controls.Add($InputBox4)
$Label7 = New-Object System.Windows.Forms.Label
$Label7.Text = "Qualifications:"
$Label7.AutoSize = $True
$Label7.Location = New-Object System.Drawing.Size(15,163) 
$Form.Controls.Add($Label7)

#Create practice combobox and label
$InputBox5 = New-Object System.Windows.Forms.ComboBox
$InputBox5.Location = New-Object System.Drawing.Size(100,190) 
$InputBox5.Size = New-Object System.Drawing.Size(230,80) 
$Form.Controls.Add($InputBox5)
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = "Practice :"
$Label6.AutoSize = $True
$Label6.Location = New-Object System.Drawing.Size(15,193) 
$Form.Controls.Add($Label6)

#Add combo list
$PracticeInfo | ForEach-Object { [void] $InputBox5.Items.Add($_.PracticeName)}

#Create manager combobox and label
$InputBox6 = New-Object System.Windows.Forms.ComboBox
$InputBox6.Location = New-Object System.Drawing.Size(100,220) 
$InputBox6.Size = New-Object System.Drawing.Size(150,80) 
$Form.Controls.Add($InputBox6)
$Label7 = New-Object System.Windows.Forms.Label
$Label7.Text = "Manager :"
$Label7.AutoSize = $True
$Label7.Location = New-Object System.Drawing.Size(15,220) 
$Form.Controls.Add($Label7)

#Create Search Button
$Button3 = New-Object System.Windows.Forms.Button
$Button3.Location = New-Object System.Drawing.Size(260,220)
$Button3.Size = New-Object System.Drawing.Size(70,20)
$Button3.Text = "Search"

#Manager Search
$Button3.Add_Click(
{
    write-host "Searching"
    $man_input = $InputBox6.Text
    $mans = get-aduser -Filter "userprincipalname -like '$($man_input)*'"
    $inputbox6.items.Clear()
    $mans | ForEach-Object { [void] $InputBox6.Items.Add($_.Name) }
    write-host "Check dropdownbox for results"
}
)

$Button3.Cursor = [System.Windows.Forms.Cursors]::Hand
$Form.Controls.Add($Button3)


###############################################################
#Create license combobox and label
$InputBox7 = New-Object System.Windows.Forms.ComboBox
$InputBox7.Location = New-Object System.Drawing.Size(100,250) 
$InputBox7.Size = New-Object System.Drawing.Size(230,80) 
$Form.Controls.Add($InputBox7)
$Label8 = New-Object System.Windows.Forms.Label
$Label8.Text = "License :"
$Label8.AutoSize = $True
$Label8.Location = New-Object System.Drawing.Size(15,250) 
$Form.Controls.Add($Label8)

$licenses = @("F3","E1","E3")
$licenses | ForEach-Object { [void] $InputBox7.Items.Add($_)}

###############################################################

#Create staff, contractor fields as requested by Identity team May 2021

$InputBox8 = New-Object System.Windows.Forms.ComboBox
$InputBox8.Location = New-Object System.Drawing.Size(100,280) 
$InputBox8.Size = New-Object System.Drawing.Size(230,80) 
$Form.Controls.Add($InputBox8)
$Label9 = New-Object System.Windows.Forms.Label
$Label9.Text = "Type :"
$Label9.AutoSize = $True
$Label9.Location = New-Object System.Drawing.Size(15,280) 
$Form.Controls.Add($Label9)

$stafftype = @("Associate","Admin_Secondary","Bots","Business_Partner","Contractor","Generic_Shared","Service")
$stafftype | ForEach-Object { [void] $InputBox8.Items.Add($_)}


$StaffTypeHashtable = @{
    'Associate'        = 'A'
    'Admin/Secondary'  = 'B'
    'Bots'             = 'C'
    'Business Partner' = 'D'
    'Contractor'       = 'E'
    'Generic/Shared'   = 'F'
    'Service'          = 'G'
}

#switch ( $stafftype )
#{
#    Associate { $stafftype = "A"    }
#    Admin_Secondary { $stafftype = "ADM"    }
#    Bots { $stafftype = "BOT"   }
#    Business_Partner { $stafftype = "BP"   }
#    Contractor { $stafftype = "C" }
#    Generic_Shared { $stafftype = "G"  }
#    Service { $stafftype = "SVC"    }
#    
#}




 #    if ($stafftype -eq "Associate") 
 #    { $stafftype = "A"
 #  } elseif($stafftype -eq "Admin/Secondary") {
 #       $stafftype = "ADM"
 #   } elseif($stafftype -eq "Bots") {
 #       $stafftype = "BOT"
 #   } elseif($stafftype -eq "Contractor") {
 #       $stafftype = "C"
 #   } elseif($stafftype -eq "Generic/Shared") {
 #       $stafftype = "G"
 #   } elseif($stafftype -eq "Service") {
 #       $stafftype = "SVC"
 #   }
###############################################################


#Create Create Button
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(15,310)
$Button1.Size = New-Object System.Drawing.Size(150,60)
$Button1.Text = "Create"
$Button1.Add_Click(

    {
    $firstname = $InputBox1.Text
    $surname = $InputBox2.Text
    $title = $InputBox3.Text
    $quals = $InputBox4.Text
    $site = $InputBox5.SelectedItem
    $manager = get-aduser -filter "name -eq '$($inputbox6.text)'"
    $license = $InputBox7.Text
    $stafftype = $InputBox8.Text

    create_user $firstname $surname $title $quals $site $manager $license $stafftype
    
    }
    )
$Button1.Cursor = [System.Windows.Forms.Cursors]::Hand
$Form.Controls.Add($Button1)
 
#Create Exit Button
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(180,310)
$Button2.Size = New-Object System.Drawing.Size(150,60)
$Button2.Text = "Exit"
$Button2.Add_Click(
	{
		<# User clicked exit, destroy form #>
		$Form.Dispose()
	}) 
$Button2.Cursor = [System.Windows.Forms.Cursors]::Hand
$Form.Controls.Add($Button2)

function Create_User ($fn,$sn,$title,$quals,$site,$manager,$license) {
    
    #Create Variable
    $details = $PracticeInfo | where {$_.practicename -eq $site}
    $alias = $fn + "." + $sn
    $name = $fn + " " + $sn
    $brandname = $fn + " " + $sn +" " + $details.brand
    $email = $alias + "@" + $details.emaildomain
    $OU = $details.OU
  

    #Connect to On-Prem Exchange Shell
    $UserCredential = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://lexch-1.linn.co.uk/PowerShell/ -Authentication Kerberos -Credential $UserCredential
    Import-PSSession $Session -DisableNameChecking

    #Create random password
    $pw = (create_password -length 9 | Tee-Object -variable teeTime ) + "1"

    try{
    #Create new remote mailbox
    write-host "Creating Remote Mailbox"

    New-RemoteMailbox -Alias $alias -DisplayName $brandname -DomainController dc -FirstName $fn -LastName $sn -Name $name -OnPremisesOrganizationalUnit $OU -Password (ConvertTo-SecureString -AsPlainText $pw -Force) -PrimarySmtpAddress $email -UserPrincipalName $email
    }
    catch {
        Show_Error
    }

    
    #Set Extra AD Attributes
    write-host "Setting extra AD attributes and groups"

    
    $user = get-aduser -filter "userprincipalname -eq '$($email)'" -Server dc    
    set-aduser -Identity $user.samaccountname -StreetAddress $details.AddrStreet -City $details.AddrCity -State $details.AddrStateProvince -PostalCode $details.AddrPostcode -Company $details.AddrCompany -title $title -OfficePhone $details.AddrPhone -Server dc
    if ($quals) {
        set-aduser -Identity $user.samaccountname -Office $quals
    }
    set-aduser -Identity $user.samaccountname -Replace @{c="GB";co="United Kingdom";countrycode=826} -Add @{wWWHomePage=$details.PracticeWebpage} -Server dc
    set-aduser -Identity $user.samaccountname -add @{extensionattribute1=$details.CustomAttribute1} -Server dc
    set-aduser -Identity $user.samaccountname -add @{extensionattribute14=$stafftype}  -Server dc
    set-aduser -Identity $user.samaccountname -add @{extensionattribute11=$details.CustomAttribute11} -Server dc
    set-aduser -Identity $user.samaccountname -add @{extensionattribute12=$details.CustomAttribute12} -Server dc
    Set-ADUser -Identity $user.samaccountname -Manager $manager -Server dc
    # Set-Aduser -Identity $user.samaccountname -add @{e

    #Assign Groups
    Add-ADGroupMember -Identity $details.ADGroup -Members $user.samaccountname -Server linnaeus-dc
    Add-ADGroupMember -Identity $details.MFAGroup -Members $user.samaccountname -Server dc
    
    #Hybrid Sync
    write-host "Syncing to 365 - wait time 30 seconds"

    Invoke-Command -ComputerName linnaeus-hybrid.linnaeusgroup.co.uk -ScriptBlock {Start-ADSyncSyncCycle -PolicyType Delta}
    Start-Sleep -Seconds 30

    try{
    write-host "Setting License"
    
    set_license $email $license
    }
    catch {
        Show_Error
    }

    [System.Windows.MessageBox]::Show("User created E-mail Address: $($email) Password: $($pw) Press Ctrl+C to copy this message to clipboard before closing",'Create','OK')
    $Form.Dispose()

    }


Function create_password {
	
	[CmdletBinding()]
	Param (
        [int] $length = 8
	)

	Begin{
	}

	Process{
        Write-Output ( -join ((0x30..0x39) + ( 0x41..0x5A) + ( 0x61..0x7A) | Get-Random -Count $length  | % {[char]$_}) )
	}	
}





 
function set_license($upn, $license) {
    
    if ($license -eq "F3") {
        $planName = "DESKLESSPACK"
    } elseif($license -eq "E1") {
        $planName = "STANDARDPACK"
    } elseif($license -eq "E3") {
        $planName = "ENTERPRISEPACK"
    }




    Connect-AzureAD
    Get-AzureADUser -objectid $upn | set-azureaduser -UsageLocation GB   

    $License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
    $License.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value $planName -EQ).SkuID
    $LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
    $LicensesToAssign.AddLicenses = $License
    Set-AzureADUserLicense -ObjectId $upn -AssignedLicenses $LicensesToAssign

    $License2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
    $License2.SkuId = (Get-AzureADSubscribedSku | Where-Object -Property SkuPartNumber -Value "EMSPREMIUM" -EQ).SkuID
    $LicensesToAssign2 = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
    $LicensesToAssign2.AddLicenses = $License2
    Set-AzureADUserLicense -ObjectId $upn -AssignedLicenses $LicensesToAssign2
}

function Show_Error() {
    [System.Windows.MessageBox]::Show("An Error Occurred, please run in ISE for further diagnostics", "OK")
    $form.Dispose()
    exit
}

<# Diplay the GUI form to the user if all working #>
 
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

Sorry, posted code above
I am new to this, I am filling in for a colleague who is off

So for Input box 8 … it drops down the employee type in a way the person entering it can understand but when it puts into AD under the extensionattribute14 it needs to convert say from Associate and just put A as that is what they need.
The nice naming “associate” is just so they know what they are selecting from the dropbox … as if it just said A they would not know what it means.

I thought this addition would be a 5min job !!

That’s almost 400 lines of code. I am not going to review or modify that for you.

So you may wait for the colleague to come back or you may learn enough to be able to extend the script by yourself. :wink:

I got this. I just don’t understand why someone would like to have for example the letter “E” instead of the “describing” and “complete” word “Contractor”!? Technically It does not make any sense. :wink:

@Olaf’s given the correct answer already. You just need to use the selection from the text box to get the entry from the hashtable so line 284

set-aduser -Identity $user.samaccountname -add @{extensionattribute14=$stafftype}  -Server dc

Would become:

set-aduser -Identity $user.samaccountname -add @{extensionattribute14=$StaffTypeHashtable.$stafftype}  -Server dc
1 Like

Thank you for this - I have put in and it works.
Cant believe I was looking at this for hours and something simple - but that’s the great thing of these forums, sometimes takes a different set of eyes and they spot instantly !!!
Thanks again … much appreciated :+1:

Now you should mark this answer as the solution and maybe like the answer if you’re satisfied!! This will help others looking for the same or a similar issue. That’s how a forum works.

1 Like