Advertisement
Guest User

Untitled

a guest
May 24th, 2018
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.26 KB | None | 0 0
  1. # Credits
  2. # http://powershell.com/cs/media/p/10883.aspx | Select-TextItem Function
  3. # http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx | Get-Filename Function
  4.  
  5.  
  6. function Select-TextItem {
  7.  
  8. PARAM(
  9. [Parameter(Mandatory=$true)]
  10. $options,
  11. $displayProperty
  12. )
  13.  
  14. [int]$optionPrefix = 1
  15.  
  16. # Create menu list
  17.  
  18. foreach ($option in $options) {
  19.  
  20. if ($displayProperty -eq $null) {
  21.  
  22. Write-Host ("{0,3}: {1}" -f $optionPrefix,$option)
  23.  
  24. } else {
  25.  
  26. Write-Host ("{0,3}: {1}" -f $optionPrefix,$option.$displayProperty)
  27. }
  28.  
  29. $optionPrefix++
  30.  
  31. }
  32.  
  33. Write-Host ("{0,3}: {1}" -f 0,"To cancel")
  34.  
  35. [int]$response = Read-Host "Enter Selection"
  36.  
  37. $val = $null
  38.  
  39. if ($response -gt 0 -and $response -le $options.Count) {
  40.  
  41. $val = $options[$response-1]
  42.  
  43. }
  44.  
  45. return $val
  46.  
  47. }
  48.  
  49. Function Get-FileName($initialDirectory,$FileTitle) {
  50.  
  51. [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
  52.  
  53. $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  54. $OpenFileDialog.ShowHelp = $True
  55. $OpenFileDialog.Title = $FileTitle
  56. $OpenFileDialog.initialDirectory = $initialDirectory
  57. $OpenFileDialog.filter = "CSV Files (*.csv)| *.csv"
  58. $OpenFileDialog.ShowDialog() | Out-Null
  59. $OpenFileDialog.filename
  60.  
  61. } #end function Get-FileName
  62.  
  63. #===========================================================================================================================================
  64. #
  65. # Connect to Office 365
  66. #
  67. #===========================================================================================================================================
  68.  
  69. Clear
  70.  
  71. Do {
  72.  
  73. Do {
  74.  
  75. If($CloudUsername -eq $Null -or $ConvertCloudPassword -eq $Null) {
  76.  
  77. Write-Host @"
  78. Welcome to the Office 365 License Assignment Tool.
  79.  
  80. Before we get started, please enter your Office 365 Administrative username & password.
  81.  
  82. "@
  83.  
  84. $CloudUsername = Read-Host "Enter your Cloud Username"
  85. $CloudPassword = Read-Host -AsSecureString "Enter your Cloud Password"
  86. $ConvertCloudPassword = ConvertFrom-SecureString($CloudPassword)
  87.  
  88. } Else {
  89.  
  90. $CloudPassword = ConvertTo-SecureString($ConvertCloudPassword)
  91.  
  92. $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $CloudUsername, $CloudPassword
  93.  
  94. $Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
  95.  
  96. Import-PSSession $Session
  97.  
  98. Connect-MsolService –Credential $cred
  99.  
  100. }
  101.  
  102. } While ($Cred -eq $Null)
  103.  
  104. $FindPSSession = Get-PSSession
  105.  
  106. } While ($FindPSSession -eq $Null)
  107.  
  108. #===========================================================================================================================================
  109. #
  110. # Choose Type of License Assignment
  111. #
  112. #===========================================================================================================================================
  113.  
  114. Clear
  115.  
  116. Do {
  117.  
  118. If ( $Type -eq $Null ) {
  119.  
  120. $MenuTitle = "Type of License Assignment"
  121. $MenuMessage = "Choose what type of license assignment you would like to perform."
  122. $Single = New-Object System.Management.Automation.Host.ChoiceDescription "&Single",""
  123. $Bulk = New-Object System.Management.Automation.Host.ChoiceDescription "&Bulk",""
  124.  
  125. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Single,$Bulk)
  126. $MenuChoice = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  127.  
  128. Switch($MenuChoice) {
  129.  
  130. 0 { $Type = "Single" }
  131.  
  132. 1 { $Type = "Bulk" }
  133.  
  134. }
  135.  
  136. }
  137.  
  138. } While($Type -eq $Null)
  139.  
  140. #===========================================================================================================================================
  141. #
  142. # License Assignment
  143. #
  144. #===========================================================================================================================================
  145.  
  146. $TenantName = (Get-MsolCompanyInformation).DisplayName
  147.  
  148. Do {
  149.  
  150. Clear
  151.  
  152. #$Type = $Null
  153. #$UPN = $Null
  154. #$UserList = $Null
  155.  
  156. Switch($Type) {
  157.  
  158. Single {
  159.  
  160. Do {
  161.  
  162. If($UPN -eq $Null -or $UPN -eq "") {
  163.  
  164. Clear
  165.  
  166. If($UPN -eq "") { Write-Host -ForegroundColor Yellow "A UserPrincipalName is required for this script to continue."; write-Host "" }
  167.  
  168. $UPN = Read-Host "Enter the UserPrincipalName to assign an Office 365 license(s) to"
  169.  
  170. }
  171.  
  172. } While ($UPN -eq "")
  173.  
  174. Do {
  175.  
  176. Clear
  177.  
  178. Write-Host "You are about to assign Office 365 license(s) to $UPN"
  179.  
  180. $MenuTitle = ""
  181. $MenuMessage = "Is the above UserPrincipalName correct?"
  182. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  183. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  184.  
  185. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  186. $MenuChoice = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  187.  
  188. Switch($MenuChoice) {
  189.  
  190. 0 {
  191.  
  192. $UPNCheck = Get-MsolUser -UserPrincipalName $UPN
  193.  
  194. If($UPNCheck.UserPrincipalName -ne $UPN) { Clear; Write-Host -ForegroundColor Red "The UserPrincipalName " -NoNewLine; Write-Host -ForegroundColor Gray $UPN -NoNewLine; Write-Host -ForegroundColor Red " you entered could not be located in the " -NoNewline; Write-Host -ForeGroundColor Gray $TenantName -NoNewline; Write-Host -ForegroundColor Red " Office 365 tenant."; $UPN = Read-Host "Please enter the UserPrincipalName again"; $MenuChoice = 1 }
  195.  
  196. }
  197.  
  198. 1 {
  199.  
  200. Clear; $UPN = Read-Host "Enter the UserPrincipalName to assign an Office 365 license(s) to"
  201.  
  202. }
  203.  
  204. }
  205. } While ($MenuChoice -ne 0)
  206.  
  207. }
  208.  
  209. Bulk {
  210.  
  211. Do {
  212.  
  213. If($UserList -eq $Null -or $UserList -eq "") {
  214.  
  215. $UserList = Get-FileName -initialDirectory "C:\" -FileTitle "Choose a UserList.csv to bulk assign licenses."
  216.  
  217. }
  218.  
  219. } While ($UserList -eq "")
  220.  
  221. Do {
  222.  
  223. Clear
  224.  
  225. Write-Host "You are about to assign Office 365 license(s) to a list of users: $UserList"
  226.  
  227. $MenuTitle = ""
  228. $MenuMessage = "Is the above User List file path correct?"
  229. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  230. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  231.  
  232. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  233. $MenuChoice = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  234.  
  235. Switch($MenuChoice) {
  236.  
  237. 0 {}
  238.  
  239. 1 {
  240.  
  241. Do {
  242.  
  243. $UserList = Get-FileName -initialDirectory $DataDirectory -FileTitle "Choose a new UserList.csv to bulk assign licenses."
  244.  
  245. } While ($UserList -eq "")
  246.  
  247. }
  248.  
  249. }
  250.  
  251. } While ($MenuChoice -ne 0)
  252.  
  253. }
  254.  
  255. }
  256.  
  257. Do {
  258.  
  259. $Disabled = @()
  260. $AssignLicenseArray = @()
  261.  
  262. Do {
  263.  
  264. Clear
  265.  
  266.  
  267. $AccountSkuId = $null
  268. $AccountSkuIdWithoutDomain = $null
  269.  
  270.  
  271. Write-Host "Choose a primary license to assign to these users:"
  272.  
  273. $values = Get-MsolAccountSku
  274. $val = Select-TextItem $values "AccountSkuId"
  275. $val.AccountSkuId
  276.  
  277. $AccountSkuId = $val.AccountSkuId
  278.  
  279. $Pos = $AccountSkuId.IndexOf(":")
  280.  
  281. $AccountSkuIdWithoutDomain = $AccountSkuId.Substring($pos+1)
  282.  
  283. $AssignLicenseArray += $AccountSkuId
  284.  
  285. $AssignLicenseString = [String]$AssignLicenseArray
  286.  
  287. Clear
  288.  
  289. If($AssignLicenseArray.Length -eq 1) { $PrimaryLicense = $AccountSkuId }
  290.  
  291. $MenuTitle = "Office 365 License Assignment"
  292. $MenuMessage = "Would you like to disable any service plans with in the $AccountSkuIdWithoutDomain subscription?"
  293. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  294. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  295.  
  296. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  297. $MenuChoice = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  298.  
  299. Switch($MenuChoice) {
  300.  
  301. 0 {
  302.  
  303. Do {
  304.  
  305. Clear
  306.  
  307. Write-Host "Choose a service plan to disable."
  308.  
  309. $values = Get-MsolAccountSku | Where {$_.SkuPartNumber -eq $AccountSkuIdWithoutDomain}
  310. $s = $values.ServiceStatus
  311. $o = $s.ServicePlan.ServiceName
  312. $val = Select-TextItem $o
  313.  
  314. If($val -ne $Null) { $Disabled += $val }
  315.  
  316. $MenuTitle = "Office 365 License Assignment"
  317. $MenuMessage = "Would you like to choose another service plan to disable?"
  318. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  319. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  320.  
  321. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  322. $AnotherDisable = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  323.  
  324. } While($AnotherDisable -ne 1)
  325.  
  326. }
  327.  
  328. }
  329.  
  330. Clear
  331.  
  332. $MenuTitle = "Office 365 License Assignment"
  333. $MenuMessage = "Would you like to assign another license to this user?"
  334. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  335. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  336.  
  337. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  338. $LicenseToAssign = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  339.  
  340. Switch($LicenseToAssign) {
  341.  
  342. 0 { $LicenseToAssign2 = $TRUE }
  343.  
  344. 1 { $LicenseToAssign2 = $FALSE; $DisabledPlans = $Disabled -join ', '; $AssignLicense = $AssignLicenseArray -join ', ' }
  345.  
  346. }
  347.  
  348. } While($LicenseToAssign2 -eq $TRUE)
  349.  
  350. Clear
  351.  
  352. Write-Host -ForegroundColor Cyan "Tenant:"$TenantName
  353. Write-Host ""
  354. Switch($Type) { Single { Write-Host -ForegroundColor Yellow "User Account:" $UPN } Bulk { Write-Host -ForegroundColor Yellow "User List CSV:" $UserList } }
  355. Write-Host ""
  356. Write-Host -ForegroundColor Green "Primary Subscription Plan"
  357. Write-Host " $AssignLicense"
  358. Write-Host ""
  359. Write-Host -ForeGroundColor Green "Service Plans to disable"
  360. Write-Host " $DisabledPlans"
  361. Write-Host ""
  362.  
  363. $MenuTitle = ""
  364. $MenuMessage = "Confirm the license assignment above."
  365. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Correct",""
  366. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&Incorrect",""
  367.  
  368. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  369. $ConfirmLicenseChoice = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  370.  
  371. Switch($ConfirmLicenseChoice) {
  372.  
  373. 0 {
  374.  
  375. Write-Host ""
  376. $ConfirmLicense = $TRUE
  377.  
  378. #==============================================
  379.  
  380. $UsageLocation = Read-Host "Enter the two letter country code for Usage Location"
  381. Write-Host ""
  382.  
  383. $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $PrimaryLicense -DisabledPlans $Disabled
  384.  
  385. Switch($Type) {
  386.  
  387. Bulk {
  388.  
  389. Import-CSV $UserList | % {
  390.  
  391. Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation
  392. Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AssignLicenseArray -LicenseOptions $LicenseOptions
  393.  
  394. Write-Host "Licenses: $AssignLicenseArray have been assigned to" $_.UserPrincipalName
  395.  
  396. }
  397.  
  398. }
  399.  
  400. Single {
  401.  
  402. Set-MsolUser -UserPrincipalName $UPN -UsageLocation $UsageLocation
  403. Set-MsolUserLicense -UserPrincipalName $UPN -AddLicenses $AssignLicenseArray -LicenseOptions $LicenseOptions
  404.  
  405. Write-Host "Licenses: $AssignLicenseArray have been assigned to $UPN"
  406.  
  407. }
  408.  
  409. }
  410.  
  411. }
  412.  
  413. 1 { $ConfirmLicense = $FALSE }
  414.  
  415. }
  416.  
  417. } While($ConfirmLicense -eq $FALSE)
  418.  
  419. Write-Host ""
  420.  
  421. $MenuTitle = ""
  422. $MenuMessage = "Would you like to assign Office 365 Licenses to another user or user list?"
  423. $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
  424. $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
  425.  
  426. $MenuOptions = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
  427. $MainLicenseMenuChoice = $host.ui.PromptForChoice($MenuTitle, $MenuMessage, $MenuOptions, 0)
  428.  
  429. } While($MainLicenseMenuChoice -ne 1)
  430.  
  431. $Session = Get-PSSession | Where-Object {$_.ComputerName -like "*outlook.com*"}
  432. Remove-PSSession -Id $Session.Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement