0

I have a script that builds a GUI with a list of printers that will be selected by the user. These printers are also on a CSV file built like this : Computer (name of the printer); IP xxxx;x.x.x.x

I want to collect all the selected values in an array named x Then I want to take every entry in the CSV that corresponds to the selected item and put it in another array named y Finally I export the y array into a new CSV that will be used to install the printers on the domain. I tried to go straight from second step to last step but i couldn't.

Here is the part of the code :

    $OKButton.Add_Click({
        foreach ($objItem in $objListbox.SelectedItems)
            {$x += $objItem}

    y=@()
    for ($i=0; $i -lt $x.length; $i++)
    {
        $y[$i]=Import-Csv C:\Users\Administrateur\Desktop\Classeur33.csv | Where-Object {$_.Computer -eq $x[$i]}
    }


$y > C:\Users\Administrateur\Desktop\allezjoue.csv

I've tried to do it with a 3 values x array in another script and it worked fine, but I really need to keep the listbox that allows the user to select the printers he wants.

Powershell always returns me "Index out of range" I tried to put "$y=$x" so they have the same range, but when I do this it returns that I can't index in an object which has "System.string" type.

user229044
  • 232,980
  • 40
  • 330
  • 338
Sami
  • 1
  • 2
  • I will start with the obvious comment that $x and $y are horrible variable names. Next I will ask what exactly makes you believe that $x is an array? In the code that you posted at least you do not initialize it as an array. – EBGreen May 18 '15 at 15:44
  • I've done it before the event on the button. – Sami May 18 '15 at 16:04
  • `Where-Object {$_.Computer -eq $x[$i]}` You're comparing one property value to an entire array with the -eq comparison. Sincerely doubt this condition will ever be true. – Jeter-work May 18 '15 at 16:37
  • The answer to your specific question about index out of range is here: https://stackoverflow.com/questions/226596/powershell-array-initialization . Lots of other good advice on this page you still should read. – Ryan Bemrose May 18 '15 at 17:14
  • Why are you importing the csv everytime your for loop runs? I would first start with loading the csv only once (in a variable), then read this on every loop. – bluuf May 18 '15 at 15:52

1 Answers1

1
  • This is PowerShell and very object oriented. Use the objects and collections at hand.
  • Decriptive variable names are your friend.
  • $objListbox.SelectedItems is already a collection of objects.
  • Put it in a variable and loop through it with Foreach-Object aka foreach.
  • Import-CSV returns a collection of objects.

    $Selection = $ObjListbox.SelectedItems  
    $printers = Import-CSV 'C:\Users\Administrateur\Desktop\Classeur33.csv'  
    foreach ($chosen in $Selection) { 
        $printers = $printers | where-object { $_.Computer -eq $Chosen.Name } 
        } 
    $printers | Export-CSV 'C:\Users\Administrateur\Desktop\allezjoue.csv' -NoTypeInformation
    

$Chosen.Name should be edited to conform with whatever objects you get in $Selection. You can test this by $ObjListbox.SelectedItems | Get-Member and examining the members for a property with the name of the item selected, then assuming the names match what's in your CSV, you should be good.

  • (bonus note) Storing data in and running as local admin is bad practice, even on your home lab. Your mistakes will have the power of local admin, and your users will not be able to run the scripts since the source/results files are in admin's desktop.
Jeter-work
  • 782
  • 7
  • 22