Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Question About Out-Grid

Posted on 2016-10-06
6
Medium Priority
?
69 Views
Last Modified: 2016-10-06
Hi, I hope you can help with a question I have.

I have a User CSV file and I want the outgrid of licenses to be selectable and populate the line

$server = read-host 'SELECT A LICENSE PRODUCT TO ADD TO USER' HERE

So, if I run the script it will loop through the users, ask what CSV file you want to open and then open the out-grid showing licenses available on the tenant. It is here I want to select multiple option of licenses and pass the selected items into the next prompted line in powershell, this is at the moment a manual process of typing them out, I want this to populate from the selections made on the out-grid.

 #CSV file picker module start
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
}

#CSV file picker module end

#Variable that holds CSV file location from file picker
$path = Get-FileName -initialDirectory "C:\csv"

#Window with list of available 365 licenses and their names
Get-MsolAccountSku | out-gridview -Title "Work Scripts" -passthru

#Input window where you provide the license package's name
$server = read-host 'SELECT A LICENSE/PRODUCT TO ADD TO USER:'

#CSV import command and mailbox creation loop
import-csv $path | foreach {
New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname $_.displayname -firstname $_.firstname -lastname $_.lastname -password $_.Password -usagelocation "us" | set-msoluserlicense -addlicenses "$server"
}

#Result report on licenses assigned to imported users
import-csv $path | Get-MSOLUser | out-gridview

Many thanks

John
0
Comment
Question by:John Davies
[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
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41831540
Can't test this with msol, but in general, it's fairly easy to use Out-Gridview to let the user choose from a set:
"Waiting for you to select something from the dialog ..."
$Selection = Get-Item C:\Windows\*.exe | Select-Object BaseName, Length, FullName | Out-GridView -OutputMode Multiple -Title "Select one or more programs"
If ($Selection) {
	"Selected programs: "
	$Selection | Format-List
} Else {
	"Operation canceled"
}

Open in new window

To restrict the selection to a single line, use "-OutputMode Single" instead of "Multiple".

So in your case, you can do something like this (and you may want to pass the Get-MsolAccountSku output through a Select-Object, like the example above):
"Waiting for you to select something from the dialog ..."
$Selection = Get-MsolAccountSku | Out-GridView -Title "Work Scripts" -OutputMode Multiple
If ($Selection) {
	"Selected licenses: "
	$Selection | Format-List
	## Do something with the selection ...
} Else {
	"Operation canceled"
}

Open in new window

0
 

Author Comment

by:John Davies
ID: 41831644
Thanks OBda

I can see this working, however it doesnt populate the line after select a license product to add to user What I get is the license info displayed. What i am looking for is to populate this line (see image) from the selections made.

CODE where I have added your multiple line

#CSV file picker module start
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
}

#CSV file picker module end

#Variable that holds CSV file location from file picker
$path = Get-FileName -initialDirectory "csv"

"Waiting for you to select something from the dialog ..."
$Selection = Get-MsolAccountSku | Out-GridView -Title "Work Scripts" -OutputMode Multiple
If ($Selection) {
      "Selected licenses: "
      $Selection | Format-List
      ## Do something with the selection ...
} Else {
      "Operation canceled"
}


#Input window where you provide the license package's name
$server  = read-host 'SELECT A LICENSE PRODUCT TO ADD TO USER'

#CSV import command and mailbox creation loop
import-csv $path | foreach {
New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname $_.displayname -firstname $_.firstname -lastname $_.lastname -password $_.Password -usagelocation "us" | set-msoluserlicense -addlicenses "$server"
}

#Result report on licenses assigned to imported users
import-csv $path | Get-MSOLUser | out-gridview

Thanks for your help.

John
2016-10-06_13-15-20.png
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41831659
The Out-GridView replaces the Read-Host; you won't need that anymore.
You can work directly with the contents of $Selection.
0
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 

Author Comment

by:John Davies
ID: 41831832
Hi Mate, looks like it is failing on the $Selection variable, have called it at the end of Set-MsolUserLicense - AddLicenses

}

#CSV file picker module end

#Variable that holds CSV file location from file picker
$path = Get-FileName -initialDirectory "C:\BulkUpload\BulkUpload.csv"

"Waiting for you to select something from the dialog ..."
$Selection = Get-MsolAccountSku | Out-GridView -Title "Work Scripts" -OutputMode Multiple
If ($Selection) {
      "Selected licenses: "
      $Selection | Format-List
      ## Do something with the selection ...
} Else {
      "Operation canceled"
}

#CSV import command and mailbox creation loop
import-csv $path | foreach {
New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname $_.displayname -firstname $_.firstname -lastname $_.lastname -password $_.Password -usagelocation "GB" | Set-MsolUserLicense -AddLicenses $Selection
}

#Result report on licenses assigned to imported users
import-csv $path | Get-MSOLUser | out-gridview

ERROR

Set-MsolUserLicense : Unable to assign this license because it is invalid. Use the Get-MsolAccountSku cmdlet to retrieve a list of valid licenses.
At C:\BulkUpload\BulkUpload1.ps1:31 char:172
+ ... ord -usagelocation "GB" | Set-MsolUserLicense -AddLicenses $Selection
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Microsoft.Onlin...countSkuDetails:String) [Set-MsolUserLicense], MicrosoftOnlineException
    + FullyQualifiedErrorId : Invalid License Assignment,Microsoft.Online.Administration.Automation.SetUserLicense

This has retrieved the users from the CSV, stored my selection in the $selection variable and am trying to assign that variable to the users when creating the accounts.

Stumped and thank you for your help so far.

John
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41831899
Out-GridView returns the selected object(s) as passed to it for display, so you need to specify whatever property returned by Get-MsolAccountSku is required for set-msoluserlicense.
Replace the uppercase PROPERTY in line 24 with the actual property you need.
#CSV file picker module start
Function Get-FileName($initialDirectory) {  
	[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
	$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
	$OpenFileDialog.initialDirectory = $initialDirectory
	$OpenFileDialog.filter = "All files (*.*)| *.*"
	$OpenFileDialog.ShowDialog() | Out-Null
	$OpenFileDialog.filename
} 
#CSV file picker module end

#Variable that holds CSV file location from file picker
$path = Get-FileName -initialDirectory "C:\csv"

"Waiting for you to select something from the dialog ..."
$Selection = Get-MsolAccountSku | Out-GridView -Title "Work Scripts" -OutputMode Multiple
If ($Selection) {
	"Selected licenses: "
	$Selection | Format-List
	## Do something with the selection ...
	#CSV import command and mailbox creation loop
	import-csv $path | foreach {
		New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname $_.displayname -firstname $_.firstname -lastname $_.lastname -password $_.Password -usagelocation "us" |
			set-msoluserlicense -addlicenses $Selection.PROPERTY
	}

	#Result report on licenses assigned to imported users
	import-csv $path | Get-MSOLUser | out-gridview
} Else {
	"Operation canceled"
}

Open in new window

0
 

Author Closing Comment

by:John Davies
ID: 41831936
Bang on Mate, accountskuid did it :-) thanks for all your help and your patience.

Cheers

John
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

609 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