Solved

Repetitive Menu in Powershell

Posted on 2013-01-07
63
284 Views
Last Modified: 2013-01-22
Hello,

I am wondering if in powershell I can do the following:

GOAL: Change AD User information (sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber )

Scenario:
AD user information is read by a file (i.e 100+ users) and compared against AD users.

Stage1:
When reading User1 from file and compared against User1 in AD, it will prompt what would you like to change ?

1. sn
2. givenName
3. department
4. title
5. telephoneNumber
6. employeeID
7. employeeNumber
8. Exit

Let's say, option 1 is selected ans surname is changed in AD for User1 in AD.

Questions:
What if DataEntry was incorrect ?
What if want to keep changing information on this USER1 before the script jumps to USER2 and so on and ask .... what would you like to change....?

Thanks for your help,
0
Comment
Question by:namerg
  • 30
  • 21
  • 12
63 Comments
 
LVL 39

Expert Comment

by:footech
Comment Utility
How about something like this?  The choice is set up as a function, one of the choices moves on to the next user, one of the choices exits the script, for the rest you would put in the code to make the change.  The function is continually looped through until you've parsed through all the users or have chosen to exit.
function editchoice 
{
    $title = "Change User Information"
    $message = "What user information do you want to change?"

    $choice0 = New-Object System.Management.Automation.Host.ChoiceDescription "&Last name", "Edit last name."

    $choice1 = New-Object System.Management.Automation.Host.ChoiceDescription "&First name", "Edit first name."

    $choice2 = New-Object System.Management.Automation.Host.ChoiceDescription "&Department", "Edit department."

    $choice3 = New-Object System.Management.Automation.Host.ChoiceDescription "&Title", "Edit title."

    $choice4 = New-Object System.Management.Automation.Host.ChoiceDescription "&Phone number", "Edit telephone number."

    $choice5 = New-Object System.Management.Automation.Host.ChoiceDescription "employee &ID", "Edit employee ID."

    $choice6 = New-Object System.Management.Automation.Host.ChoiceDescription "employee &Number", "Edit employee number."

    $choice7 = New-Object System.Management.Automation.Host.ChoiceDescription "Next &User", "Move on to next User."
    
    $choice8 = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit", "Exit."

    $options = [System.Management.Automation.Host.ChoiceDescription[]]($choice0, $choice1, $choice2, $choice3, $choice4, $choice5, $choice6, $choice7, $choice8)

    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 

    switch ($result)
        {
            0 { scriptblock to change surName }
            1 { scriptblock to change givenName }
            2 { scriptblock }
            3 { scriptblock }
            4 { scriptblock }
            5 {  }
            6 {  }
            7 { $script:sameuser = $false }
            8 { "Exiting..." ; exit }
        }

} #end function editchoice

$listusers = Import-CSV userlist.txt -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ADusers  = (Get-ADUser -filter * -properties sn,givenName,department,title,telephoneNumber,employeeID)
[bool]$sameuser = $true
$i = 0
While ($i -lt $listusers.count )
{
  ForEach ( blah blah)
  {
  #list to ADusers matching code goes here
  #may want to use Compare-Object here
      Do
      {
        editchoice
      } Until ( !($sameuser) )
    $i++
  }
}

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
What happened to the script which you were working on? is it not serving the purpose?
0
 

Author Comment

by:namerg
Comment Utility
Hey Guys.

@footech. I will take a look.

@Subsun: Yes, it is working. I appreciate your help. But, as you know when you are sort of developing, you work by stages with lots of validation and conditionals. While you keep progressing you find out little things to improve or what do with the code.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Okie.. :-) in that case what will be input file and data format?
0
 

Author Comment

by:namerg
Comment Utility
0
 

Author Comment

by:namerg
Comment Utility
@footech,

I got stuck on this part [bool]$sameuser = $true and DO i++

@sameuser could be.....?

Thanks,
PS: Never mind, I got it.
0
 

Author Comment

by:namerg
Comment Utility
I am stuck...

This is the code:
Clear-Host

function Edit_AD_User
	{
	Write-Host "Which of the following Employee information would you like to change in Active Directory ?"
	Write-Host ""
	$AD_User_Change = Read-Host '1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER. 8 = Move on to NEXT USER. 0 = EXIT'
	switch ($AD_User_Change) 
		{
					1 {
						Write-Host ""
						$InputLastName = Read-Host 'Type the new LAST Name:'
						Write-Host ""
						$NewLastName = Read-Host "Are you sure, you want to replace the following Lastname ""$($AD_User.sn)"" by ""$InputLastName"" (Y/N)"
						if (($NewLastName -eq "y"))
						{
							Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
							Start-Sleep -Seconds 5
							$NewName = $InputLastName+", "+$AD_USER.givenName
							Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
							Start-Sleep -Seconds 5
							Write-Host "New LastName SET"
							Write-Host ""
						}
					}
					8 {
						$script:sameuser = $false
					}
					0 {
						Write-Host "Exiting...."
						exit
					}
	}
} #end function Edit_AD_User

$file = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
[bool]$sameuser = $true
$i = 0
While ($i -lt $file.count )
{
	Do 
		{
			foreach ($AD_User in $ad) 
				{ 
					#Do 
					#	{	
							foreach ($file_User in $file)
								{
									#Do 
										#{
											if ($file_User.sn -eq $AD_User.sn)
												{
													Write-Host "Querying Employee from file file against Active Directory"
													Write-Host ""
													Write-Host "+++++ FOUND THE FOLLOWING EMPLOYEE IN file FILE ++++++++++++++++++++++++++"
													Write-Host "LASTNAME: $($file_User.sn)"
													Write-Host "FIRST NAME: $($file_User.givenName)"
													Write-Host "DEPARTMENT: $($file_User.department)"
													Write-Host "TITLE: $($file_User.title)"
													Write-Host "TELEPHONE NUMBER: $($file_User.telephoneNumber)"
													Write-Host "FLEX ID: $($file_User.employeeID)"
													Write-Host "CLOCK NUMBER: $($file_User.employeeNumber)"
													Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
													Write-Host ""
													Start-Sleep -Seconds 5
													#Write-Host ""
													Write-Host "============================ FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =========================="
													Write-Host "LASTNAME: $($AD_USER.sn)"
													Write-Host "FIRST NAME: $($AD_USER.givenName)"
													Write-Host "DEPARTMENT: $($AD_USER.department)"
													Write-Host "TITLE: $($AD_USER.title)"
													Write-Host "TELEPHONE NUMBER: $($AD_USER.telephoneNumber)"
													Write-Host "FLEX ID: $($AD_USER.employeeID)"
													Write-Host "CLOCK NUMBER: $($AD_USER.employeeNumber)"
													Write-Host "=========================================================================================================="
													Write-Host ""
													$RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'
													Write-Host ""
													if ($RightWrong -eq "N") 
														{
															Write-Host "Moving to the next user"
															[void]$foreach.moveNext()
													}
													if ($RightWrong -eq "Y") 
														{
															$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the file File? (Y/N)'
															Write-Host ""
															#Write-Host "YesNo Value: $($YesNo)"
															#if (($RightWrong -eq "Y") -and ($YesNo -eq "N"))
															if ($YesNo -eq "N") 
																{
																	Edit_AD_User
																}
													}
											}
									#} Until (!($sameuser))
									#i++
								} 
					#} Until (!($sameuser))
					#i++
				}
	} Until (!($sameuser))
	i++
}

Open in new window


And this is the Result:

Querying  Employee from file against Active Directory

+++ FOUND THE FOLLOWING EMPLOYEE IN FILE +++++++
LASTNAME: Rosero
FIRST NAME: German
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

============= FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =========
LASTNAME: Rosero
FIRST NAME: German
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID:
CLOCK NUMBER: 2770
=======================================================================

NOTE: Is this the right Employee, You are working on? (Y/N): y

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the File? (Y/N): n

Which of the following Employee information would you like to change in Active Directory ?

1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER. 8 = Move on to NEXT USER. 0 = EXIT: 1

Type the new LAST Name:: Rose

Are you sure, you want to replace the following Lastname "Rosero" by "Rose" (Y/N): y
New LastName SET

###########
# I AM STUCK,  RIGHT HERE. What if i typed the wrong last name ?
# I can ask, "Do you need to change anything else on this user ?" If Yes. I should come #back to the choices statement and of course $AD_USER continues to be Rose, do the #change, will prompt Do you need to change anything else on this user ?, if No, will move #to the next user on the file and AD
#########

Querying Employee from file against Active Directory

+++++++ FOUND THE FOLLOWING EMPLOYEE IN FILE ++++++++++++++++
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

============ FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ========
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/Network Administrator
TELEPHONE NUMBER: 333-333-3333
FLEX ID: 3333
CLOCK NUMBER: 2222
========================================================================

NOTE: Is this the right Employee, You are working on? (Y/N):

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
As your code stands right now, you don't need the While loop (and its $i variable).  I thought it may be of some use based on some earlier scrap that I had seen, but right now it's just extra fluff.

I think you're going to want to restructure some of your elements.  Most likely after each edit you will want to query the info from AD again for the specific user to show its current values.  I'm not sure whether you would want to also re-show the info from the list.

I would put this in at about line 67 or possibly 77
[bool]$sameuser = $true
$iteration = 0
While ($sameuser)
{
  #Include this If statement if the info has already been shown before the While loop
  If ($iteration -ne 0)
  { "show_current_info" }
  If ($iteration -eq 0)
  {
    $iteration++
    $RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'
    Write-Host ""
    if ($RightWrong -eq "N") 
    {
      Write-Host "Moving to the next user"
      [void]$foreach.moveNext()
    }
  }
  #Assume that any response other than "N" means it is the correct user, could use an Else but it's pretty much implied
  edit_AD_User
}

Open in new window


 A choice of "next user" (maybe should be called "None") in response to the question "what information do you want to edit?" would exit the loop and move on, instead of having separate questions for "is the information correct?", but you could put the following right before the edit_AD_User function call if you want.
  $yesNo = Read-Host "is the information correct?"
  If ($yesno -eq "y")
  {break}

Open in new window

In response to your question about typing a wrong last name, etc., I would question why you are entering anything.  Why not just use the information obtained from the file, isn't that what it's for?  But in any case, this loop will keep asking you if you want to change anything, so any screwups can be redone.
0
 

Author Comment

by:namerg
Comment Utility
@footech, i am taking your suggestion from the beginning and is helping me but always get stuck in certain things (I posted a new question)

Do you think if I do a flow chart diagram will give you a better idea of what I am looking for?

Thanks for your help,
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Oh, yes.  I consider flow charts a very good thing, especially when you start getting into loops.
0
 

Author Comment

by:namerg
Comment Utility
ok, will do it tonight.. thanks
0
 

Author Comment

by:namerg
Comment Utility
@footech,

I hope did my best. See the attachment.

thanks,
Visio-Drawing1.pdf
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
My code works with your flowchart, though I still think after each edit you will want to fetch and display the info for the current user before asking if you want to edit again.  Here it is inserted into the sample you provided (there are a couple comments inline):
$file = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
[bool]$sameuser = $true
foreach ($AD_User in $ad) 
{
	foreach ($file_User in $file)
	{
		if ($file_User.sn -eq $AD_User.sn)
		{
			Write-Host "Querying Employee from file file against Active Directory"
			Write-Host ""
			Write-Host "+++++ FOUND THE FOLLOWING EMPLOYEE IN file FILE ++++++++++++++++++++++++++"
			Write-Host "LASTNAME: $($file_User.sn)"
			Write-Host "FIRST NAME: $($file_User.givenName)"
			Write-Host "DEPARTMENT: $($file_User.department)"
			Write-Host "TITLE: $($file_User.title)"
			Write-Host "TELEPHONE NUMBER: $($file_User.telephoneNumber)"
			Write-Host "FLEX ID: $($file_User.employeeID)"
			Write-Host "CLOCK NUMBER: $($file_User.employeeNumber)"
			Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
			Write-Host ""
			Start-Sleep -Seconds 5
			#Write-Host ""
			Write-Host "============================ FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =========================="
			Write-Host "LASTNAME: $($AD_USER.sn)"
			Write-Host "FIRST NAME: $($AD_USER.givenName)"
			Write-Host "DEPARTMENT: $($AD_USER.department)"
			Write-Host "TITLE: $($AD_USER.title)"
			Write-Host "TELEPHONE NUMBER: $($AD_USER.telephoneNumber)"
			Write-Host "FLEX ID: $($AD_USER.employeeID)"
			Write-Host "CLOCK NUMBER: $($AD_USER.employeeNumber)"
			Write-Host "=========================================================================================================="
			Write-Host ""
			$iteration = 0
			While ($sameuser)
			{
				If ($iteration -eq 0)
				{
					$iteration++
					$RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'
					Write-Host ""
					if ($RightWrong -eq "N") #Assume that any response other than "N" means it is the correct user
					{
						Write-Host "Moving to the next user"
						[void]$foreach.moveNext()
					}
				Else 
				{
					#show_current_info  # Can either put the code to show info here, or wrap the code into a function
					Get-ADUser -filter {samAccountName -like "$AD_User.samAccountName"} -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
					#if you want you could assign the previous line to a variable and then use that to display your info like in earlier Write-Host commands.
				}
				$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the file File? (Y/N)'
				Write-Host ""
				if ($YesNo -eq "y") 
				{
					break
				}
				Edit_AD_User
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
@footech, will give a shot and take a look your code.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
A couple issues:
- I left out a closing "}" before the Else on line 47.
- I don't think the [void]$foreach.moveNext() will skip the rest of the code in the loop.  Might be better to put a break statement here.
0
 

Author Comment

by:namerg
Comment Utility
Thank footech, but I am still stuck....

This is the result...

Querying Employee from file file against Active Directory

+++++ FOUND THE FOLLOWING EMPLOYEE IN file FILE ++++++++++++++++++++++++++
LASTNAME: Rosero
FIRST NAME: German
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

============================ FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ==========================
LASTNAME: Rosero
FIRST NAME: German
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID:
CLOCK NUMBER: 2770
==========================================================================================================

NOTE: Is this the right Employee, You are working on? (Y/N): y

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the file File? (Y/N): n

Which of the following Employee information would you like to change in Active Directory ?

1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER. 8 = Move on to NEXT USER. 0 = EXIT: 1

Type the new LAST Name:: Rose

Are you sure, you want to replace the following LASTNAME "Rosero" for "Rose" (Y/N): y
Rose, German
New LastName SET

#LETS SAY I TYPED THE WRONG LAST NAME

Do you still want to correct anything else on this user? (Y/N): y

Which of the following Employee information would you like to change in Active Directory ?

1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = EMPLOYEE ID. 7 = EMPLOYEE NUMBER. 8 = Move on to NEXT USER. 0 = EXIT: 1

Type the new LAST Name:: Rosero

#HERE IS THE PROBLEM, IT SHOULD DISPLAY "Rose" for "Rosero"

Are you sure, you want to replace the following LASTNAME "Rosero" for "Rosero" (Y/N):

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
You're referring to line 49?  That's because you're still using $($AD_User.sn) to display the lastname.  After any change is made to user info, potentially any reference to an object in $ad could be incorrect because $ad was created/populated before the change.  Nothing in the edit function is making a new query for updated information.

One way you might handle this to make the query for AD users (the $ad variable), use it to match against the list like you currently do, but then once the match has been verified and you're inside the loop, perform another query for the specific AD user info and store it to a new variable ($current_user), then use this new variable anytime you need to get or set attributes.  Each time through the While loop the variable would be updated.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Also, I'm still wondering why you would type the name and not just pull the info from the list.
0
 

Author Comment

by:namerg
Comment Utility
Are you referring to line 45 ?

If so, is because what if I entered the wrong last name.

I will take your $current_user suggestion.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
More so line 31 (i.e. the first time you're changing it).  Assuming the info in the file is correct (which is the whole reason you're checking AD against it isn't it), then just update the AD info with info from the file, then you don't have to worry about mistyping anything, and will be simpler and quicker.
0
 

Author Comment

by:namerg
Comment Utility
@footech got it.

But, I think the FOR gets stuck in a loop. This is the result, Line 49

Querying Employee from file file against Active Directory

+++++ FOUND THE FOLLOWING EMPLOYEE IN file FILE ++++++++++++++++++++++++++
LASTNAME: Rosero
FIRST NAME: German
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

======== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ================
LASTNAME: Rosero
FIRST NAME: German
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
========================================================================

NOTE: Is this the right Employee, You are working on? (Y/N): y

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the file File? (Y/N): n

Which of the following Employee information would you like to change in Active Directory ?

1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = FLEX ID. 7 = CLOCK NUMBER. 8 = Move on to NEXT USER. 0 = EXIT: 7

Type the new CLOCK Number:: 2770

Are you sure, you want to replace the following CLOCK number "2222" for "2770" (Y/N): y
New Clock Number SET

Do you still want to correct anything else on this user? (Y/N): y

Which of the following Employee information would you like to change in Active Directory ?

1 = LASTNAME. 2 = FIRST NAME. 3 = DEPARTMENT. 4 = TITLE. 5 = TELEPHONE NUMBER. 6 = FLEX ID. 7 = CLOCK NUMBER. 8 = Move on to NEXT USER. 0 = EXIT: 7

Type the new CLOCK Number:: 4444

Are you sure, you want to replace the following CLOCK number "2770" for "4444" (Y/N): y
New Clock Number SET

Do you still want to correct anything else on this user? (Y/N): n

#RIGHT HERE, WHEN ALL CHANGES ARE DONE FOR A XYZ USER IN AD, @FILE_USER NEEDS TO READ THE NEXT USER AND AD_USER START QUERIES AGAINST AD, CLEAR SCREES AND START ALL OVER WITH THE WRITE HOST. WE ARE ALMOST DONE.

End of Edit_AD_User function

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 125 total points
Comment Utility
Sorry.. I was busy the whole day.. didn't get much time to check your flow chart.. I have quickly done following code which I think will loop as you required. It will prompt to change until you press 0 to exit for a user.. Check and let us know if this is what you are looking for.

PS: I didn't add many check points, just concetrated on the loop.. If it is fine with you then you can add the confirmation questions later..
$File = Import-Csv "c:\scripts\ad\temp\FILE.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Foreach ($User in $FILE){
$Search = $Ad | ? {$_.sn -eq $User.sn}
If ($Search){
	Foreach ($U in $Search) {
 Write-Host "=====================AD User===================================="
 Write-Host "LASTNAME: $($U.sn)"
 Write-Host "FIRST NAME: $($U.givenName)"
 Write-Host "DEPARTMENT: $($U.department)"
 Write-Host "TITLE: $($U.title)"
 Write-Host "TELEPHONE NUMBER: $($U.telephoneNumber)"
 Write-Host "EMPLOYEE ID: $($U.employeeID)"
 Write-Host "EMPLOYEE NUMBER: $($U.employeeNumber)"
 Write-Host "==============================================================="
 Write-Host ""
 Write-Host "=====================CSV User===================================="
 Write-Host "LASTNAME: $($User.sn)"
 Write-Host "FIRST NAME: $($User.givenName)"
 Write-Host "DEPARTMENT: $($User.department)"
 Write-Host "TITLE: $($User.title)"
 Write-Host "TELEPHONE NUMBER: $($User.telephoneNumber)"
 Write-Host "EMPLOYEE ID: $($User.employeeID)"
 Write-Host "EMPLOYEE NUMBER: $($User.employeeNumber)"
 Write-Host "==============================================================="
 Write-Host ""
 $YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
  If ($YesNo -eq "Y"){
   Do{
      $AD_User_Change = Read-Host "
		  1 = LASTNAME.
                  2 = FIRST NAME.
                  3 = DEPARTMENT.
                  4 = TITLE.
                  5 = TELEPHONE NUMBER.
                  6 = EMPLOYEE ID.
                  7 = EMPLOYEE NUMBER.
                  0 = Next User
                  "
                        Switch ($AD_User_Change){
                        1 {"sn code"}
                        2 {"givenName code"}
                        3 {"department code"}
                        4 {"title code"}
                        5 {"telephoneNumber code"}
                        6 {"employeeID code"}
                        7 {"employeeNumber code"}
                        }
                  }
   Until($AD_User_Change -eq "0")
   }
  }
 }
Else {"No user Found in AD for $($User.SN)"}
}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
From my last code post, I needed to move line 3 to around 34 so that the $sameuser variable is reset in the foreach before beginning the While loop.  We could also change the While loop to a Do-Until so that it runs through at least once.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
BTW, I like Subsun's way of repeating the question until you choose "Next User" (same as I had posted in my first comment) instead of asking another Y/N question.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
@namerg - I don't know where the following came from.
"Do you still want to correct anything else on this user? (Y/N): " - as seen on line 47 of your last output.  The code I posted is looping through the users fine in my testing.

If you want the screen to clear between each user, you'll have to add a Clear-Host command inside the ForEach loop.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Sorry for all the posts.
Just thought I'd mention I spotted an error in the logic of the script. You're trying to match the last name of users found in AD against the last name in a file.  If the AD info is wrong (or the list info), you'll never get a match.  So not much point in having an option to change the last name.  You'll probably want some logging commands around where Subsun has "Else {"No user Found in AD for $($User.SN)"}", so that you can manually check any users that don't get a match.
0
 

Author Comment

by:namerg
Comment Utility
@footech. The file is going to be 100% correct. AD not. Hmm, maybe you missed "Do you still want to correct anything else on this user? (Y/N):"

@Subsun: You code worked for the repetitive menu. Although, I do still have issues moving to the next user. It should be something like:

User_File pulled first record from CSV and looks for matching last name in AD. If the lastname is unique no problem. But, if the last name IS NOT unique,(more than one user with a lastname Dean) User_File should keep first record in memory and User_AD should move to the next possible non-unique last name. That is why the question in my previous versions of the code. See below question and result.
"$RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'" 

Open in new window


=====================CSV User====================================
LASTNAME: Dean
FIRST NAME: Patrick
DEPARTMENT: Information Services
TITLE: IS Manager
TELEPHONE NUMBER: 303-303-3034
EMPLOYEE ID: 1111
EMPLOYEE NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Dean
FIRST NAME: Gloria
DEPARTMENT: Information Services
TITLE: Sr Technical Support Analyst
TELEPHONE NUMBER: 303-444-4444
EMPLOYEE ID:
EMPLOYEE NUMBER:
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N): n

Open in new window


Do you want me to close this question and give you poinst to you both ?

Can i open a new question some like PowerShell Project AD & CSV ?
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
At following code If you press "N" then It should move to the next User.

For example If you have two users with surname Dean, Then It will prompt you for those two users for modification, If you say No  to both users then it will move to the Next user in CSV.

$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Can you point me to a code sample where you have "Do you still want to correct anything else on this user? (Y/N): " - I'm not seeing it anywhere.

Even if the file is 100% correct, if the last name in AD is wrong you won't get a match.  So my point stands.

My code is working and Subsun's should too.  I would suggest that you re-examine any code modifications you've made or your sample data that you're testing with to check for blocking issues.
0
 

Author Comment

by:namerg
Comment Utility
@footech: Lastnames in AD are 100% correct

@Subsun.
Check this out.  I am working with your code and adjusted to my needs.
So, file read the first record from csv file, found a match in AD. Perfect,  it prompted to change any information, Perfect. But, Ok. I am done with this user. So, I chose "0" it should move to the next user/record on File.CSV and displays it, then again look for that user in AD.

Guys, I am willing to go straight all night long on this project this coming Friday night. If you guys are willing to help me. I do appreciate it.

=====================EDITED AD User====================================
LASTNAME: Silvei
FIRST NAME: Rod
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================


                  1 = LASTNAME.

                  2 = FIRST NAME.

                  3 = DEPARTMENT.

                  4 = TITLE.

                  5 = TELEPHONE NUMBER.

                  6 = FLEX ID.

                  7 = CLOCK NUMBER.

                  0 = Next User
                  : 0

                  1 = LASTNAME.

                  2 = FIRST NAME.

                  3 = DEPARTMENT.

                  4 = TITLE.

                  5 = TELEPHONE NUMBER.

                  6 = FLEX ID.

                  7 = CLOCK NUMBER.

                  0 = Next User
                  :1

Type the new LAST Name: Silveira
Are you sure, you want to replace the following LASTNAME "Silvei" for "Silveira" (Y/N):

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
Also. let's say I find the following users:

=====================CSV User====================================
LASTNAME: Dean
FIRST NAME: Patrick
DEPARTMENT: Information Services
TITLE: IS Manager
TELEPHONE NUMBER: 303-303-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Dean
FIRST NAME: Gloria
DEPARTMENT: Information Services
TITLE: Sr Technical Support Analyst
TELEPHONE NUMBER: 303-444-4444
FLEX ID:
CLOCK NUMBER:
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N): n

#Not good here, I chose n
#AD should jump to the next user until finds Dean Patrick
#Then it will give me the opportunity to change though the menu.

                 1 = LASTNAME.
                 2 = FIRST NAME.
                 3 = DEPARTMENT.
                 4 = TITLE.
                 5 = TELEPHONE NUMBER
                 6 = FLEX ID.
                 7 = CLOCK NUMBER.
                 0 = Next User
                 :

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 39

Assisted Solution

by:footech
footech earned 375 total points
Comment Utility
This isn't exactly how I would do it if writing from scratch, but in the interest of keeping as much of your current code as possible, this is what I have working (I didn't include the edit function).

$file = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
foreach ($AD_User in $ad) 
{
	foreach ($file_User in $file)
	{
		if ($file_User.sn -eq $AD_User.sn)
		{
			Write-Host "Querying Employee from file file against Active Directory"
			Write-Host ""
			Write-Host "+++++ FOUND THE FOLLOWING EMPLOYEE IN file FILE ++++++++++++++++++++++++++"
			Write-Host "LASTNAME: $($file_User.sn)"
			Write-Host "FIRST NAME: $($file_User.givenName)"
			Write-Host "DEPARTMENT: $($file_User.department)"
			Write-Host "TITLE: $($file_User.title)"
			Write-Host "TELEPHONE NUMBER: $($file_User.telephoneNumber)"
			Write-Host "FLEX ID: $($file_User.employeeID)"
			Write-Host "CLOCK NUMBER: $($file_User.employeeNumber)"
			Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
			Write-Host ""
			Start-Sleep -Seconds 1
			#Write-Host ""
			Write-Host "============================ FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =========================="
			Write-Host "LASTNAME: $($AD_USER.sn)"
			Write-Host "FIRST NAME: $($AD_USER.givenName)"
			Write-Host "DEPARTMENT: $($AD_USER.department)"
			Write-Host "TITLE: $($AD_USER.title)"
			Write-Host "TELEPHONE NUMBER: $($AD_USER.telephoneNumber)"
			Write-Host "FLEX ID: $($AD_USER.employeeID)"
			Write-Host "CLOCK NUMBER: $($AD_USER.employeeNumber)"
			Write-Host "=========================================================================================================="
			Write-Host ""
			[bool]$sameuser = $true
			$iteration = 0
			While ($sameuser)
			{
				If ($iteration -eq 0)
				{
					$iteration++
					$RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'
					Write-Host ""
					if ($RightWrong -eq "N") #Assume that any response other than "N" means it is the correct user
					{
						Write-Host "Moving to the next user"
						break
					}
				}
				Else 
				{
					#show_current_info  # Can either put the code to show info here, or wrap the code into a function
					Get-ADUser -filter {samAccountName -like "$AD_User.samAccountName"} -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
					#if you want you could assign the previous line to a variable and then use that to display your info like in earlier Write-Host commands.
				}
				$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the file File? (Y/N)'
				Write-Host ""
				if ($YesNo -eq "y") 
				{
					break
				}
				Edit_AD_User
			}
		}
	}
}

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
If I've understood you correctly ... Adding break after Until($AD_User_Change -eq "0") should do it..
    ......................
              }
   Until($AD_User_Change -eq "0")
   break
   }
    ......................

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Answer to ID: 38763718, I just tested My code and it's working as you mentioned. If you press N then it will go to next AD user. If there is no AD user available then it will go to next CSV user.

By adding above mentioned code.. If you have one Dean in CSV and 3 Dean in AD and you press 0 after modifying one of the AD user, then it will skip the rest of AD users and go to the CSV user..

I am not sure If something changed when you added your confirmations and tasks.. but the basic code is working for me as you mentioned..

So if it is not working for you then it would be better to post the complete code which you are working on..

We both have given suggestions, so I am confused which one is not working for you.. :-).. I would stick with one code and then work around it..
0
 

Author Comment

by:namerg
Comment Utility
Ok, let's decide...
Which one do you want me to work, footech or subsun ? You too guys are great and gurus, not complain about it.

Thanks,
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
You can choose the code which you can understand and best suits to you... We both can help... Honestly I cannot assure you time (I guess no one can)  as I have a full time job and a Jr.Subsun to take care, but sure will jump in whenever I am free.. :-)
0
 

Author Comment

by:namerg
Comment Utility
Both codes are great. Hmm, okay

@subsun, it does not your code for moving the user. :(
See below.
=====================CSV User====================================
LASTNAME: Dean
FIRST NAME: Patrick
DEPARTMENT: Information Services
TITLE: IS Manager
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Dean
FIRST NAME: Gloria
DEPARTMENT: Information Services
TITLE: Sr Technical Support Analyst
TELEPHONE NUMBER: 303-444-4444
FLEX ID:
CLOCK NUMBER:
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N): n
#Pressed n
                  1 = LASTNAME.
                  2 = FIRST NAME.
                  3 = DEPARTMENT.
                  4 = TITLE.
                  5 = TELEPHONE NUMBER.
                  6 = FLEX ID.
                  7 = CLOCK NUMBER.
                  0 = Next User
                  : 0

#Problem, it should appear as CSV User "Dean, Patrick" and AD "Dean, Patrick"

=====================CSV User====================================
LASTNAME: Bokowski
FIRST NAME: Nancy
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-555-5555
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Bokowski
FIRST NAME: Nancy
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-555-5555
FLEX ID:
CLOCK NUMBER:
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N):

Open in new window


Now, I will try footech's code
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Posting only result wont give much idea as my code works for me.. So Please post the code which you used along with the result..
0
 

Author Comment

by:namerg
Comment Utility
@Subsun
Of course, there are Footech inputs also

#Subsun's code

Clear-Host
function Edit_AD_User {
Do {
	  $AD_User_Change = Read-Host "
		  1 = LASTNAME.
		  
                  2 = FIRST NAME.
				  
                  3 = DEPARTMENT.
				  
                  4 = TITLE.
				  
                  5 = TELEPHONE NUMBER.
				  
                  6 = FLEX ID.
				  
                  7 = CLOCK NUMBER.
				  
                  0 = Next User
                  "
                        Switch ($AD_User_Change){
                        1 {
							$flag1 = 1
							Write-Host ""
							$InputLastName = Read-Host 'Type the new LAST Name'
							Write-Host ""
							$NewLastName = Read-Host "Are you sure, you want to replace the following LASTNAME ""$($AD_User.sn)"" for ""$InputLastName"" (Y/N)"
							if (($NewLastName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $InputLastName+", "+$AD_USER.givenName
								write-host "$($NewName)"
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								Write-Host "New LastName SET"
								Write-Host ""
								#Write-Host $($AD_USER)
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.sn = $InputLastName
									Edit_AD_User
								}
								Else { 
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($InputLastName)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
								}
							}
						}	
                        2 {
							$flag2 = 2
							Write-Host ""
							$InputFirstName = Read-Host 'Type the new FIRST Name'
							Write-Host ""
							$NewFirstName = Read-Host "Are you sure, you want to replace the following FIRSTNAME ""$($AD_User.givenName)"" FOR ""$InputFirstName"" (Y/N)"
							if (($NewFirstName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -givenName $InputFirstName -DisplayName $($AD_USER.sn+", "+$InputFirstName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $AD_USER.sn+", "+$InputFirstName
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								Write-Host "New FirstName SET"
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.givenName = $InputFirstName
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($InputFirstName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
								}
							}  
						}
                        3 {
							$flag3 = 3
							Write-Host ""
							$InputDepartmentName = Read-Host 'Type the new DEPARTMENT Name'
							Write-Host ""
							$NewDepartmentName = Read-Host "Are you sure, you want to replace the following DEPARTMENT ""$($AD_User.department)"" for ""$InputDepartmentName"" (Y/N)"
							if (($NewDepartmentName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -department $InputDepartmentName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									Write-Host "New DepartmentName SET"
									Start-Sleep -Seconds 3
									Write-Host ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									Write-Host ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.department = $InputDepartmentName
										Edit_AD_User
									}
									Else {
										Clear-Host
										Write-Host ""
										Write-Host "=====================EDITED AD User===================================="
										Write-Host "LASTNAME: $($AD_USER.sn)"
										#Write-Host "FIRST NAME: $($AD_User.givenName)"
										Write-Host "FIRST NAME: $($InputFirstName)"
										Write-Host "DEPARTMENT: $($InputDepartmentName)"
										Write-Host "TITLE: $($AD_User.title)"
										Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										Write-Host "FLEX ID: $($AD_User.employeeID)"
										Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
										Write-Host "==============================================================="
										Write-Host ""
									}
							}
						}
                        4 {
							$flag4 = 4
							Write-Host ""
							Write-Host "$($AD_User.title)"
							$InputTitleName = Read-Host 'Type the new TITLE Name'
							Write-Host ""
							$NewTitleName = Read-Host "Are you sure, you want to replace the following TITLE ""$($AD_User.title)"" for ""$InputTitleName"" (Y/N)"
							if (($NewTitleName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -title $InputTitleName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									Write-Host "New TitleName SET"
									Start-Sleep -Seconds 3
									Write-Host ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									Write-Host ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.title = $InputTitleName
										Edit_AD_User
									}
									ElseIf (($flag2 -ne 2) -and ($flag3 -ne 3) -and ($flag4 -eq 4)) {
										Clear-Host
										Write-Host ""
										Write-Host "=====================EDITED AD User===================================="
										Write-Host "LASTNAME: $($AD_USER.sn)"
										Write-Host "FIRST NAME: $($AD_USER.givenName)"
										Write-Host "DEPARTMENT: $($AD_USER.Department)"
										Write-Host "TITLE: $($InputTitleName)"
										Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										Write-Host "FLEX ID: $($AD_User.employeeID)"
										Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
										Write-Host "==============================================================="
										Write-Host ""
										$AD_User.title = $InputTitleName
									}
							}
						}
                        5 {
							Write-Host ""
							$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
							Write-Host ""
							$NewTelephoneNumber = Read-Host "Are you sure, you want to replace the following TELEPHONE Number ""$($AD_User.telephoneNumber)"" for ""$InputTelephoneNumber"" (Y/N)"
							if (($NewTelephoneNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -OfficePhone $InputTelephoneNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								Write-Host "New TelephoneNumber SET"
								Start-Sleep -Seconds 3
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.telephoneNumber = $InputTelephoneNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($InputTelephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
									$AD_User.telephoneNumber = $InputTelephoneNumber
								}
							}
						}
                        6 {
							Write-Host ""
							$InputFlexIDNumber = Read-Host 'Type the new FLEX ID Number'
							Write-Host ""
							$NewFlexIDNumber = Read-Host "Are you sure, you want to replace the following FLEXID Number ""$($AD_User.employeeID)"" for ""$InputFlexIDNumber"" (Y/N)"
							if (($NewFlexIDNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeID $InputFlexIDNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								Write-Host "New FlexID Number SET"
								Start-Sleep -Seconds 3
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeID = $InputFlexIDNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($InputFlexIDNumber)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
									$AD_User.employeeID = $InputFlexIDNumber
								}
							}
						}
                        7 {
							Write-Host ""
							$InputEmployeeNumber = Read-Host 'Type the new CLOCK Number'
							Write-Host ""
							$NewEmployeeNumber = Read-Host "Are you sure, you want to replace the following CLOCK number ""$($AD_User.employeeNumber)"" for ""$InputEmployeeNumber"" (Y/N)"
							if (($NewEmployeeNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeNumber $InputEmployeeNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								Write-Host "New Employee Number SET"
								Start-Sleep -Seconds 3
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeNumber = $InputEmployeeNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($InputEmployeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
									$AD_User.employeeNumber = $InputEmployeeNumber
								}
							}
						}
                    }
	} Until($AD_User_Change -eq "0")
	break
} #end function Edit_AD_User

$File = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Foreach ($User in $FILE){
	$Search = $Ad | ? {$_.sn -eq $User.sn}
	If ($Search){
		Foreach ($AD_User in $Search) {
			Write-Host "=====================CSV User===================================="
			Write-Host "LASTNAME: $($User.sn)"
			Write-Host "FIRST NAME: $($User.givenName)"
			Write-Host "DEPARTMENT: $($User.department)"
			Write-Host "TITLE: $($User.title)"
			Write-Host "TELEPHONE NUMBER: $($User.telephoneNumber)"
			Write-Host "FLEX ID: $($User.employeeID)"
			Write-Host "CLOCK NUMBER: $($User.employeeNumber)"
			Write-Host "==============================================================="
			Write-Host ""
			Write-Host "=====================AD User===================================="
			Write-Host "LASTNAME: $($AD_User.sn)"
			Write-Host "FIRST NAME: $($AD_User.givenName)"
			Write-Host "DEPARTMENT: $($AD_User.department)"
			Write-Host "TITLE: $($AD_User.title)"
			Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
			Write-Host "FLEX ID: $($AD_User.employeeID)"
			Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
			Write-Host "==============================================================="
			Write-Host ""
			$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
			If ($YesNo -eq "N"){
				Edit_AD_User
			}
			Else {
				clear-host
			}
		}
	}
	Else {"No user Found in AD for $($User.SN)"}
}

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'

Open in new window

For this question If I press Y the you don't want to edit the user right?
0
 

Author Comment

by:namerg
Comment Utility
Correct. if information from User-File is correct with User-AD, (Y) is chosen and File should move to the next record and Queries AD one more time to find a possible match according with last name from the user on File.

But, if user on file matches lastname with aduser but everything else is different, aduser should jump to the next record until finds the right user.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Try adding break after Clear-host
			If ($YesNo -eq "N"){
				Edit_AD_User
			}
			Else {
				Clear-host
                                Break			
                        }

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
@subsun, nada.

=====================CSV User====================================
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N): Y

#Chose (Y)

=====================CSV User====================================
LASTNAME: Dean
FIRST NAME: Patrick
DEPARTMENT: Information Services
TITLE: IS Manager
TELEPHONE NUMBER: 303-444-4444
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Dean
FIRST NAME: Gloria
DEPARTMENT: Information Services
TITLE: Sr Technical Support Analyst
TELEPHONE NUMBER: 303-555-5555
FLEX ID:
CLOCK NUMBER:
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N): N

#Chose (N)


 1 = LASTNAME.
 2 = FIRST NAME.
 3 = DEPARTMENT.
 4 = TITLE.
 5 = TELEPHONE NUMBER
 6 = FLEX ID.
 7 = CLOCK NUMBER.

 0 = Next User
 :
#Chose (0)
#Problem, it does not  display Dean, Patrick on AD.

=====================CSV User====================================
LASTNAME: Bokowski
FIRST NAME: Nancy
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-666-6666
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Bokowski
FIRST NAME: Nancy
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-666-6666
FLEX ID:
CLOCK NUMBER:
===============================================================

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N):

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
#Chose (0)
#Problem, it does not  display Dean, Patrick on AD.

This is how the script works..When it go to edit mode it will loop in attribute changes until you press 0.. Once you press 0 it will finish edit and move to next CSV user. Else it will b confusing..

IMO You probably want to reface the question..
NOTE: Do you want to Edit this AD user? (Y/N):  or something like that.. Press Y to Edit and N to move to next user.. Does it make sense?
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Or introduce a skip in question. S for skip to next AD user (If there is no AD user it will skip to next CSV user)..

N = Edit
Y = Next CSV
S = Next AD/CSV

$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N/S)'
If ($YesNo -eq "N") { Edit_AD_User}
Elseif ($YesNo -eq "S") {Cls }
Else {Cls;Break}

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
#Chose (0)
#Problem, it does not  display Dean, Patrick on AD.

