?
Solved

PowerShell Script menu choices

Posted on 2014-02-25
4
Medium Priority
?
933 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 70

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 70

Accepted Solution

by:
Qlemo earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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