Solved

PowerShell Script menu choices

Posted on 2014-02-25
4
825 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
  • 2
  • 2
4 Comments
 
LVL 68

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 68

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A procedure for exporting installed hotfix details of remote computers using powershell
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now