I guess the logic is not working. Needs to be changed.

NOTE: Do you want to Edit this AD user? (Y/N): instead of "NOTE: Is the EMPLOYEE Information FOUND in ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND in the FILE File? (Y/N): ?
0
 

Author Comment

by:namerg
Comment Utility
@footech, how would you do it ?
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Limiting my responses to just the looping functionality...

I think for it to work the way you're expecting, from your last code post, remove the break from line 274, and keep the break after the Clear-Host around line 308 as mentioned in comment #a38764695.

Other option is to rework the question and choices a bit.  See the following (you may notice that I changed all the Write-Host commands to Write-Output, doesn't really change what you will see with the functionality of the script at this point, but makes viewing a transcript much easier):
Clear-Host
function Edit_AD_User {
Do {
	Write-Output "What information do you want to edit?"
	  $AD_User_Change = Read-Host "
		  1 = LASTNAME.
		  
                  2 = FIRST NAME.
				  
                  3 = DEPARTMENT.
				  
                  4 = TITLE.
				  
                  5 = TELEPHONE NUMBER.
				  
                  6 = FLEX ID.
				  
                  7 = CLOCK NUMBER.

                  8 = None - info is correct.
				  
                  0 = Incorrect match - try Next User
                  "
                        Switch ($AD_User_Change){
                        1 {
							$flag1 = 1
							write-output ""
							$InputLastName = Read-Host 'Type the new LAST Name'
							write-output ""
							$NewLastName = Read-Host "Are you sure, you want to replace the following LASTNAME ""$($AD_User.sn)"" for ""$InputLastName"" (Y/N)"
							if (($NewLastName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $InputLastName+", "+$AD_USER.givenName
								write-output "$($NewName)"
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								write-output "New LastName SET"
								write-output ""
								#write-output $($AD_USER)
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.sn = $InputLastName
									Edit_AD_User
								}
								Else { 
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($InputLastName)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
								}
							}
						}	
                        2 {
							$flag2 = 2
							write-output ""
							$InputFirstName = Read-Host 'Type the new FIRST Name'
							write-output ""
							$NewFirstName = Read-Host "Are you sure, you want to replace the following FIRSTNAME ""$($AD_User.givenName)"" FOR ""$InputFirstName"" (Y/N)"
							if (($NewFirstName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -givenName $InputFirstName -DisplayName $($AD_USER.sn+", "+$InputFirstName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $AD_USER.sn+", "+$InputFirstName
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								write-output "New FirstName SET"
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.givenName = $InputFirstName
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($InputFirstName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
								}
							}  
						}
                        3 {
							$flag3 = 3
							write-output ""
							$InputDepartmentName = Read-Host 'Type the new DEPARTMENT Name'
							write-output ""
							$NewDepartmentName = Read-Host "Are you sure, you want to replace the following DEPARTMENT ""$($AD_User.department)"" for ""$InputDepartmentName"" (Y/N)"
							if (($NewDepartmentName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -department $InputDepartmentName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									write-output "New DepartmentName SET"
									Start-Sleep -Seconds 3
									write-output ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									write-output ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.department = $InputDepartmentName
										Edit_AD_User
									}
									Else {
										Clear-Host
										write-output ""
										write-output "=====================EDITED AD User===================================="
										write-output "LASTNAME: $($AD_USER.sn)"
										#write-output "FIRST NAME: $($AD_User.givenName)"
										write-output "FIRST NAME: $($InputFirstName)"
										write-output "DEPARTMENT: $($InputDepartmentName)"
										write-output "TITLE: $($AD_User.title)"
										write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										write-output "FLEX ID: $($AD_User.employeeID)"
										write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
										write-output "==============================================================="
										write-output ""
									}
							}
						}
                        4 {
							$flag4 = 4
							write-output ""
							write-output "$($AD_User.title)"
							$InputTitleName = Read-Host 'Type the new TITLE Name'
							write-output ""
							$NewTitleName = Read-Host "Are you sure, you want to replace the following TITLE ""$($AD_User.title)"" for ""$InputTitleName"" (Y/N)"
							if (($NewTitleName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -title $InputTitleName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									write-output "New TitleName SET"
									Start-Sleep -Seconds 3
									write-output ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									write-output ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.title = $InputTitleName
										Edit_AD_User
									}
									ElseIf (($flag2 -ne 2) -and ($flag3 -ne 3) -and ($flag4 -eq 4)) {
										Clear-Host
										write-output ""
										write-output "=====================EDITED AD User===================================="
										write-output "LASTNAME: $($AD_USER.sn)"
										write-output "FIRST NAME: $($AD_USER.givenName)"
										write-output "DEPARTMENT: $($AD_USER.Department)"
										write-output "TITLE: $($InputTitleName)"
										write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										write-output "FLEX ID: $($AD_User.employeeID)"
										write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
										write-output "==============================================================="
										write-output ""
										$AD_User.title = $InputTitleName
									}
							}
						}
                        5 {
							write-output ""
							$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
							write-output ""
							$NewTelephoneNumber = Read-Host "Are you sure, you want to replace the following TELEPHONE Number ""$($AD_User.telephoneNumber)"" for ""$InputTelephoneNumber"" (Y/N)"
							if (($NewTelephoneNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -OfficePhone $InputTelephoneNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								write-output "New TelephoneNumber SET"
								Start-Sleep -Seconds 3
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.telephoneNumber = $InputTelephoneNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($InputTelephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
									$AD_User.telephoneNumber = $InputTelephoneNumber
								}
							}
						}
                        6 {
							write-output ""
							$InputFlexIDNumber = Read-Host 'Type the new FLEX ID Number'
							write-output ""
							$NewFlexIDNumber = Read-Host "Are you sure, you want to replace the following FLEXID Number ""$($AD_User.employeeID)"" for ""$InputFlexIDNumber"" (Y/N)"
							if (($NewFlexIDNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeID $InputFlexIDNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								write-output "New FlexID Number SET"
								Start-Sleep -Seconds 3
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeID = $InputFlexIDNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($InputFlexIDNumber)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
									$AD_User.employeeID = $InputFlexIDNumber
								}
							}
						}
                        7 {
							write-output ""
							$InputEmployeeNumber = Read-Host 'Type the new CLOCK Number'
							write-output ""
							$NewEmployeeNumber = Read-Host "Are you sure, you want to replace the following CLOCK number ""$($AD_User.employeeNumber)"" for ""$InputEmployeeNumber"" (Y/N)"
							if (($NewEmployeeNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeNumber $InputEmployeeNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								write-output "New Employee Number SET"
								Start-Sleep -Seconds 3
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeNumber = $InputEmployeeNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($InputEmployeeNumber)"
									write-output "==============================================================="
									write-output ""
									$AD_User.employeeNumber = $InputEmployeeNumber
								}
							}
						}
                        8 { break userloop }
                    }
	} Until($AD_User_Change -eq "0")
} #end function Edit_AD_User

$File = Import-Csv ""c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Foreach ($User in $FILE){
	$Search = $Ad | ? {$_.sn -eq $User.sn}
	If ($Search){
		:userloop Foreach ($AD_User in $Search) {
			Clear-Host
			write-output "=====================CSV User===================================="
			write-output "LASTNAME: $($User.sn)"
			write-output "FIRST NAME: $($User.givenName)"
			write-output "DEPARTMENT: $($User.department)"
			write-output "TITLE: $($User.title)"
			write-output "TELEPHONE NUMBER: $($User.telephoneNumber)"
			write-output "FLEX ID: $($User.employeeID)"
			write-output "CLOCK NUMBER: $($User.employeeNumber)"
			write-output "==============================================================="
			write-output ""
			write-output "=====================AD User===================================="
			write-output "LASTNAME: $($AD_User.sn)"
			write-output "FIRST NAME: $($AD_User.givenName)"
			write-output "DEPARTMENT: $($AD_User.department)"
			write-output "TITLE: $($AD_User.title)"
			write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
			write-output "FLEX ID: $($AD_User.employeeID)"
			write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
			write-output "==============================================================="
			write-output ""
			Edit_AD_User			
		}
	}
	Else {"No user Found in AD for $($User.SN)"}
}

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
Guys, I am sorry If I am wasting your time.

I do not think my logic was the right one after some deep thinking.

Now I have 3 functions, and check line 25:
function Search_User_File {
foreach  ($User in $FILE) {
      Search_User_AD
}
}

function Search_User_AD {
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=COMPANY,DC=COMPANYcolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Search = $Ad | ? {$_.sn -eq $User.sn}
If ($Search) {
   Foreach ($AD_User in $Search)  {
      Write-Host "=============CSV User==============="
      Write-Host ""
      Write-Host "=============AD User==============="
      Write-Host ""
      $RightWrong = Read-Host 'Is this the RIGHT EMPLOYEE we are working (Y/N)'
      If ($RightWrong -eq "Y") {
			write-host ""
				$Correct = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In AD CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
				If (($RightWrong -eq "Y") -and ($Correct -eq "N")){
					Edit_AD_User
				}
				Elseif (($RightWrong -eq "Y") -and ($Correct -eq "Y"))  {
                                 clear-host
#RIGHT HERE, HOW DO I MOVE.NEXT ON SEARCH_USER_FILE ?
#IF I DO NOT DO IT, THEN  Search_User_File IS GOING TO START ALL OVER.
                                 Search_User_File
                              }
        }
}
}
function Edit_User_AD {

}

$File = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Search_User_File

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Did you try both of my suggestions?

I don't think I see a point in making what was in place into separate functions.  If you just want separate questions for: 1) confirming the user; and, 2) confirming (or not) the user information is correct, before being presented with any edit options - then you can add that to the code we were just working with.  What you just posted appears to go back to what we had in comment #a38763820
0
 

Author Comment

by:namerg
Comment Utility
@Footech,

The think is the ForEach loop are sequential and they will never go back at least for the AD part. So, I had make it a function.
0
 

Author Comment

by:namerg
Comment Utility
@footech, trying your suggestions now.
0
 

Author Comment

by:namerg
Comment Utility
Hmm,  same thing....
=====================CSV User====================================
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================

=====================AD User====================================
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
FLEX ID: 1111
CLOCK NUMBER: 2222
===============================================================


                  1 = LASTNAME.
                  2 = FIRST NAME.
                  3 = DEPARTMENT.
                  4 = TITLE.
                  5 = TELEPHONE NUMBER.
                  6 = FLEX ID.
                  7 = CLOCK NUMBER.
                  8 = None - Info is Correct
                  0 = Incorrect match - Try Next User
                  : 8
#RIGHT HERE BY CHOOSING 8, CSV SHOULD MOVE TO THE NEXT RECORD ON THE CSV FILE
#AND AD STARTS A NEW QUERY AGAINST THAT NEW  RECORD FOUND ON CSV FILE.
                  1 = LASTNAME.
                  2 = FIRST NAME.
                  3 = DEPARTMENT.
                  4 = TITLE.
                  5 = TELEPHONE NUMBER.
                  6 = FLEX ID.
                  7 = CLOCK NUMBER.
                  8 = None - Info is Correct
                  0 = Incorrect match - Try Next User
                  :

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Did you copy all the code I posted and just modify it for your environment or did you pull out the bits I changed and paste it back into your code?  If the latter, I think something was missed.  I think you would get the results above if you didn't label the Foreach loop so the Break doesn't work as intended.

In my testing, everytime I press 8 it advances to the next user in the CSV.  The one thing I hadn't verified was the following scenario:
If there are 2 users with the last name "Smith" in the CSV, but only 1 in AD, pressing 0 after the first (incorrect) match is found advances to the next user in the CSV without showing a notification that no further match was found in AD.
0
 

Author Comment

by:namerg
Comment Utility
This is what I have:

Clear-Host
function Edit_AD_User {
Do {
	  $AD_User_Change = Read-Host "
		  1 = LASTNAME.		  
                  2 = FIRST NAME.
                  3 = DEPARTMENT.
                  4 = TITLE.
                  5 = TELEPHONE NUMBER.
                  6 = FLEX ID.
                  7 = CLOCK NUMBER.
		  8 = None - Info is Correct
                  0 = Incorrect match - Try Next User
                  "
                        Switch ($AD_User_Change){
                        1 {
							$flag1 = 1
							Write-Host ""
							$InputLastName = Read-Host 'Type the new LAST Name'
							Write-Host ""
							$NewLastName = Read-Host "Are you sure, you want to replace the following LASTNAME ""$($AD_User.sn)"" for ""$InputLastName"" (Y/N)"
							if (($NewLastName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $InputLastName+", "+$AD_USER.givenName
								write-host "$($NewName)"
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								Write-Host "New LastName SET"
								Write-Host ""
								#Write-Host $($AD_USER)
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.sn = $InputLastName
									Edit_AD_User
								}
								Else { 
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($InputLastName)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
								}
							}
						}	
                        2 {
							$flag2 = 2
							Write-Host ""
							$InputFirstName = Read-Host 'Type the new FIRST Name'
							Write-Host ""
							$NewFirstName = Read-Host "Are you sure, you want to replace the following FIRSTNAME ""$($AD_User.givenName)"" FOR ""$InputFirstName"" (Y/N)"
							if (($NewFirstName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -givenName $InputFirstName -DisplayName $($AD_USER.sn+", "+$InputFirstName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $AD_USER.sn+", "+$InputFirstName
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								Write-Host "New FirstName SET"
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.givenName = $InputFirstName
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($InputFirstName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
								}
							}  
						}
                        3 {
							$flag3 = 3
							Write-Host ""
							$InputDepartmentName = Read-Host 'Type the new DEPARTMENT Name'
							Write-Host ""
							$NewDepartmentName = Read-Host "Are you sure, you want to replace the following DEPARTMENT ""$($AD_User.department)"" for ""$InputDepartmentName"" (Y/N)"
							if (($NewDepartmentName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -department $InputDepartmentName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									Write-Host "New DepartmentName SET"
									Start-Sleep -Seconds 3
									Write-Host ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									Write-Host ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.department = $InputDepartmentName
										Edit_AD_User
									}
									Else {
										Clear-Host
										Write-Host ""
										Write-Host "=====================EDITED AD User===================================="
										Write-Host "LASTNAME: $($AD_USER.sn)"
										#Write-Host "FIRST NAME: $($AD_User.givenName)"
										Write-Host "FIRST NAME: $($InputFirstName)"
										Write-Host "DEPARTMENT: $($InputDepartmentName)"
										Write-Host "TITLE: $($AD_User.title)"
										Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										Write-Host "FLEX ID: $($AD_User.employeeID)"
										Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
										Write-Host "==============================================================="
										Write-Host ""
									}
							}
						}
                        4 {
							$flag4 = 4
							Write-Host ""
							Write-Host "$($AD_User.title)"
							$InputTitleName = Read-Host 'Type the new TITLE Name'
							Write-Host ""
							$NewTitleName = Read-Host "Are you sure, you want to replace the following TITLE ""$($AD_User.title)"" for ""$InputTitleName"" (Y/N)"
							if (($NewTitleName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -title $InputTitleName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									Write-Host "New TitleName SET"
									Start-Sleep -Seconds 3
									Write-Host ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									Write-Host ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.title = $InputTitleName
										Edit_AD_User
									}
									ElseIf (($flag2 -ne 2) -and ($flag3 -ne 3) -and ($flag4 -eq 4)) {
										Clear-Host
										Write-Host ""
										Write-Host "=====================EDITED AD User===================================="
										Write-Host "LASTNAME: $($AD_USER.sn)"
										Write-Host "FIRST NAME: $($AD_USER.givenName)"
										Write-Host "DEPARTMENT: $($AD_USER.Department)"
										Write-Host "TITLE: $($InputTitleName)"
										Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										Write-Host "FLEX ID: $($AD_User.employeeID)"
										Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
										Write-Host "==============================================================="
										Write-Host ""
										$AD_User.title = $InputTitleName
									}
							}
						}
                        5 {
							Write-Host ""
							$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
							Write-Host ""
							$NewTelephoneNumber = Read-Host "Are you sure, you want to replace the following TELEPHONE Number ""$($AD_User.telephoneNumber)"" for ""$InputTelephoneNumber"" (Y/N)"
							if (($NewTelephoneNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -OfficePhone $InputTelephoneNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								Write-Host "New TelephoneNumber SET"
								Start-Sleep -Seconds 3
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.telephoneNumber = $InputTelephoneNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($InputTelephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
									$AD_User.telephoneNumber = $InputTelephoneNumber
								}
							}
						}
                        6 {
							Write-Host ""
							$InputFlexIDNumber = Read-Host 'Type the new FLEX ID Number'
							Write-Host ""
							$NewFlexIDNumber = Read-Host "Are you sure, you want to replace the following FLEXID Number ""$($AD_User.employeeID)"" for ""$InputFlexIDNumber"" (Y/N)"
							if (($NewFlexIDNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeID $InputFlexIDNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								Write-Host "New FlexID Number SET"
								Start-Sleep -Seconds 3
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeID = $InputFlexIDNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($InputFlexIDNumber)"
									Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
									$AD_User.employeeID = $InputFlexIDNumber
								}
							}
						}
                        7 {
							Write-Host ""
							$InputEmployeeNumber = Read-Host 'Type the new CLOCK Number'
							Write-Host ""
							$NewEmployeeNumber = Read-Host "Are you sure, you want to replace the following CLOCK number ""$($AD_User.employeeNumber)"" for ""$InputEmployeeNumber"" (Y/N)"
							if (($NewEmployeeNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeNumber $InputEmployeeNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								Write-Host "New Employee Number SET"
								Start-Sleep -Seconds 3
								Write-Host ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								Write-Host ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeNumber = $InputEmployeeNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									Write-Host ""
									Write-Host "=====================EDITED AD User===================================="
									Write-Host "LASTNAME: $($AD_USER.sn)"
									Write-Host "FIRST NAME: $($AD_User.givenName)"
									Write-Host "DEPARTMENT: $($AD_User.department)"
									Write-Host "TITLE: $($AD_User.title)"
									Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									Write-Host "FLEX ID: $($AD_User.employeeID)"
									Write-Host "CLOCK NUMBER: $($InputEmployeeNumber)"
									Write-Host "==============================================================="
									Write-Host ""
									$AD_User.employeeNumber = $InputEmployeeNumber
								}
							}
						}
						8	{ break }
                    }
	} Until($AD_User_Change -eq "0")
	#break
} #end function Edit_AD_User

$File = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=company,DC=companycolo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Foreach ($User in $FILE){
	$Search = $Ad | ? {$_.sn -eq $User.sn}
	If ($Search){
		Foreach ($AD_User in $Search) {
			Write-Host "=====================CSV User===================================="
			Write-Host "LASTNAME: $($User.sn)"
			Write-Host "FIRST NAME: $($User.givenName)"
			Write-Host "DEPARTMENT: $($User.department)"
			Write-Host "TITLE: $($User.title)"
			Write-Host "TELEPHONE NUMBER: $($User.telephoneNumber)"
			Write-Host "FLEX ID: $($User.employeeID)"
			Write-Host "CLOCK NUMBER: $($User.employeeNumber)"
			Write-Host "==============================================================="
			Write-Host ""
			Write-Host "=====================AD User===================================="
			Write-Host "LASTNAME: $($AD_User.sn)"
			Write-Host "FIRST NAME: $($AD_User.givenName)"
			Write-Host "DEPARTMENT: $($AD_User.department)"
			Write-Host "TITLE: $($AD_User.title)"
			Write-Host "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
			Write-Host "FLEX ID: $($AD_User.employeeID)"
			Write-Host "CLOCK NUMBER: $($AD_User.employeeNumber)"
			Write-Host "==============================================================="
			Write-Host ""
			#$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the FILE File? (Y/N)'
			#If ($YesNo -eq "N"){
				Edit_AD_User
			#}
			#Else {
			#	clear-host
			#	break
			#}
		}
	}
	Else {"No user Found in AD for $($User.SN)"}
}

Open in new window

0
 
LVL 39

Assisted Solution

by:footech
footech earned 375 total points
Comment Utility
...which isn't the same as what I posted.  The break in the function has to reference a label to break out of the proper loop (which has to have the same label).

Here's the code again, copy it in it's entirety and just edit the parts necessary for your environment or use a file comparison utility to help you spot all the differences between what you posted.  FYI, I have also included some additional code to handle the scenario I mentioned in my previous post.

function Edit_AD_User {
Do {
	Write-Output "What information do you want to edit?"
	  $AD_User_Change = Read-Host "
		  1 = LASTNAME.
		  
                  2 = FIRST NAME.
				  
                  3 = DEPARTMENT.
				  
                  4 = TITLE.
				  
                  5 = TELEPHONE NUMBER.
				  
                  6 = FLEX ID.
				  
                  7 = CLOCK NUMBER.

                  8 = None - info is correct.
				  
                  0 = Incorrect match - try Next User
                  "
                        Switch ($AD_User_Change){
                        1 {
							$flag1 = 1
							write-output ""
							$InputLastName = Read-Host 'Type the new LAST Name'
							write-output ""
							$NewLastName = Read-Host "Are you sure, you want to replace the following LASTNAME ""$($AD_User.sn)"" for ""$InputLastName"" (Y/N)"
							if (($NewLastName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -surname $InputLastName -DisplayName $($InputLastName+", "+$AD_USER.givenName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $InputLastName+", "+$AD_USER.givenName
								write-output "$($NewName)"
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								write-output "New LastName SET"
								write-output ""
								#write-output $($AD_USER)
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.sn = $InputLastName
									Edit_AD_User
								}
								Else { 
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($InputLastName)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
								}
							}
						}	
                        2 {
							$flag2 = 2
							write-output ""
							$InputFirstName = Read-Host 'Type the new FIRST Name'
							write-output ""
							$NewFirstName = Read-Host "Are you sure, you want to replace the following FIRSTNAME ""$($AD_User.givenName)"" FOR ""$InputFirstName"" (Y/N)"
							if (($NewFirstName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -givenName $InputFirstName -DisplayName $($AD_USER.sn+", "+$InputFirstName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $AD_USER.sn+", "+$InputFirstName
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								Start-Sleep -Seconds 3
								write-output "New FirstName SET"
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.givenName = $InputFirstName
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($InputFirstName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
								}
							}  
						}
                        3 {
							$flag3 = 3
							write-output ""
							$InputDepartmentName = Read-Host 'Type the new DEPARTMENT Name'
							write-output ""
							$NewDepartmentName = Read-Host "Are you sure, you want to replace the following DEPARTMENT ""$($AD_User.department)"" for ""$InputDepartmentName"" (Y/N)"
							if (($NewDepartmentName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -department $InputDepartmentName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									write-output "New DepartmentName SET"
									Start-Sleep -Seconds 3
									write-output ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									write-output ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.department = $InputDepartmentName
										Edit_AD_User
									}
									Else {
										Clear-Host
										write-output ""
										write-output "=====================EDITED AD User===================================="
										write-output "LASTNAME: $($AD_USER.sn)"
										#write-output "FIRST NAME: $($AD_User.givenName)"
										write-output "FIRST NAME: $($InputFirstName)"
										write-output "DEPARTMENT: $($InputDepartmentName)"
										write-output "TITLE: $($AD_User.title)"
										write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										write-output "FLEX ID: $($AD_User.employeeID)"
										write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
										write-output "==============================================================="
										write-output ""
									}
							}
						}
                        4 {
							$flag4 = 4
							write-output ""
							write-output "$($AD_User.title)"
							$InputTitleName = Read-Host 'Type the new TITLE Name'
							write-output ""
							$NewTitleName = Read-Host "Are you sure, you want to replace the following TITLE ""$($AD_User.title)"" for ""$InputTitleName"" (Y/N)"
							if (($NewTitleName -eq "y")) {
									Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -title $InputTitleName -ErrorAction SilentlyContinue -ErrorVariable Err1
									Start-Sleep -Seconds 3
									write-output "New TitleName SET"
									Start-Sleep -Seconds 3
									write-output ""
									$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
									write-output ""
									If ($ChangeYesNo -eq "Y") {
										$AD_User.title = $InputTitleName
										Edit_AD_User
									}
									ElseIf (($flag2 -ne 2) -and ($flag3 -ne 3) -and ($flag4 -eq 4)) {
										Clear-Host
										write-output ""
										write-output "=====================EDITED AD User===================================="
										write-output "LASTNAME: $($AD_USER.sn)"
										write-output "FIRST NAME: $($AD_USER.givenName)"
										write-output "DEPARTMENT: $($AD_USER.Department)"
										write-output "TITLE: $($InputTitleName)"
										write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
										write-output "FLEX ID: $($AD_User.employeeID)"
										write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
										write-output "==============================================================="
										write-output ""
										$AD_User.title = $InputTitleName
									}
							}
						}
                        5 {
							write-output ""
							$InputTelephoneNumber = Read-Host 'Type the new TELEPHONE Number'
							write-output ""
							$NewTelephoneNumber = Read-Host "Are you sure, you want to replace the following TELEPHONE Number ""$($AD_User.telephoneNumber)"" for ""$InputTelephoneNumber"" (Y/N)"
							if (($NewTelephoneNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -OfficePhone $InputTelephoneNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								write-output "New TelephoneNumber SET"
								Start-Sleep -Seconds 3
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.telephoneNumber = $InputTelephoneNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($InputTelephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
									$AD_User.telephoneNumber = $InputTelephoneNumber
								}
							}
						}
                        6 {
							write-output ""
							$InputFlexIDNumber = Read-Host 'Type the new FLEX ID Number'
							write-output ""
							$NewFlexIDNumber = Read-Host "Are you sure, you want to replace the following FLEXID Number ""$($AD_User.employeeID)"" for ""$InputFlexIDNumber"" (Y/N)"
							if (($NewFlexIDNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeID $InputFlexIDNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								write-output "New FlexID Number SET"
								Start-Sleep -Seconds 3
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeID = $InputFlexIDNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($InputFlexIDNumber)"
									write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
									write-output "==============================================================="
									write-output ""
									$AD_User.employeeID = $InputFlexIDNumber
								}
							}
						}
                        7 {
							write-output ""
							$InputEmployeeNumber = Read-Host 'Type the new CLOCK Number'
							write-output ""
							$NewEmployeeNumber = Read-Host "Are you sure, you want to replace the following CLOCK number ""$($AD_User.employeeNumber)"" for ""$InputEmployeeNumber"" (Y/N)"
							if (($NewEmployeeNumber -eq "y"))	{
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -employeeNumber $InputEmployeeNumber -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								write-output "New Employee Number SET"
								Start-Sleep -Seconds 3
								write-output ""
								$ChangeYesNo = Read-Host 'Do you still want to correct anything else on this user? (Y/N)'
								write-output ""
								If ($ChangeYesNo -eq "Y") {
									$AD_User.employeeNumber = $InputEmployeeNumber
									Edit_AD_User
								}
								Else {
									Clear-Host
									write-output ""
									write-output "=====================EDITED AD User===================================="
									write-output "LASTNAME: $($AD_USER.sn)"
									write-output "FIRST NAME: $($AD_User.givenName)"
									write-output "DEPARTMENT: $($AD_User.department)"
									write-output "TITLE: $($AD_User.title)"
									write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
									write-output "FLEX ID: $($AD_User.employeeID)"
									write-output "CLOCK NUMBER: $($InputEmployeeNumber)"
									write-output "==============================================================="
									write-output ""
									$AD_User.employeeNumber = $InputEmployeeNumber
								}
							}
						}
                        8 { break userloop }
                    }
	} Until($AD_User_Change -eq "0")
	If ($i -eq $($Search.count))
	{
		"No other user Found in AD for $($User.SN)"
		Start-Sleep 5
	}
} #end function Edit_AD_User

$File = Import-Csv "c:\scripts\ad\temp\file.csv" -header sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
$Ad = Get-ADUser -filter * -Properties sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber  -SearchBase "OU=ou,DC=domain,DC=com" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Foreach ($User in $FILE){
	$i = 0
	[array]$Search = $Ad | ? {$_.sn -eq $User.sn}
	If ($Search){
		:userloop Foreach ($AD_User in $Search) {
			$i++
			Clear-Host
			write-output "=====================CSV User===================================="
			write-output "LASTNAME: $($User.sn)"
			write-output "FIRST NAME: $($User.givenName)"
			write-output "DEPARTMENT: $($User.department)"
			write-output "TITLE: $($User.title)"
			write-output "TELEPHONE NUMBER: $($User.telephoneNumber)"
			write-output "FLEX ID: $($User.employeeID)"
			write-output "CLOCK NUMBER: $($User.employeeNumber)"
			write-output "==============================================================="
			write-output ""
			write-output "=====================AD User===================================="
			write-output "LASTNAME: $($AD_User.sn)"
			write-output "FIRST NAME: $($AD_User.givenName)"
			write-output "DEPARTMENT: $($AD_User.department)"
			write-output "TITLE: $($AD_User.title)"
			write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
			write-output "FLEX ID: $($AD_User.employeeID)"
			write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
			write-output "==============================================================="
			write-output ""
			Edit_AD_User			
		}
	}
	Else {"No user Found in AD for $($User.SN)"}
}

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
@Footech Love youuuuuu. :) Almost, almost done...

Scenario 1: Reads CSV and AD and finds a perfect match and if all info is good, choose 8, CSV jumps to next record and AD queries for the next possible match. PERFECT

Scenario 2: CSV jumps to next record and AD queries for the next possible match and if incorrect math, 0 is pressed and finds the right match. PERFECT

Scenario 3. Reads CSV and AD and finds a perfect match and NOT all info is good, and info gets corrected on AD, PERFECT

This one is not working:
Scenario 4: Reads CSV, AD and finds a perfect match and if NOT all info is good, info gets corrected on AD but ONCE all info is corrected CSV needs to jumps to next CSV user and AD query stars again.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
I'm not sure what the difference between scenarios 3 and 4 is.  Do you mean that once scenario 3 is complete you're having trouble getting to the next user?

I haven't looked at all the code you have for making corrections, but I'm thinking you'll always end at the prompt for what info you want to change, whether you've made corrections or not, pressing 8 will advance to the next user in the CSV.
0
 

Author Comment

by:namerg
Comment Utility
Ay dios mio...yes sorry...I am getting overwhelmed.

Never mind...I think it is working :) Sorry sorry
0
 

Author Comment

by:namerg
Comment Utility
PERFECTO
Love You, love youuuu @footech.  You da man.. and Subsun too.

Experts-Exchange should put emoticons like chocolates, kisses, aces or something like that to cheer you.

I will do a full test tomorrow.

I am exhausted. I do have another question which I am going to post in a bit.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
I would take out the question "Do you still want to correct anything else on this user? (Y/N):"  It's redundant.  If you don't want to edit any further information, a response of "None - the info is correct" takes you to the next user.

Simplify a choice like number 2 and make it like the following (apply the same concept to the other choices as well):
				2 {
							$flag2 = 2
							write-output ""
							$InputFirstName = Read-Host 'Type the new FIRST Name'
							write-output ""
							$NewFirstName = Read-Host "Are you sure, you want to replace the following FIRSTNAME ""$($AD_User.givenName)"" FOR ""$InputFirstName"" (Y/N)"
							if (($NewFirstName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -givenName $InputFirstName -DisplayName $($AD_USER.sn+", "+$InputFirstName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $AD_USER.sn+", "+$InputFirstName
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								$AD_User.givenName = $InputFirstName
								Clear-Host
								write-output ""
								write-output "=====================EDITED AD User===================================="
								write-output "LASTNAME: $($AD_USER.sn)"
								write-output "FIRST NAME: $($InputFirstName)"
								write-output "DEPARTMENT: $($AD_User.department)"
								write-output "TITLE: $($AD_User.title)"
								write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
								write-output "FLEX ID: $($AD_User.employeeID)"
								write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
								write-output "==============================================================="
								write-output ""
							}  
						}

Open in new window

0
 

Author Comment

by:namerg
Comment Utility
Hmm, you are right, i did change it for option 2.
But,

Ran the script,
Option1, lastname changed in AD, PERFECT
Option2. firstname changed in AD but lastname went back to the original.. hmmm

And, if I leave the way it was, it does not change it back to the original. Do you want to see code ?
0
 
LVL 39

Accepted Solution

by:
footech earned 375 total points
Comment Utility
Here, I've simplified two of the options.  The information displayed by the "Edited AD User" block is always up-to-date, that's the reason for setting the $AD_User.<whatever property> to the input.  I never saw a case where it was displaying the original information with the previous code, but I wasn't looking for that specifically, so I suppose it's possible I missed it.  With these two I'm always seeing the updated information.
			2 {
							write-output ""
							$InputFirstName = Read-Host 'Type the new FIRST Name'
							write-output ""
							$NewFirstName = Read-Host "Are you sure, you want to replace the following FIRSTNAME ""$($AD_User.givenName)"" FOR ""$InputFirstName"" (Y/N)"
							if (($NewFirstName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -givenName $InputFirstName -DisplayName $($AD_USER.sn+", "+$InputFirstName) -ErrorAction SilentlyContinue -ErrorVariable Err1
								Start-Sleep -Seconds 3
								$NewName = $AD_USER.sn+", "+$InputFirstName
								Get-ADUser $AD_User.sAMAccountName  | Rename-ADObject -newname $NewName
								$AD_User.givenName = $InputFirstName
								Clear-Host
								write-output ""
								write-output "=====================EDITED AD User===================================="
								write-output "LASTNAME: $($AD_USER.sn)"
								write-output "FIRST NAME: $($AD_User.givenName)"
								write-output "DEPARTMENT: $($AD_User.department)"
								write-output "TITLE: $($AD_User.title)"
								write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
								write-output "FLEX ID: $($AD_User.employeeID)"
								write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
								write-output "==============================================================="
								write-output ""
							}  
						}
                        3 {
							write-output ""
							$InputDepartmentName = Read-Host 'Type the new DEPARTMENT Name'
							write-output ""
							$NewDepartmentName = Read-Host "Are you sure, you want to replace the following DEPARTMENT ""$($AD_User.department)"" for ""$InputDepartmentName"" (Y/N)"
							if (($NewDepartmentName -eq "y")) {
								Get-ADUser $AD_User.sAMAccountName  | Set-ADUser -department $InputDepartmentName -ErrorAction SilentlyContinue -ErrorVariable Err1
								$AD_User.department = $InputDepartmentName
								Clear-Host
								write-output ""
								write-output "=====================EDITED AD User===================================="
								write-output "LASTNAME: $($AD_USER.sn)"
								write-output "FIRST NAME: $($AD_User.givenName)"
								write-output "DEPARTMENT: $($AD_User.department)"
								write-output "TITLE: $($AD_User.title)"
								write-output "TELEPHONE NUMBER: $($AD_User.telephoneNumber)"
								write-output "FLEX ID: $($AD_User.employeeID)"
								write-output "CLOCK NUMBER: $($AD_User.employeeNumber)"
								write-output "==============================================================="
								write-output ""
							}
						}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now