Solved

How to jump to the next an Active Directory User within a ForEach statement ?

Posted on 2013-01-02
32
533 Views
Last Modified: 2013-01-03
Hello,

I have a foreach statement that queries/reads AD users and prompts some questions while querying the AD. If there is a question that answers No, it will have to jump to the next user.

code:
$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) {
      if ($file[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
               #Ask Questions
              $RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'

#I AM STUCK HERE...
if $RightWrong -eq "N" {

     #It needs to jump to the next AD user and Ask questions
}
}

Thanks for your help,
0
Comment
Question by:namerg
  • 18
  • 14
32 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 38737589
Try..
if ($RightWrong -eq "N") {break}

Open in new window

0
 

Author Comment

by:namerg
ID: 38737598
Hey Guru Subsun,

But will it not break the "for" statement ?
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 38737655
Yes it will.. If you are looking to go to next value in loop itself then try..
if ($RightWrong -eq "N") {$foreach.moveNext() | Out-Null}

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:namerg
ID: 38737674
hmm, it did not move to the next AD user, it kept the same value or AD User found on the if statement.  
if ($file[0].sn -eq $AD_User.sn) {....}
0
 

Author Comment

by:namerg
ID: 38737707
By the way this is all related to my previous questions, it is like a mini application but with poweshell.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38737728
I think you have to place the second IF statement outside the first one.. Try this..
foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
      #Write-Host "BINGO FOUND IT"
      #Ask Questions
      $RightWrong = Read-Host 'NOTE: Is this the right Employee, You are working on? (Y/N)'
      }
      if ($RightWrong -eq "N") {[void]$foreach.moveNext()}
}

Open in new window

0
 

Author Comment

by:namerg
ID: 38737766
Same thing,

Result:
======== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ========
LASTNAME: Silveira
FIRST NAME: Rodrigo
DEPARTMENT: Information Services
TITLE: Sr System/NetworkAdministrator
TELEPHONE NUMBER: 303-333-3333
EMPLOYEE ID:
EMPLOYEE NUMBER:
=================================================

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

#The following comes after the following code:
#if ($RightWrong -eq "N") {
#                  #break
#                  [void]$foreach.moveNext()
#                  Write-Host "LASTNAME: $($AD_USER.sn)"
#                  Write-Host "FIRST NAME: $($AD_USER.givenName)"
                  
            }
#Same user as == FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY ======== section

LASTNAME: Silveira
FIRST NAME: Rodrigo

NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the File? (Y/N): PS C:\scripts\AD>
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38737776
BTB there will be only one user which match condition $file[0].sn -eq $AD_User.sn right? It yes.. Ideally the script should exit if you input "N". Please correct me if I am wrong in my assumption..
0
 

Author Comment

by:namerg
ID: 38737788
Correct. The result of that of IF is, 1, True.
But think about that can be more than one User with a lastname Silveira.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38737800
[void]$foreach.moveNext()
                 Write-Host "LASTNAME: $($AD_USER.sn)"
                  Write-Host "FIRST NAME: $($AD_USER.givenName)"

Write-Host here will not write the next user, if will just write the same user which is in pipeline..

It would be better if you can post the complete code..
0
 

Author Comment

by:namerg
ID: 38737849
Here it goes:

*******************************************************

