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?
 
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
 
cawasakiAuthor Commented:
hello oBdA

thank you a lot its work :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.