Solved

PowerShell Script menu choices

Posted on 2014-02-25
4
885 Views
Last Modified: 2014-02-25
I am still learning PowerShell and am in no way great at it yet. I am a novice. I can get the basics to work and pretty much get done what is needed. Now I am trying to get into something a bit more pretty and hope I can get some help.


I have a PS script which applies licenses for users in Office 365. The script currently asks which license to apply and requires the users input.  

What I would really like to do is make it a multiple choice question.
Press 1 for this license
Press 2 for that license

Here is my current script which is working, but does not have a menu selection choice.

#--------------------------------------------------------------------------------------
$PackageName = "ApplyLicenseO365"
$logDir = "c:\Windows\Temp\"
$inputCVS = "c:\Windows\Temp\Users.csv"
$logFile = "$logDir\Apply_License_Log.txt"

#--------------------------------------------------------------------------------------



Write-Host "Please Enter the license SKU to apply to users on the list" -ForegroundColor Magenta
Write-Host "SKU:DESKLESSWOFFPACK for K1" -ForegroundColor Green
Write-Host "SKU:STANDARDPACK for E1" -ForegroundColor DarkGreen
$license = Read-Host


$AccountSkuId = "$license"

$UsageLocation = "US"

$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $license

#Import CSV File
Import-Csv $inputCVS |foreach {
	$getUsername = $_.User

Set-MsolUser -UserPrincipalName "$getUsername" -UsageLocation $UsageLocation

Set-MsolUserLicense -UserPrincipalName "$getUsername" -AddLicenses $license -LicenseOptions $LicenseOptions

}

Open in new window

0
Comment
Question by:alexr54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 39886031
One way to do that:
#--------------------------------------------------------------------------------------
$PackageName = "ApplyLicenseO365"
$logDir = "c:\Windows\Temp\"
$inputCVS = "c:\Windows\Temp\Users.csv"
$logFile = "$logDir\Apply_License_Log.txt"

#--------------------------------------------------------------------------------------

do {
  $resp = Read-Host @"
  1 - DESKLESSWOFFPACK for K1
  2 - STANDARDPACK for E1
  0 - none
"@
} while ('1', '2', '0' -notcontains $resp)

if ($resp -ne 0)
{
  $AccountSkuId = @('','SKU:DESKLESSWOFFPACK for K1','SKU:STANDARDPACK for E1')[[int]$resp]
  $UsageLocation = "US"

  $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $license

  #Import CSV File
  Import-Csv $inputCVS | foreach {
    $getUsername = $_.User
    Set-MsolUser -UserPrincipalName "$getUsername" -UsageLocation $UsageLocation
    Set-MsolUserLicense -UserPrincipalName "$getUsername" -AddLicenses $license -LicenseOptions $LicenseOptions
  } 
}

Open in new window

You can, of course, use your Write-Host/Read-Host combination again, to allow for colored output. The basics are the same.
0
 
LVL 2

Author Comment

by:alexr54
ID: 39886065
Tried that but its coming back with an error:

New-MsolLicenseOptions : Cannot bind argument to parameter 'AccountSkuId'
because it is null.
At C:\Scripts\ApplyO365LicenseChoices.ps1:22
char:58
+   $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $license
+                                                          ~~~~~~~~
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39886090
Sorry, I didn't check your remaining code. You didn't use $AccountSkuId, but $license, in the cmdlets.
#--------------------------------------------------------------------------------------
$PackageName = "ApplyLicenseO365"
$logDir = "c:\Windows\Temp\"
$inputCVS = "c:\Windows\Temp\Users.csv"
$logFile = "$logDir\Apply_License_Log.txt"

#--------------------------------------------------------------------------------------

do {
  $resp = Read-Host @"
  1 - DESKLESSWOFFPACK for K1
  2 - STANDARDPACK for E1
  0 - none
"@
} while ('1', '2', '0' -notcontains $resp)

if ($resp -ne 0)
{
  $AccountSkuId = @('','SKU:DESKLESSWOFFPACK for K1','SKU:STANDARDPACK for E1')[[int]$resp]
  $UsageLocation = "US"

  $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId

  #Import CSV File
  Import-Csv $inputCVS | foreach {
    $getUsername = $_.User
    Set-MsolUser -UserPrincipalName "$getUsername" -UsageLocation $UsageLocation
    Set-MsolUserLicense -UserPrincipalName "$getUsername" -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions
  } 
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:alexr54
ID: 39886117
Works like a charm!
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question