Advertisement
Guest User

Untitled

a guest
Nov 11th, 2017
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. #Import needed module.
  2. Import-Module ActiveDirectory
  3.  
  4. #Prompt for needed information to use as variables below
  5. $fullname = Read-Host "Enter Full Name"
  6. $first = Read-Host "First name"
  7. $last = Read-Host "Last name"
  8. $user = Read-Host "Username"
  9. $title = Read-Host "Title"
  10. Get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" |
  11. Select-Object -Property Name
  12. $department = Read-Host "Enter department from above list"
  13. $manager = Read-Host "Manager userame"
  14. $srcuser = Read-Host "Username to copy"
  15. Get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" |
  16. Select-Object -Property Name, DistinguishedName |
  17. Format-Table -Auto
  18. $OU = Read-Host "Select OU from above list"
  19.  
  20. #Create a new user with the provided information and some static information
  21. New-ADUser -Name "$fullname" -GivenName "$first" -Surname "$last" -DisplayName "$first $last" -Description "$title" -EmailAddress "$first.$last@bnztest.com" -SamAccountName "$user" -UserPrincipalName "$user@bnztest.com" -Manager "$manager" -Title "$title" -AccountPassword (Read-Host -AsSecureString "Please enter the desired password") -Enabled $true -Path $OU
  22.  
  23. #Add multiple ProxyAddresses if needed
  24. Set-ADUser "$user" -Add @{ProxyAddresses="smtp:$first.$last@bnztest.com"}
  25.  
  26. #Copy group membership of the source user above
  27. Get-ADUser -Identity "$srcuser" -Properties memberof |
  28. Select-Object -ExpandProperty memberof |
  29. Add-ADGroupMember -Members "$user" -PassThru |
  30. Select-Object -Property SamAccountName >$null
  31. Write-Host 'CHECK AD REPLICATION BEFORE CONTINUING!'
  32. pause
  33.  
  34. #Sync user to Office 365 using Dir Sync on a remote server
  35. Import-Module ADSync
  36. Start-ADSyncSyncCycle -PolicyType Initial
  37. Start-Sleep -s 100
  38.  
  39. #License user in Office 365
  40. $AdminName = "admin@testbnz.onmicrosoft.com"
  41. $Pass = Get-Content "C:UsersAdministratorDesktopCreateUserCred.txt" |
  42. ConvertTo-SecureString
  43. $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminName, $Pass
  44. Import-Module MSOnline
  45. Connect-MsolService -Credential $cred
  46. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
  47. Import-PSSession $Session
  48. Start-Sleep -s 15
  49. Set-MsolUser -UserPrincipalName "$user@bnztest.com" -UsageLocation 'US'
  50. Set-MsolUserLicense -UserPrincipalName "$user@bnztest.com" -AddLicenses "TESTBNZ:O365_BUSINESS_PREMIUM"
  51. Start-Sleep 90
  52. Write-Host 'ENSURE THERE ARE NO ERRORS AND THAT THE MAILBOX HAS BEEN CREATED BEFORE CONTINUING!'
  53. pause
  54.  
  55. #Import needed module.
  56. Import-Module ActiveDirectory
  57.  
  58. #Prompt for needed information to use as variables below
  59. $fullname = Read-Host "Enter Full Name"
  60. $first = Read-Host "First name"
  61. $last = Read-Host "Last name"
  62. $user = Read-Host "Username"
  63. $title = Read-Host "Title"
  64. Get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" |
  65. Select-Object -Property Name
  66. $department = Read-Host "Enter department from above list"
  67. $manager = Read-Host "Manager userame"
  68. $srcuser = Read-Host "Username to copy"
  69. Get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" |
  70. Select-Object -Property Name, DistinguishedName |
  71. Format-Table -Auto
  72. $OU = Read-Host "Select OU from above list"
  73.  
  74. #Create a new user with the provided information and some static information
  75. New-ADUser -Name "$fullname" -GivenName "$first" -Surname "$last" -DisplayName "$first $last" -Description "$title" -EmailAddress "$first.$last@bnztest.com" -SamAccountName "$user" -UserPrincipalName "$user@bnztest.com" -Manager "$manager" -Title "$title" -AccountPassword (Read-Host -AsSecureString "Please enter the desired password") -Enabled $true -Path $OU
  76.  
  77. #Add multiple ProxyAddresses if needed
  78. Set-ADUser "$user" -Add @{ProxyAddresses="smtp:$first.$last@bnztest.com"}
  79.  
  80. #Copy group membership of the source user above
  81. Get-ADUser -Identity "$srcuser" -Properties memberof |
  82. Select-Object -ExpandProperty memberof |
  83. Add-ADGroupMember -Members "$user" -PassThru |
  84. Select-Object -Property SamAccountName >$null
  85. Write-Host 'CHECK AD REPLICATION BEFORE CONTINUING!'
  86. pause
  87.  
  88. #Sync user to Office 365 using Dir Sync on a remote server
  89. Import-Module ADSync
  90. Start-ADSyncSyncCycle -PolicyType Initial
  91. Start-Sleep -s 100
  92.  
  93. #License user in Office 365
  94. $AdminName = "admin@testbnz.onmicrosoft.com"
  95. $Pass = Get-Content "C:UsersAdministratorDesktopCreateUserCred.txt" |
  96. ConvertTo-SecureString
  97. $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminName, $Pass
  98. Import-Module MSOnline
  99. Connect-MsolService -Credential $cred
  100. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
  101. Import-PSSession $Session
  102. Start-Sleep -s 15
  103. Set-MsolUser -UserPrincipalName "$user@bnztest.com" -UsageLocation 'US'
  104. Set-MsolUserLicense -UserPrincipalName "$user@bnztest.com" -AddLicenses "TESTBNZ:O365_BUSINESS_PREMIUM"
  105. Start-Sleep 90
  106. Write-Host 'ENSURE THERE ARE NO ERRORS AND THAT THE MAILBOX HAS BEEN CREATED BEFORE CONTINUING!'
  107. pause
  108.  
  109. $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",
  110. "Exits the loop."
  111. $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",
  112. "Allows to add another user."
  113.  
  114. $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  115.  
  116. :OuterLoop do
  117. {
  118. $user = New-Object System.Management.Automation.Host.ChoiceDescription "&User", "User"
  119. $it = New-Object System.Management.Automation.Host.ChoiceDescription "&IT", "IT"
  120. $sales = New-Object System.Management.Automation.Host.ChoiceDescription "&Sales", "Sales"
  121. $OUoptions = [System.Management.Automation.Host.ChoiceDescription[]]($user, $it, $sales)
  122. $OU = $host.ui.PromptForChoice("Which OU", "Which OU", $OUoptions, 0)
  123.  
  124. switch ($OU)
  125. {
  126. 0 {Write-Host "The choise is User."}
  127. 1 {Write-Host "IT"}
  128. 2 {Write-Host "Sales"}
  129. default {Write-Host "The color could not be determined."}
  130. }
  131.  
  132. $result = $host.ui.PromptForChoice("Endless Loop", "Do you want to add another user?", $options, 1)
  133.  
  134. switch ($result)
  135. {
  136. 1 {break OuterLoop}
  137. }
  138. }
  139. while ($y -ne 100)
  140.  
  141. $SearchBase = "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM"
  142. $OUList = Get-ADOrganizationalUnit -SearchBase $SearchBase -Filter * -Properties Name,DistinguishedName | Select-Object -Property Name,DistinguishedName
  143.  
  144. $OU = $OUList | Out-GridView -Title "Select OU and Click OK" -OutputMode Single
  145.  
  146. New-ADUser [...] -OU $OU.DistinguishedName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement