Solved

PowerShell Script menu choices

Posted on 2014-02-25
4
876 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.

856 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