Advertisement
Guest User

EX

a guest
Sep 15th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. param(
  2. [parameter(Mandatory=$false)]
  3. [Switch]
  4. $MasterOnPremise = $false,
  5.  
  6. [parameter(Mandatory=$false,HelpMessage="Please provide the SKU's you want to activate")]
  7. [ValidateSet("K1","K2","E1","E3","A1S","A2S","A3S","A1F","A2F","A3F")]
  8. [ValidateNotNullOrEmpty()]
  9. [array]
  10. $Licenses,
  11.  
  12. [parameter(Mandatory=$false)]
  13. [string]
  14. $LicenseAttribute = "employeeType",
  15.  
  16. [parameter(Mandatory=$false)]
  17. [string]
  18. $AdminUser = "mmjadmin@saxopayments.com",
  19.  
  20. [parameter(Mandatory=$false)]
  21. [string]
  22. $DefaultCountry = "DK"
  23. )
  24. #Define variables
  25. $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
  26. $Logfile = ($PSScriptRoot + "\MSOL-UserActivation.log")
  27. $TimeDate = Get-Date -Format "yyyy-MM-dd-HH:mm"
  28. $SupportedSKUs = @{
  29. "K1" = "DESKLESSPACK"
  30. "K2" = "DESKLESSWOFFPACK"
  31. "E1" = "STANDARDPACK"
  32. "E3" = "ENTERPRISEPACK"
  33. "A1S" = "STANDARDPACK_STUDENT"
  34. "A2S" = "STANDARDWOFFPACKPACK_STUDENT"
  35. "A3S" = "ENTERPRISEPACK_STUDENT"
  36. "A1F" = "STANDARDPACK_FACULTY"
  37. "A2F" = "STANDARDWOFFPACKPACK_FACULTY"
  38. "A3F" = "ENTERPRISEPACK_FACULTY"
  39.  
  40. }
  41.  
  42. ################################################
  43. ##### Functions
  44.  
  45. #Function to Import Modules with error handling
  46. Function Import-MyModule
  47. {
  48. Param([string]$name)
  49. if(-not(Get-Module -name $name))
  50. {
  51. if(Get-Module -ListAvailable $name)
  52. {
  53. Import-Module -Name $name
  54. Write-Host "Imported module $name" -ForegroundColor Yellow
  55. }
  56. else
  57. {
  58. Throw "Module $name is not installed. Exiting..."
  59. }
  60. }
  61. else
  62. {
  63. Write-Host "Module $name is already loaded..." -ForegroundColor Yellow }
  64. }
  65.  
  66. #Function to log to file and write output to host
  67. Function LogWrite
  68. {
  69. Param ([string]$Logstring)
  70. Add-Content $Logfile -value $logstring -ErrorAction Stop
  71. Write-Host $logstring
  72. }
  73. #Function to activate your users based on ad attribute
  74. Function Activate-MsolUsers
  75. {
  76. Param([string]$SKU)
  77.  
  78. begin {
  79. #Set counter to 0
  80. $i = 0
  81. }
  82.  
  83. process {
  84.  
  85. #Catch and log errors
  86. trap {
  87. $ErrorText = $_.ToString()
  88. LogWrite "Error: $_"
  89. $error.Clear()
  90. Continue
  91. }
  92.  
  93. #If the switch -MasterOnPremise has been used, start processing licenses from the AD-attribute
  94.  
  95. if ($MasterOnPremise) {
  96.  
  97. $UsersToActivate = Get-ADUser -filter {$LicenseAttribute -eq $SKU} -Properties $LicenseAttribute -ErrorAction Stop
  98.  
  99. if ($UsersToActivate)
  100. {
  101. $NumUsers = ($UsersToActivate | Measure-Object).Count
  102.  
  103. LogWrite "Info: $NumUsers user(s) to activate with $SKU"
  104. foreach($user in $UsersToActivate) {
  105.  
  106. trap {
  107. $ErrorText = $_.ToString()
  108. LogWrite "Error: $_"
  109. $error.Clear()
  110. Continue
  111. }
  112. $UPN = $user.userprincipalname
  113. $Country = $user.Country
  114. LogWrite "Info: Trying to assign licenses to: $UPN"
  115. if (!($country)) {
  116. $Country = $DefaultCountry }
  117.  
  118. if ((Get-MsolUser -UserPrincipalName $UPN -ErrorAction Stop)) {
  119.  
  120. Set-MsolUser -UserPrincipalName $UPN -UsageLocation $country -Erroraction Stop
  121. Set-MsolUserLicense -UserPrincipalName $UPN -AddLicenses $SKUID -Erroraction Stop
  122. #Verify License Assignment
  123. if (Get-MsolUser -UserPrincipalName $UPN | Where-Object {$_.IsLicensed -eq $true}) {
  124. Set-ADUser $user -Replace @{$LicenseAttribute=$SKU+' - Licensed at ' + $TimeDate}
  125. LogWrite "Info: $upn successfully licensed with $SKU"
  126. $i++;
  127. }
  128. else
  129. {
  130. LogWrite "Error: Failed to license $UPN with $SKU, please do further troubleshooting"
  131.  
  132. }
  133. }
  134. }
  135. }
  136. }
  137. #If no switch has been used, process users and licenses from MSOnline
  138. else {
  139. $UsersToActivate = Get-MsolUser -UnlicensedUsersOnly -All
  140. if ($Userstoactivate)
  141. {
  142. $NumUsers = $UsersToActivate.Count
  143. LogWrite "Info: $NumUsers unlicensed user(s) in tenant: $($SKUID.ToLower().Split(':')[0]).onmicrosoft.com"
  144. foreach ($user in $UsersToActivate) {
  145. trap {
  146. $ErrorText = $_.ToString()
  147. LogWrite "Error: $_"
  148. $error.Clear()
  149. Continue
  150. }
  151.  
  152. $UPN = $user.UserPrincipalName
  153. $ADUser = Get-Aduser -Filter {userPrincipalName -eq $UPN} -Properties $LicenseAttribute -ErrorAction Stop
  154. $Country = $ADUser.Country
  155. if (!($Country)) {
  156. $Country = $DefaultCountry
  157. }
  158. if ($ADUser.$LicenseAttribute -eq $SKU) {
  159. LogWrite "Info: Trying to assign licenses to: $UPN"
  160. Set-MsolUser -UserPrincipalName $UPN -UsageLocation $country -Erroraction Stop
  161. Set-MsolUserLicense -UserPrincipalName $UPN -AddLicenses $SKUID -Erroraction Stop
  162.  
  163. #Verify License Assignment
  164. if (Get-MsolUser -UserPrincipalName $UPN | Where-Object {$_.IsLicensed -eq $true}) {
  165. LogWrite "Info: $upn successfully licensed with $SKU"
  166. $i++;
  167. }
  168. else
  169. {
  170.  
  171. LogWrite "Error: Failed to license $UPN with $SKU, please do further troubleshooting"
  172.  
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179.  
  180. End{
  181. LogWrite "Info: $i user(s) was activated with $license ($SKUID)"
  182. }
  183. }
  184. ################################################
  185. #Import modules required for the script to run
  186. Import-MyModule MsOnline
  187. Import-MyModule ActiveDirectory
  188.  
  189. #Start logging and check logfile access
  190. try
  191. {
  192. LogWrite -Logstring "**************************************************`r`nLicense activation job started at $timedate`r`n**************************************************"
  193. }
  194. catch
  195. {
  196. Throw "You don't have write permissions to $logfile, please start an elevated PowerShell prompt or change NTFS permissions"
  197. }
  198.  
  199. #Connect to Azure Active Directory
  200. try
  201. {
  202. $PasswordFile = ($PSScriptRoot + "\$adminuser.txt")
  203. if (!(Test-Path -Path $passwordfile))
  204. {
  205. Write-Host "You don't have an admin password assigned for $adminuser, please provide the password followed with enter below:" -Foregroundcolor Yellow
  206. Read-Host -assecurestring | convertfrom-securestring | out-file $passwordfile
  207. }
  208. $password = get-content $passwordfile | convertto-securestring -ErrorAction Stop
  209. $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $adminuser,$password -ErrorAction Stop
  210. Connect-MsolService -credential $credentials -ErrorAction Stop
  211. }
  212. catch [system.exception]
  213. {
  214. $err = $_.Exception.Message
  215. LogWrite "Error: Could not Connect to Office 365, please check connection, username and password.`r`nError: $err"
  216. exit
  217. }
  218.  
  219. if (!$licenses) {
  220. LogWrite "Error: No licenses specified, please specify a supported license`r`nInfo: Supported licenses are: K1,K2,E1,E3,A1S,A2S,A3S,A1F,A2F,A3F!"
  221. }
  222. else
  223. {
  224.  
  225. #Start processing license assignment
  226. foreach($license in $Licenses) {
  227.  
  228. $SKUID = (Get-MsolAccountSku | Where-Object {$_.AccountSkuId -like "*:$($SupportedSKUs.Get_Item($license))"}).AccountSkuId
  229.  
  230. if ($SKUID)
  231. {
  232. Activate-MsolUsers -SKU $license
  233. }
  234. else
  235. {
  236. LogWrite "Error: No $license licenses in your tenant!"
  237. }
  238. }
  239. }
  240. LogWrite -Logstring "**************************************************"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement