Using a function to populate a textbox in Powershell











up vote
0
down vote

favorite












I currently have a script that queries our AD for the users' AD attribute "Department".



Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.



If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.



How can I get my function to populate TextBox2?

BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.



Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Howard Center Profile Migration'
$form.Size = New-Object System.Drawing.Size(800,650)
$form.StartPosition = 'CenterScreen'

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the Cost Center # in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(100,20)
$form.Controls.Add($textBox)

$RunButton = New-Object System.Windows.Forms.Button
$RunButton.Location = New-Object System.Drawing.Point(75,120)
$RunButton.Size = New-Object System.Drawing.Size(75,23)
$RunButton.Text = 'RUN'
#$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK


<#$RunButton.Add_Click({
#add here code triggered by the event
$TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
})
#>

$form.AcceptButton = $RunButton
$form.Controls.Add($RunButton)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'When you are ready click the Run button below'
$form.Controls.Add($label2)

$TextBox2 = New-Object system.windows.Forms.TextBox
$TextBox2.Text = ""
$TextBox2.Multiline = $true
$TextBox2.BackColor = "#013686"
$TextBox2.ScrollBars = "Both"
$TextBox2.Width = 750
$TextBox2.Height = 450
$TextBox2.location = new-object system.drawing.point(10,150)
$TextBox2.Font = "Microsoft Sans Serif,10"
$TextBox2.ForeColor = "#ffffff"



$Form.controls.Add($TextBox2)

$form.Topmost = $true

function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq $dept"
ForEach ($user in $deptUsers) {
IF ( ((get-aduser $user).enabled) -eq $True ) {
$UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
$UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
Write-Host "$user, $UHomeDir, $UProfPath"
}
}
}

$RunButton.Add_Click({
$TextBox2.Text = Get-CCUsers
})

$TextBox2.Add_TextChanged({
$TextBox2.SelectionStart = $TextBox2.Text.Length
$TextBox2.ScrollToCaret()
})

$form.Add_Shown({$Form.Update()})
$result = $form.ShowDialog()

$global:x = $textBox.Text
# $x


# if ($result -eq [System.Windows.Forms.DialogResult]::OK)
#{