Clear-Host
$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=colo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Write-Host "Querying   Employee from   file against Active Directory"
Write-Host ""
Write-Host "++ FOUND THE FOLLOWING EMPLOYEE IN   FILE +++++"
#Write-Host ""
Write-Host "LASTNAME: $($ [0].sn)"
Write-Host "FIRST NAME: $($ [0].givenName)"
Write-Host "DEPARTMENT: $($ [0].department)"
Write-Host "TITLE: $($ [0].title)"
Write-Host "TELEPHONE NUMBER: $($ [0].telephoneNumber)"
Write-Host "FLEX ID: $($ [0].employeeID)"
Write-Host "CLOCK NUMBER: $($ [0].employeeNumber)"
#Write-Host ""
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++"
#Write-Host "Waiting for 5 seconds"
Start-Sleep -Seconds 5
foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
            $Flag LastName = 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 "EMPLOYEE ID: $($AD_USER.employeeID)"
            Write-Host "EMPLOYEE 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") {
            #I AM STUCK HERE
                  #break
                  [void]$foreach.moveNext()
                  Write-Host "LASTNAME: $($AD_USER.sn)"
                  Write-Host "FIRST NAME: $($AD_USER.givenName)"
                  
            }
            $YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the   File? (Y/N)'
            Write-Host ""
            #Write-Host "YesNo Value: $($YesNo)"
            if (($RightWrong -eq "Y") -and ($YesNo -eq "N"))
               {
                        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'
                  }
            switch ($AD_User_Change) {
             1 {
                  Write-Host "1"
                  $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"
                        Start-Sleep -Seconds 30
                    }
              }
             2 {Write-Host "2"}
             3 {Write-Host "3"}
             4 {Write-Host "4"}
             5 {Write-Host "5"}
             6 {Write-Host "6"}
             7 {Write-Host "7"}
            }
            Start-Sleep -Seconds 30
      }

}
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38737980
Can you try this and see if it gives expected result?
Clear-Host
$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=colo,DC=pvt" | Select-Object  sAMAccountName,sn,givenName,department,title,telephoneNumber,employeeID,employeeNumber
Write-Host "Querying   Employee from   file against Active Directory"
Write-Host ""
Write-Host "++ FOUND THE FOLLOWING EMPLOYEE IN   FILE +++++"
#Write-Host ""
Write-Host "LASTNAME: $($ [0].sn)"
Write-Host "FIRST NAME: $($ [0].givenName)"
Write-Host "DEPARTMENT: $($ [0].department)"
Write-Host "TITLE: $($ [0].title)"
Write-Host "TELEPHONE NUMBER: $($ [0].telephoneNumber)"
Write-Host "FLEX ID: $($ [0].employeeID)"
Write-Host "CLOCK NUMBER: $($ [0].employeeNumber)"
#Write-Host ""
Write-Host "++++++++++++++++++++++++++++++++++++++++++++++++++++++"
#Write-Host "Waiting for 5 seconds"
Start-Sleep -Seconds 5
foreach ($AD_User in $ad) {
      if ($file[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
            $FlagLastName = 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 "EMPLOYEE ID: $($AD_USER.employeeID)"
            Write-Host "EMPLOYEE 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") {
                  [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? (Y/N)'
           Write-Host ""
         #Write-Host "YesNo Value: $($YesNo)"
            
	  If ($YesNo -eq "N")
               {
               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'
                }
            switch ($AD_User_Change) {
             1 {
                  Write-Host "1"
                  $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"
                        Start-Sleep -Seconds 30
                    }
              }
             2 {Write-Host "2"}
             3 {Write-Host "3"}
             4 {Write-Host "4"}
             5 {Write-Host "5"}
             6 {Write-Host "6"}
             7 {Write-Host "7"}
            }
            Start-Sleep -Seconds 30
      }

} 

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38738008
Add $file[0].sn -eq $AD_User.sn in third If statement..

If ($RightWrong -eq "Y" -and $file[0].sn -eq $AD_User.sn)
0
 

Author Comment

by:namerg
ID: 38738250
Hmm, I have to change little bit the logic,

      Elseif ($RightWrong -eq "N")
                        {
                        [void]$foreach.moveNext()
                        Write-Host "Moving to the next user"
                        Start-Sleep -Seconds 30
                        }
                  else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }
      } #Closes if ($file[0].sn -eq $AD_User.sn)
}closes For statement

But I get the following error:

Elseif : The term 'Elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
that the path is correct and try again.
At C:\scripts\AD\test.ps1:90 char:4
+             Elseif ($RightWrong -eq "N")
+             ~~~~~~
    + CategoryInfo          : ObjectNotFound: (Elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


                [void]$foreach.moveNext()
                Write-Host "Moving to the next user"
                Start-Sleep -Seconds 30

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
the path is correct and try again.
At C:\scripts\AD\test.ps1:96 char:4
+             else
+             ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


                Write-Host "USER DOES NOT EXIST"
0
 

Author Comment

by:namerg
ID: 38738431
I think is a bug on PowerShell v3..hmmm....
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38739730
Elseif ($RightWrong -eq "N")
                        {
                        [void]$foreach.moveNext()
                        Write-Host "Moving to the next user"
                        Start-Sleep -Seconds 30
                        }
                  else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }

Elseif here doesn’t make any sense to me..

You can try something like..
$adsearch = $ad | Where {$file[0].sn -eq $_.sn}
If ($adsearch -eq $null)
{Write-Host "USER DOES NOT EXIST"}
Else{"Start your script"}

Open in new window


Did you get any error for the code which I suggested? or is it not working as expected?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38739746
And regarding PS 3.0 if bug, If I am not wrong It only fails if the closing curly brace comes in same line with if keyword..
0
 

Author Comment

by:namerg
ID: 38740261
Hi Subsun I just got to the office, let me try.
0
 

Author Comment

by:namerg
ID: 38740360
Subsun, I get the following error:

Elseif : The term 'Elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, veri
that the path is correct and try again.

