Link to home
Create AccountLog in
Avatar of Deacon Eugene Wohlfarth
Deacon Eugene Wohlfarth

asked on

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
Avatar of oBdA
oBdA

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

Avatar of Deacon Eugene Wohlfarth

ASKER

oBdA,

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

Thanks again,

Eugene (PS newbie)
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...
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?
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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
OBdA,

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

Eugene