none
Import-csv in a foreach statement help RRS feed

  • Question

  • I am currently writing this script to loop thru a csv and if the userid matches the current log on user printer will install and output the result to text box and if not found output that result to a textbox also. However the output is all out of order. It seem like it only get the last line and output the result. if I input in the input box the last line it output correct, but if its any other it just output and error out the last line saying not foundl E It output the wrong user stuff and does out output a error if user not found to the outbox. Inside the csv is 5 collumn:USERID,Printer1,Printer2,Printer3,Printer4 with 500 rows.

    Example: Userid is Ricky on line 800 which is last line. If add Ricky to the input box it will output his printer. However if i put in any other Userid  exist or not it will still give me and error saying Ricky not found.

    The above is this script is a XML not sure if it can be copy here but the rest of the script is here. Need help if you can.

    -----------------------------------------------------------

    [xml]$XAML  = @"
    <Window  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PrinterInstaller"
    Title="Printer Install" Height="250"  Width="400">
    <Grid>
    <GroupBox  x:Name="Actions" Header="Actions"  HorizontalAlignment="Left" Height="299"  VerticalAlignment="Top" Width="77"  Margin="0,11,0,0">
    <StackPanel>
    <Button  x:Name="Printers_btn" Content="Printer"/>
    <Label  />
    </StackPanel>
    </GroupBox>
    <GroupBox  x:Name="Username" Header="Username"  HorizontalAlignment="Left" Margin="92,11,0,0" VerticalAlignment="Top"  Height="45" Width="270">
    <TextBox  x:Name="Inputbox" TextWrapping="Wrap"/>            
    </GroupBox>
    <GroupBox  x:Name="Results" Header="Results"  HorizontalAlignment="Left" Margin="92,61,0,0"  VerticalAlignment="Top" Height="150"  Width="270">
    <TextBox  x:Name="Output" IsReadOnly="True"  HorizontalScrollBarVisibility="Auto"  VerticalScrollBarVisibility="Auto" />
    </GroupBox>
    </Grid>
    </Window>
    "@ 

    $reader=(New-Object System.Xml.XmlNodeReader  $xaml)

    $Window=[Windows.Markup.XamlReader]::Load(  $reader )
    #Connect to Controls 
    $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach {
    New-Variable  -Name $_.Name -Value $Window.FindName($_.Name) -Force}
    #region Events 
    $Printers_btn.Add_Click({
    if ($Inputbox.Text)  {
    $Username  = $Inputbox.Text
    Import-csv c:\temp\id.csv | ForEach {
        $User = $_.UseriD
        if ($Username -match $User){
        Add-printer -ConnectionName $_.printer0
     $output.text = $_.printer1, $_.printer2|out-string
            }
     Else {
        $output.text = "$User not found"|out-string
      }
        }
        }
      })

      #endregion Events 




    • Edited by tn6478 Saturday, June 15, 2019 3:21 AM add more info
    Saturday, June 15, 2019 3:03 AM

Answers

  • Thank you for the respond but I have my reason for this. Its a complicated company and Department dont work with each other so well so this is why its has to be coded the way it is. Yes I understand this should be done via Group Policy. Either way thank you for your response.

    No matter how you code it it won't work.  It is just a bad idea and a very bad implementation trying to solve a simple problem.  Just use a script that gets the users printers and assigns them. 

    What I posted will work as a simple script.

    This is all you need to do this:

    Import-csv c:\temp\id.csv | 
        Where-Object{ $_.UseriD -eq $env:USERNAME } |
        ForEach-Object{
            Add-printer -ConnectionName $_.printer0
            Write-Host $_.printer1, $_.printer2
        }

    Just put it in a file and run it.


    \_(ツ)_/


    • Marked as answer by tn6478 Saturday, June 15, 2019 6:04 PM
    • Edited by jrvModerator Saturday, June 15, 2019 6:08 PM
    Saturday, June 15, 2019 5:04 PM
    Moderator

All replies

  • Please post your code correctly using the code posting tool provided.

    We do  not customize code found on the Internet.

    Please ask a single question and provide a simple example of your problem.

    The primary CmdLet for altering a GPO is Set-GPRegistryValue - read the docs in the link for instructions.

    How to ask questions in a technical forum


    \_(ツ)_/


    Saturday, June 15, 2019 9:52 AM
    Moderator
  • You never exit the foreach loop if you find a match. For every user that doesn't match the one you're looking for you set $output.text.

    Try adding  "break" after the "$output.text = $_.printer, . . ."


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, June 15, 2019 2:17 PM
  • The following avoids all of the noise:

    $Printers_btn.Add_Click({
        if($Inputbox.Text){
            Import-csv c:\temp\id.csv | 
                Where-Object{ $_.UseriD -eq $Inputbox.Text } |
                ForEach-Object{
                    Add-printer -ConnectionName $_.printer0
                    $output.text = $_.printer1, $_.printer2|out-string
                }
            }
    })
    

    Still this exercise makes no sense technically.  Why enter a user name when the username already exisits as "$env:username"?  Where would you run a GUI for a user to do something that does not require a GUI? 

    All of the should be done with Group Policy.


    \_(ツ)_/

    Saturday, June 15, 2019 2:56 PM
    Moderator
  • Thank you for the respond but I have my reason for this. Its a complicated company and Department dont work with each other so well so this is why its has to be coded the way it is. Yes I understand this should be done via Group Policy. Either way thank you for your response.

    Saturday, June 15, 2019 4:31 PM
  • Thank you for the respond but I have my reason for this. Its a complicated company and Department dont work with each other so well so this is why its has to be coded the way it is. Yes I understand this should be done via Group Policy. Either way thank you for your response.

    No matter how you code it it won't work.  It is just a bad idea and a very bad implementation trying to solve a simple problem.  Just use a script that gets the users printers and assigns them. 

    What I posted will work as a simple script.

    This is all you need to do this:

    Import-csv c:\temp\id.csv | 
        Where-Object{ $_.UseriD -eq $env:USERNAME } |
        ForEach-Object{
            Add-printer -ConnectionName $_.printer0
            Write-Host $_.printer1, $_.printer2
        }

    Just put it in a file and run it.


    \_(ツ)_/


    • Marked as answer by tn6478 Saturday, June 15, 2019 6:04 PM
    • Edited by jrvModerator Saturday, June 15, 2019 6:08 PM
    Saturday, June 15, 2019 5:04 PM
    Moderator