At C:\scripts\AD\test.ps1:88 char:4
+             Elseif ($RightWrong -eq "N")
+             ~~~~~~
    + CategoryInfo          : ObjectNotFound: (Elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
                Write-Host "Moving to the next user"
                #[void]$foreach.moveNext()
                #Write-Host "Moving to the next user"
                #Start-Sleep -Seconds 30
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t
the path is correct and try again.
At C:\scripts\AD\test.ps1:95 char:4
+             else
+             ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


                Write-Host "USER DOES NOT EXIST"

Code:

$YesNo = Read-Host 'NOTE: Is the EMPLOYEE Information FOUND In ACTIVE DIRECTORY CORRECT AGAINST The EMPLOYEE Information FOUND In the File? (Y/N)'
            Write-Host ""
            #Write-Host "YesNo Value: $($YesNo)"
            #if (($RightWrong -eq "Y") -and ($YesNo -eq "N"))
            if ($RightWrong -eq "Y")
             {
                        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'
                  
                  switch ($AD_User_Change)
                  {
                        1 {
                              Write-Host "1"
                              $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"
                                    Start-Sleep -Seconds 30
                              }
                        }

                  }
             }
             Start-Sleep -Seconds 30      
             Elseif ($RightWrong -eq "N")
                      {
                        Write-Host "Moving to the next user"
                        #[void]$foreach.moveNext()
                        #Write-Host "Moving to the next user"
                        #Start-Sleep -Seconds 30
                        }
             else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38740486
Can you try starting Curly Brace in same line with IF statement?
Elseif ($RightWrong -eq "N") {
0
 

Author Comment

by:namerg
ID: 38740553
:( same thing.
0
 

Author Comment

by:namerg
ID: 38740754
I think I am going to give up for this part using powershell. :( I will do this first part of the project in C# then the second part in powershell.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38740805
I will check in PS 3.0 and let you know..
0
 

Author Comment

by:namerg
ID: 38740814
ok thanks a lot.
0
 

Author Comment

by:namerg
ID: 38741003
Subsun,

Should I use powershell v2 ?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38741022
I Actually tried Elseif in PS 3.0 and it's working for me.. I will take a close look at your code as soon as I get some time..:-)
0
 

Author Comment

by:namerg
ID: 38741046
hmm ok... I will paste the code one more time.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38741069
it's fine, i can take it from old post..
0
 

Author Comment

by:namerg
ID: 38741093
Here it goes:

****************************************

Clear-Host
$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
Write-Host "Querying COMPANY Employee from FILE file against Active Directory"
Write-Host ""
Write-Host "++ FOUND THE FOLLOWING EMPLOYEE IN FILE FILE +++++++++++++++"
#Write-Host ""
Write-Host "LASTNAME: $($FILE[0].sn)"
Write-Host "FIRST NAME: $($FILE[0].givenName)"
Write-Host "DEPARTMENT: $($FILE[0].department)"
Write-Host "TITLE: $($FILE[0].title)"
Write-Host "TELEPHONE NUMBER: $($FILE[0].telephoneNumber)"
Write-Host "FLEX ID: $($FILE[0].employeeID)"
Write-Host "CLOCK NUMBER: $($FILE[0].employeeNumber)"
#Write-Host ""
Write-Host"+++++++++++++++++++++++++++++++++++++++++"
#Write-Host "Waiting for 5 seconds"
Start-Sleep -Seconds 5
foreach ($AD_User in $ad) {
      if ($FILE[0].sn -eq $AD_User.sn)
      {
            #Write-Host "BINGO FOUND IT"
            $FlagFILELastName = 1
            Write-Host ""
            Write-Host "==== FOUND THE FOLLOWING EMPLOYEE IN ACTIVE DIRECTORY =="
            #Write-Host ""
            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 "EMPLOYEE ID: $($AD_USER.employeeID)"
            Write-Host "EMPLOYEE NUMBER: $($AD_USER.employeeNumber)"
            #Write-Host ""
            Write-Host "==============================================================="
            Write-Host ""
            #$RightWrong = Read-Host 'NOTE: Is this the right COMPANY Employee, You are working on? (Y/N)'
            Write-Host ""
            #Write-Host "RightWrong Value: $($RightWrong)"
            $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 ($RightWrong -eq "Y") {
                        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'
                  
                  switch ($AD_User_Change)
                  {
                        1 {
                              Write-Host "1"
                              $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"
                                    Start-Sleep -Seconds 30
                              }
                        }
                        2 {Write-Host "2"}
                        3 {Write-Host "3"}
                        4 {Write-Host "4"}
                        5 {Write-Host "5"}
                        6 {Write-Host "6"}
                        7 {Write-Host "7"}
                  }
             }
             Start-Sleep -Seconds 30      
             Elseif ($RightWrong -eq "N")
                      {
                        Write-Host "Moving to the next user"
                        #[void]$foreach.moveNext()
                        #Write-Host "Moving to the next user"
                        #Start-Sleep -Seconds 30
                        }
             else
                        {
                        Write-Host "USER DOES NOT EXIST"
                        }
      }
}
*********************************************************************
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38741100
Ok Remove Start-Sleep -Seconds 30 it breaks the if statement flow..
0
 

Author Comment

by:namerg
ID: 38741117
^&%$# it worked... PERFECT

But I will try something else. I have a meeting in 5 min and I will get back to you.
0
 

Author Comment

by:namerg
ID: 38742098
Subsun, you are my hero :)
I need to implement it for the other user properties as you saw in the code. If i get stuck more question on the way.

Thanks for your help, G
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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