Solved

PowerShell Script menu choices

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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