#}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I currently have a script that queries our AD for the users' AD attribute "Department".



    Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.



    If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.



    How can I get my function to populate TextBox2?

    BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.



    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Howard Center Profile Migration'
    $form.Size = New-Object System.Drawing.Size(800,650)
    $form.StartPosition = 'CenterScreen'

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Please enter the Cost Center # in the space below:'
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(100,20)
    $form.Controls.Add($textBox)

    $RunButton = New-Object System.Windows.Forms.Button
    $RunButton.Location = New-Object System.Drawing.Point(75,120)
    $RunButton.Size = New-Object System.Drawing.Size(75,23)
    $RunButton.Text = 'RUN'
    #$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK


    <#$RunButton.Add_Click({
    #add here code triggered by the event
    $TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
    })
    #>

    $form.AcceptButton = $RunButton
    $form.Controls.Add($RunButton)

    $label2 = New-Object System.Windows.Forms.Label
    $label2.Location = New-Object System.Drawing.Point(10,70)
    $label2.Size = New-Object System.Drawing.Size(280,20)
    $label2.Text = 'When you are ready click the Run button below'
    $form.Controls.Add($label2)

    $TextBox2 = New-Object system.windows.Forms.TextBox
    $TextBox2.Text = ""
    $TextBox2.Multiline = $true
    $TextBox2.BackColor = "#013686"
    $TextBox2.ScrollBars = "Both"
    $TextBox2.Width = 750
    $TextBox2.Height = 450
    $TextBox2.location = new-object system.drawing.point(10,150)
    $TextBox2.Font = "Microsoft Sans Serif,10"
    $TextBox2.ForeColor = "#ffffff"



    $Form.controls.Add($TextBox2)

    $form.Topmost = $true

    function Get-CCUsers {
    Write-Host "The textbox text is $textbox.Text"
    $dept = $textBox.Text
    $deptUsers = Get-ADUser -Filter "Department -eq $dept"
    ForEach ($user in $deptUsers) {
    IF ( ((get-aduser $user).enabled) -eq $True ) {
    $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
    $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
    Write-Host "$user, $UHomeDir, $UProfPath"
    }
    }
    }

    $RunButton.Add_Click({
    $TextBox2.Text = Get-CCUsers
    })

    $TextBox2.Add_TextChanged({
    $TextBox2.SelectionStart = $TextBox2.Text.Length
    $TextBox2.ScrollToCaret()
    })

    $form.Add_Shown({$Form.Update()})
    $result = $form.ShowDialog()

    $global:x = $textBox.Text
    # $x


    # if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    #{




    #}









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I currently have a script that queries our AD for the users' AD attribute "Department".



      Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.



      If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.



      How can I get my function to populate TextBox2?

      BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.



      Add-Type -AssemblyName System.Windows.Forms
      Add-Type -AssemblyName System.Drawing

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Howard Center Profile Migration'
      $form.Size = New-Object System.Drawing.Size(800,650)
      $form.StartPosition = 'CenterScreen'

      $CancelButton = New-Object System.Windows.Forms.Button
      $CancelButton.Location = New-Object System.Drawing.Point(150,120)
      $CancelButton.Size = New-Object System.Drawing.Size(75,23)
      $CancelButton.Text = 'Cancel'
      $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
      $form.CancelButton = $CancelButton
      $form.Controls.Add($CancelButton)

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Please enter the Cost Center # in the space below:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(100,20)
      $form.Controls.Add($textBox)

      $RunButton = New-Object System.Windows.Forms.Button
      $RunButton.Location = New-Object System.Drawing.Point(75,120)
      $RunButton.Size = New-Object System.Drawing.Size(75,23)
      $RunButton.Text = 'RUN'
      #$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK


      <#$RunButton.Add_Click({
      #add here code triggered by the event
      $TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
      })
      #>

      $form.AcceptButton = $RunButton
      $form.Controls.Add($RunButton)

      $label2 = New-Object System.Windows.Forms.Label
      $label2.Location = New-Object System.Drawing.Point(10,70)
      $label2.Size = New-Object System.Drawing.Size(280,20)
      $label2.Text = 'When you are ready click the Run button below'
      $form.Controls.Add($label2)

      $TextBox2 = New-Object system.windows.Forms.TextBox
      $TextBox2.Text = ""
      $TextBox2.Multiline = $true
      $TextBox2.BackColor = "#013686"
      $TextBox2.ScrollBars = "Both"
      $TextBox2.Width = 750
      $TextBox2.Height = 450
      $TextBox2.location = new-object system.drawing.point(10,150)
      $TextBox2.Font = "Microsoft Sans Serif,10"
      $TextBox2.ForeColor = "#ffffff"



      $Form.controls.Add($TextBox2)

      $form.Topmost = $true

      function Get-CCUsers {
      Write-Host "The textbox text is $textbox.Text"
      $dept = $textBox.Text
      $deptUsers = Get-ADUser -Filter "Department -eq $dept"
      ForEach ($user in $deptUsers) {
      IF ( ((get-aduser $user).enabled) -eq $True ) {
      $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
      $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
      Write-Host "$user, $UHomeDir, $UProfPath"
      }
      }
      }

      $RunButton.Add_Click({
      $TextBox2.Text = Get-CCUsers
      })

      $TextBox2.Add_TextChanged({
      $TextBox2.SelectionStart = $TextBox2.Text.Length
      $TextBox2.ScrollToCaret()
      })

      $form.Add_Shown({$Form.Update()})
      $result = $form.ShowDialog()

      $global:x = $textBox.Text
      # $x


      # if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      #{




      #}









      share|improve this question















      I currently have a script that queries our AD for the users' AD attribute "Department".



      Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.



      If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.



      How can I get my function to populate TextBox2?

      BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.



      Add-Type -AssemblyName System.Windows.Forms
      Add-Type -AssemblyName System.Drawing

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Howard Center Profile Migration'
      $form.Size = New-Object System.Drawing.Size(800,650)
      $form.StartPosition = 'CenterScreen'

      $CancelButton = New-Object System.Windows.Forms.Button
      $CancelButton.Location = New-Object System.Drawing.Point(150,120)
      $CancelButton.Size = New-Object System.Drawing.Size(75,23)
      $CancelButton.Text = 'Cancel'
      $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
      $form.CancelButton = $CancelButton
      $form.Controls.Add($CancelButton)

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Please enter the Cost Center # in the space below:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(100,20)
      $form.Controls.Add($textBox)

      $RunButton = New-Object System.Windows.Forms.Button
      $RunButton.Location = New-Object System.Drawing.Point(75,120)
      $RunButton.Size = New-Object System.Drawing.Size(75,23)
      $RunButton.Text = 'RUN'
      #$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK


      <#$RunButton.Add_Click({
      #add here code triggered by the event
      $TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
      })
      #>

      $form.AcceptButton = $RunButton
      $form.Controls.Add($RunButton)

      $label2 = New-Object System.Windows.Forms.Label
      $label2.Location = New-Object System.Drawing.Point(10,70)
      $label2.Size = New-Object System.Drawing.Size(280,20)
      $label2.Text = 'When you are ready click the Run button below'
      $form.Controls.Add($label2)

      $TextBox2 = New-Object system.windows.Forms.TextBox
      $TextBox2.Text = ""
      $TextBox2.Multiline = $true
      $TextBox2.BackColor = "#013686"
      $TextBox2.ScrollBars = "Both"
      $TextBox2.Width = 750
      $TextBox2.Height = 450
      $TextBox2.location = new-object system.drawing.point(10,150)
      $TextBox2.Font = "Microsoft Sans Serif,10"
      $TextBox2.ForeColor = "#ffffff"



      $Form.controls.Add($TextBox2)

      $form.Topmost = $true

      function Get-CCUsers {
      Write-Host "The textbox text is $textbox.Text"
      $dept = $textBox.Text
      $deptUsers = Get-ADUser -Filter "Department -eq $dept"
      ForEach ($user in $deptUsers) {
      IF ( ((get-aduser $user).enabled) -eq $True ) {
      $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
      $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
      Write-Host "$user, $UHomeDir, $UProfPath"
      }
      }
      }

      $RunButton.Add_Click({
      $TextBox2.Text = Get-CCUsers
      })

      $TextBox2.Add_TextChanged({
      $TextBox2.SelectionStart = $TextBox2.Text.Length
      $TextBox2.ScrollToCaret()
      })

      $form.Add_Shown({$Form.Update()})
      $result = $form.ShowDialog()

      $global:x = $textBox.Text
      # $x


      # if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      #{




      #}






      winforms function powershell user-interface textbox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 3:30









      mklement0

      121k20233261




      121k20233261










      asked Nov 10 at 16:48









      Michael Louth

      32




      32
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I have made your script working changing the 'Get-CCUSers' function



          function Get-CCUsers {
          Write-Host "The textbox text is $textbox.Text"
          $dept = $textBox.Text
          $deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
          $res = @()
          ForEach ($user in $deptUsers) {
          IF ( ((get-aduser $user).enabled) -eq $True ) {
          $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
          $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
          $res += "$user, $UHomeDir, $UProfPath"
          }
          }
          return $res
          }


          In short:




          1. I removed the Write-Host (the output was actually redirected to stdout)

          2. I added a $res in the function initialized as array

          3. In the ForEach loop, results are added as items in the array

          4. The $res is returned by the function, and then used $RunButton






          share|improve this answer























          • Thanks, Pietro. I'd been going at this for 2 days.
            – Michael Louth
            Nov 11 at 0:30










          • It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
            – mklement0
            Nov 11 at 3:45










          • mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
            – reverpie
            Nov 11 at 15:13












          • I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
            – Michael Louth
            Nov 11 at 16:15










          • mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
            – Michael Louth
            Nov 11 at 17:41











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241170%2fusing-a-function-to-populate-a-textbox-in-powershell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I have made your script working changing the 'Get-CCUSers' function



          function Get-CCUsers {
          Write-Host "The textbox text is $textbox.Text"
          $dept = $textBox.Text
          $deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
          $res = @()
          ForEach ($user in $deptUsers) {
          IF ( ((get-aduser $user).enabled) -eq $True ) {
          $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
          $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
          $res += "$user, $UHomeDir, $UProfPath"
          }
          }
          return $res
          }


          In short:




          1. I removed the Write-Host (the output was actually redirected to stdout)

          2. I added a $res in the function initialized as array

          3. In the ForEach loop, results are added as items in the array

          4. The $res is returned by the function, and then used $RunButton






          share|improve this answer























          • Thanks, Pietro. I'd been going at this for 2 days.
            – Michael Louth
            Nov 11 at 0:30










          • It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
            – mklement0
            Nov 11 at 3:45










          • mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
            – reverpie
            Nov 11 at 15:13












          • I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
            – Michael Louth
            Nov 11 at 16:15










          • mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
            – Michael Louth
            Nov 11 at 17:41















          up vote
          0
          down vote



          accepted










          I have made your script working changing the 'Get-CCUSers' function



          function Get-CCUsers {
          Write-Host "The textbox text is $textbox.Text"
          $dept = $textBox.Text
          $deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
          $res = @()
          ForEach ($user in $deptUsers) {
          IF ( ((get-aduser $user).enabled) -eq $True ) {
          $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
          $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
          $res += "$user, $UHomeDir, $UProfPath"
          }
          }
          return $res
          }


          In short:




          1. I removed the Write-Host (the output was actually redirected to stdout)

          2. I added a $res in the function initialized as array

          3. In the ForEach loop, results are added as items in the array

          4. The $res is returned by the function, and then used $RunButton






          share|improve this answer























          • Thanks, Pietro. I'd been going at this for 2 days.
            – Michael Louth
            Nov 11 at 0:30










          • It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
            – mklement0
            Nov 11 at 3:45










          • mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
            – reverpie
            Nov 11 at 15:13












          • I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
            – Michael Louth
            Nov 11 at 16:15










          • mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
            – Michael Louth
            Nov 11 at 17:41













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I have made your script working changing the 'Get-CCUSers' function



          function Get-CCUsers {
          Write-Host "The textbox text is $textbox.Text"
          $dept = $textBox.Text
          $deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
          $res = @()
          ForEach ($user in $deptUsers) {
          IF ( ((get-aduser $user).enabled) -eq $True ) {
          $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
          $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
          $res += "$user, $UHomeDir, $UProfPath"
          }
          }
          return $res
          }


          In short:




          1. I removed the Write-Host (the output was actually redirected to stdout)

          2. I added a $res in the function initialized as array

          3. In the ForEach loop, results are added as items in the array

          4. The $res is returned by the function, and then used $RunButton






          share|improve this answer














          I have made your script working changing the 'Get-CCUSers' function



          function Get-CCUsers {
          Write-Host "The textbox text is $textbox.Text"
          $dept = $textBox.Text
          $deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
          $res = @()
          ForEach ($user in $deptUsers) {
          IF ( ((get-aduser $user).enabled) -eq $True ) {
          $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
          $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
          $res += "$user, $UHomeDir, $UProfPath"
          }
          }
          return $res
          }


          In short:




          1. I removed the Write-Host (the output was actually redirected to stdout)

          2. I added a $res in the function initialized as array

          3. In the ForEach loop, results are added as items in the array

          4. The $res is returned by the function, and then used $RunButton







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 3:31









          mklement0

          121k20233261




          121k20233261










          answered Nov 10 at 17:14









          reverpie

          18616




          18616












          • Thanks, Pietro. I'd been going at this for 2 days.
            – Michael Louth
            Nov 11 at 0:30










          • It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
            – mklement0
            Nov 11 at 3:45










          • mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
            – reverpie
            Nov 11 at 15:13












          • I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
            – Michael Louth
            Nov 11 at 16:15










          • mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
            – Michael Louth
            Nov 11 at 17:41


















          • Thanks, Pietro. I'd been going at this for 2 days.
            – Michael Louth
            Nov 11 at 0:30










          • It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
            – mklement0
            Nov 11 at 3:45










          • mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
            – reverpie
            Nov 11 at 15:13












          • I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
            – Michael Louth
            Nov 11 at 16:15










          • mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
            – Michael Louth
            Nov 11 at 17:41
















          Thanks, Pietro. I'd been going at this for 2 days.
          – Michael Louth
          Nov 11 at 0:30




          Thanks, Pietro. I'd been going at this for 2 days.
          – Michael Louth
          Nov 11 at 0:30












          It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
          – mklement0
          Nov 11 at 3:45




          It would be simpler - and more efficient - to only remove the Write-Host, given that you can use a foreach loop as an expression too (e.g., $arr = foreach ($i in 1..3) { $_ }); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: Write-Host doesn't write to stdout, it writes to the host, which in a console window is the console (terminal), which bypasses PowerShell's stdout equivalent, the success stream.
          – mklement0
          Nov 11 at 3:45












          mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
          – reverpie
          Nov 11 at 15:13






          mkelement0 is right about the efficiency: stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too
          – reverpie
          Nov 11 at 15:13














          I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
          – Michael Louth
          Nov 11 at 16:15




          I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example?
          – Michael Louth
          Nov 11 at 16:15












          mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
          – Michael Louth
          Nov 11 at 17:41




          mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop.
          – Michael Louth
          Nov 11 at 17:41


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241170%2fusing-a-function-to-populate-a-textbox-in-powershell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