script filtering multiple returns to menu

here's is what I am trying to do:

install printer on a remote machine . Sounds simple eh?

the script will gather information from a master printer list (csv) it will then take make,model an ip address from the csv and execute vb script to set up printer.

now here is where it gets complicated. on occasion a location will have more than one printer. how can i offer the user a choice of which printer to install in this case?

I have attached some of the code I am using to test...

the PSCustomObject section prints the information very nicely, I now need to turn that into a menu/selection option...
$list = Import-Excel -path "C:\Users\ewholfarth\Downloads\posh1.xlsx"  
$list | export-Csv "C:\Users\ewholfarth\Downloads\posh1.csv"


$printerlist =import-csv -path "C:\Users\ewholfarth\Downloads\posh1.csv"
$printercount = $printerlist.count;
$jobnumber = read-host "Enter job number =>"
$currentitem = 1;
$i = 1

foreach($printer in $printerlist){
    if ($printer.job_number -eq $jobnumber){
        [PSCustomObject][ordered]@{
            Make   = $printer.make
            Model  = $printer.model
            'Job#' = $printer.job_number
        }
    }
}

Open in new window




Thanks,

Eugene
Deacon Eugene WohlfarthSoccer and Futsal RefAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Out-GridView (PS 3.0 and later) is a nice way for a selection like this. You pass it a list of objects, it'll display them, the user can select one (or more, depending on the arguments passed), and it will return the selected object(s).
## ...
Write-Host "Please select a printer in the GUI popup to continue ..."
$SelectedPrinter = $printerlist | Out-Gridview -Title 'Please select a printer and click OK.' -OutputMode Single
If ($SelectedPrinter) {
	Write-Host "Selected printer: $($SelectedPrinter.Model)"
} Else {
	Write-Warning "Installation canceled by user!"
}

Open in new window

0
Deacon Eugene WohlfarthSoccer and Futsal RefAuthor Commented:
oBdA,

Thank you, I'm just having some issues as to where to insert the code....

Thanks again,

Eugene (PS newbie)
0
Deacon Eugene WohlfarthSoccer and Futsal RefAuthor Commented:
update:

I think I found some of my own answers..here's current code:
$list = Import-Excel -path "C:\Users\ewholfarth\Downloads\posh1.xlsx"  
$list | export-Csv "C:\Users\ewholfarth\Downloads\posh1.csv"


$printerlist =import-csv -path "C:\Users\ewholfarth\Downloads\posh1.csv"
$printercount = $printerlist.count;
$jobnumber = read-host "Enter job number =>"
$currentitem = 1;
$i = 1

foreach($printer in $printerlist){
    if ($printer.job_number -eq $jobnumber){
       
        Write-Host "Please select a printer in the GUI popup to continue ..."
       $selectprinter =  $printer | Out-Gridview -Title 'Please select a printer and click OK.' -OutputMode Single
        If ($SelectedPrinter) {
	        Write-Host "Selected printer: $($SelectedPrinter.Model)"
          } Else {
	        Write-Warning "Installation canceled by user!"
            


       # [PSCustomObject][ordered]@{
       #     Make   = $printer.make
       #     Model  = $printer.model
       #     'Job#' = $printer.job_number   
         
       
    
       
       
       
        }
    }
  }

Open in new window




now my issue is that if i have multiple printers at a job location I am getting separate GUI boxes for each printer instead of all listed in 1...
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

oBdACommented:
Because the point is to pass the complete printer list to Out-GridView, not a single printer.
I don't quite get what the "job number" is for you're querying in the Read-Host? Was that your initial attempt at a menu, or does it filter the selection for the user from a huge list?
Will the user running the script know the job number required beforehand?
And is there a specific reason why you're first exporting the list to a file, only to read the same file immediately again?
0
Deacon Eugene WohlfarthSoccer and Futsal RefAuthor Commented:
OK,

Job # is the site where the printer are located. each site has different printers. we only want to list the printers listed at that site(job). Basically it filters from the entire list down to what is required.

The reason for the import-excel - export-csv - import-csv was that PS was doing (or not doing) the data conversion correctly when I first started.

The user running the script will know the job(site) number when beginning. They will also know the IP address where the printer will be installed.

Thanks again for your help,

Eugene
0
oBdACommented:
This should work; the menu now only pops up if more than one printer is found.
$PrinterList = Import-Excel -Path "C:\Users\ewholfarth\Downloads\posh1.xlsx" | ConvertTo-Csv | ConvertFrom-Csv
$JobNumberList = $PrinterList | Select-Object -ExpandProperty 'Job_Number' -Unique | Sort-Object
Write-Host "Job numbers: $($JobNumberList -join ', ')"
$JobNumber = (Read-Host "Enter job number =>").Trim()
If ($JobNumberList -notcontains $JobNumber) {
	Write-Warning "Invalid job number '$($JobNumber)'!"
	Exit
}

$PrinterListFiltered = $PrinterList | Where-Object {$_.Job_Number -eq $JobNumber}
If ($PrinterListFiltered.Count -gt 1) {
	Write-Host "Please select a printer in the GUI popup to continue ..."
	$SelectedPrinter = $PrinterListFiltered | Out-Gridview -Title 'Please select a printer and click OK.' -OutputMode Single
	If (-not $SelectedPrinter) {
		Write-Warning "Installation canceled by user!"
		Exit
	}
} Else {
	$SelectedPrinter = $PrinterListFiltered
}
Write-Host "Selected printer: $($SelectedPrinter.Model)"
## ...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Deacon Eugene WohlfarthSoccer and Futsal RefAuthor Commented:
OBdA,

This works great ! Thank you !!!!!!

Eugene
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.