Advertisement
Guest User

CopyUser

a guest
Mar 28th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $creds = Get-Credential -Message "Enter Domain Admin Creds"
  2. $UsernameToCopy = Read-Host -Prompt "Username of person to copy"
  3. $Firstname = Read-Host -Prompt "First name"
  4. $LastName = Read-Host -Prompt "Last name"
  5. $Username = $firstname.SubString(0,1) + $LastName
  6. $name = $Firstname + " " + $LastName
  7. $EmailAddress = $Username + "@mycompany.com"
  8. $ExchangeServer = "Exchange2010"
  9.  
  10. #password confirmation/set
  11. $password = Read-Host -Prompt "Password" -AsSecureString
  12. $passwordconfirm = Read-Host -Prompt "Confirm Password" -AsSecureString
  13. $i = 0
  14. $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
  15. $pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwordconfirm))
  16. While ($i -eq 0) {
  17. if ($pwd1_text -eq $pwd2_text) {$i = 1 }
  18. else
  19. {
  20. write-host "Passwords do not match, please try again" -BackgroundColor White -ForegroundColor Red
  21. $password = Read-Host -Prompt "Password" -AsSecureString
  22. $passwordconfirm = Read-Host -Prompt "Confirm Password" -AsSecureString
  23. $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
  24. $pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwordconfirm))
  25. }}
  26. #end password stuff
  27.  
  28. #use username to copy to get information about them
  29. $OldUsername = Get-ADUser -Identity $UsernameToCopy -Properties Memberof,l
  30.  
  31. #This grabs the parent by querying LDAP since the parent folder in AD isnt brought over as a property.
  32. $DN = $OldUsername.distinguishedName
  33. $OldUser = [ADSI]"LDAP://$DN"
  34. $Parent = $OldUser.Parent
  35.  
  36.  
  37. $UserSettings = @{
  38. 'SamAccountName'= $username;
  39. 'name'= $name;
  40. 'ChangePasswordAtLogon'= $true;
  41. 'Credential' = $Creds;
  42. 'AccountPassword'= $password;
  43. 'userPrincipalName'= $EmailAddress;
  44. 'EmailAddress'= $EmailAddress;
  45. 'Path'=$Parent.Substring(7);
  46. 'enabled'= $True;
  47. 'GivenName'= $Firstname;
  48. 'Surname'= $LastName;
  49. }
  50.  
  51. New-ADUser @UserSettings -Instance $OldUsername -verbose
  52.  
  53. $OldUsername.MemberOf | Add-ADGroupMember -Members $Username -Credential $creds
  54.  
  55.  
  56. Invoke-Command -ComputerName $ExchangeServer -Credential $creds {"C:\Scripts\CopyuserRemoteMailbox(WIP).ps1 -username $using:Username"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement