PowerShell's 'If' statement to get OU of users in Active directory

hello,

i use this gui script to change user password for my helpdesk.

i need to be able to send the new password in the local printer server for the user so it can print the new password with his own badge :).
i need to add a condition before line 23 with if condition like this:
i have 15 different OU so i think 15 different condition

get the OU of user in this form: OU=USA,OU=USERS,DC=contoso,DC=com
if $OU= "OU=USA,OU=USERS,DC=contoso,DC=com" ==> $printer="serverUSA.contoso.com"  and do this command:
PsExec.exe -u contoso\$SSAUserName -p $SSAPassword1 cmd.exe /c "PRINT /d:\\$printer\printerqueue D:\script\password.txt"

get the OU of user in this form: OU=CANADA,OU=USERS,DC=contoso,DC=com
if $OU= "OU=CANADA,OU=USERS,DC=contoso,DC=com" ==> $printer="servercanada.contoso.com"  and do this command:
PsExec.exe -u contoso\$SSAUserName -p $SSAPassword1 cmd.exe /c "PRINT /d:\\$printer\printerqueue D:\script\password.txt"
......
.....

# Change password.
	$ButtonMainChangePasswordChangePassword_Click =
	{
		if ($SSAActiveDirectoryModuleLoaded -eq $True)
		{
			Get-SSAUserName
			if ($SSAUserName)
			{
				Add-SSAOutput -OutputText "Changing password of user '$SSAUserName'."
				try
				{
					$QueryADUser = Get-ADUser -Identity $SSAUserName -Properties * @SSAApplicationUser -Server $SSAActiveDirectoryDomain -ErrorAction Stop
					Get-SSAPassword1
					Get-SSAPassword2
					if ($SSAPassword1 -and $SSAPassword2)
					{
						if ($SSAPassword1 -eq $SSAPassword2)
						{
							Set-ADAccountPassword -Identity $SSAUserName -NewPassword (ConvertTo-SecureString -String $SSAPassword1 -AsPlainText –Force) –Reset @SSAApplicationUser -Server $SSAActiveDirectoryDomain -ErrorAction Stop -PassThru
							Add-SSAOutput -OutputText "Password for user '$SSAUserName' has been updated"
                                                        $SSAPassword1 | out-file -Encoding Ascii -filepath D:\script\password.txt
														
                                                        PsExec.exe -u contoso\$SSAUserName -p $SSAPassword1 cmd.exe /c "PRINT /d:\\printerserver\printerqueue D:\script\password.txt"
                                                        remove-item -path D:\script\*.txt
                                                         Set-ADUser -Identity $SSAUserName -ChangePasswordAtLogon $True @SSAApplicationUser -Server $SSAActiveDirectoryDomain -ErrorAction Stop
							Add-SSAOutput -OutputText "Option 'Change password at next logon' has been enabled for user '$SSAUserName'."

Open in new window


thank you for help
cawasakiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
There's no need for extensive branching.
Define a hash table with the OU names as keys and their respective print servers as values:
$PrintServer = @{
	'USA' = 'serverUSA.contoso.com'
	'CANADA' = 'serverCANADA.contoso.com'
}

Open in new window

Then, when it comes to printing, get the OU from the user's DistinguishedName, and retrieve the printer name from the hash table:
$SSAUserOU = $QueryADUser.DistinguishedName -replace '\ACN=(?:.+?),OU=(.+?),(OU|DC)=.*', '$1'
$Printer = $PrintServer[$SSAUserOU]
PsExec.exe -u contoso\$SSAUserName -p $SSAPassword1 cmd.exe /c "PRINT /d:\\$Printer\printerqueue D:\script\password.txt"

Open in new window

This assumes that the printerqueue is the same on all servers. If it isn't, just define the complete UNC in the hash table:
$PrintServer = @{
	'USA' = '\\serverUSA.contoso.com\printerqueue1'
	'CANADA' = '\\serverCANADA.contoso.com\printerqueue1'
}

Open in new window

$SSAUserOU = $QueryADUser.DistinguishedName -replace '\ACN=(?:.+?),OU=(.+?),(OU|DC)=.*', '$1'
$PrinterQueue = $PrintServer[$SSAUserOU]
PsExec.exe -u contoso\$SSAUserName -p $SSAPassword1 cmd.exe /c "PRINT /d:$PrinterQueue D:\script\password.txt"

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cawasakiAuthor Commented:
hello oBdA

thank you a lot its work :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.