Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $creds = Get-Credential -Message "Enter Domain Admin Creds"
- $UsernameToCopy = Read-Host -Prompt "Username of person to copy"
- $Firstname = Read-Host -Prompt "First name"
- $LastName = Read-Host -Prompt "Last name"
- $Username = $firstname.SubString(0,1) + $LastName
- $name = $Firstname + " " + $LastName
- $EmailAddress = $Username + "@mycompany.com"
- $ExchangeServer = "Exchange2010"
- #password confirmation/set
- $password = Read-Host -Prompt "Password" -AsSecureString
- $passwordconfirm = Read-Host -Prompt "Confirm Password" -AsSecureString
- $i = 0
- $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
- $pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwordconfirm))
- While ($i -eq 0) {
- if ($pwd1_text -eq $pwd2_text) {$i = 1 }
- else
- {
- write-host "Passwords do not match, please try again" -BackgroundColor White -ForegroundColor Red
- $password = Read-Host -Prompt "Password" -AsSecureString
- $passwordconfirm = Read-Host -Prompt "Confirm Password" -AsSecureString
- $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
- $pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwordconfirm))
- }}
- #end password stuff
- #use username to copy to get information about them
- $OldUsername = Get-ADUser -Identity $UsernameToCopy -Properties Memberof,l
- #This grabs the parent by querying LDAP since the parent folder in AD isnt brought over as a property.
- $DN = $OldUsername.distinguishedName
- $OldUser = [ADSI]"LDAP://$DN"
- $Parent = $OldUser.Parent
- $UserSettings = @{
- 'SamAccountName'= $username;
- 'name'= $name;
- 'ChangePasswordAtLogon'= $true;
- 'Credential' = $Creds;
- 'AccountPassword'= $password;
- 'userPrincipalName'= $EmailAddress;
- 'EmailAddress'= $EmailAddress;
- 'Path'=$Parent.Substring(7);
- 'enabled'= $True;
- 'GivenName'= $Firstname;
- 'Surname'= $LastName;
- }
- New-ADUser @UserSettings -Instance $OldUsername -verbose
- $OldUsername.MemberOf | Add-ADGroupMember -Members $Username -Credential $creds
- Invoke-Command -ComputerName $ExchangeServer -Credential $creds {"C:\Scripts\CopyuserRemoteMailbox(WIP).ps1 -username $using:Username"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement