Removing Blank Lines from Multi-Line Textbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mitchell_loftis
Posts: 4
Last visit: Mon Sep 18, 2023 2:44 pm

Removing Blank Lines from Multi-Line Textbox

Post by mitchell_loftis »

Good Afternoon All,

I'm trying to build a tool that will show what type of device builds are in our AD. The GUI has a multiline textbox for users to bring in the hostnames. If the hostnames list have no blank lines or an extra line at the end, the script works as expected. What I would like to have is if users copied and pasted into the textbox, and if there were these extra spaces, they would be removed before the script would do the ForEach loop. Currently, the spaces don't cause any issues, it just looks tacky in the output.

Secondly, how can I pipe my results to a datagridview in the GUI? I would like 2 columns "Hostname" and "SSO Build Type"

Here is my ButtonOK_Click script:
  1. $buttonOK_Click= {
  2.     #TODO: Place custom script here
  3.     $Computers = $Hostnames.Lines  
  4.    
  5.     $Results = foreach ($one in $computers)
  6.     {
  7.         $one = $one.ToUpper()
  8.         $OU = Get-ADComputer $one | select -ExpandProperty DistinguishedName
  9.        
  10.         If ($OU -like "*OU=EXAM01*")
  11.         {
  12.             $BuildType = "EXAM01"
  13.         }
  14.         ElseIf ($OU -like "*OU=INP01*")
  15.         {
  16.             $BuildType = "INP01"
  17.         }
  18.         ElseIf ($OU -like "*OU=PREOP01*")
  19.         {
  20.             $BuildType = "PREOP01"
  21.         }
  22.         ElseIf ($OU -like "*OU=ORO1*")
  23.         {
  24.             $BuildType = "ORO1"
  25.         }
  26.         Else
  27.         {
  28.             $BuildType = "NOT SSO"
  29.         }
  30.        
  31.         $hash = @{
  32.             "SSO BUILD TYPE" = $BuildType
  33.             HOSTNAME = $one
  34.         }
  35.        
  36.         New-Object -TypeName System.Management.Automation.PSObject -Property $hash
  37.     }
  38.    
  39.     $Results | Out-GridView
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Removing Blank Lines from Multi-Line Textbox

Post by jvierra »

Start with this:

$list = $textbox.Lines | Where{$_} | ForEach{ $_.Trim() }
User avatar
mitchell_loftis
Posts: 4
Last visit: Mon Sep 18, 2023 2:44 pm

Re: Removing Blank Lines from Multi-Line Textbox

Post by mitchell_loftis »

Wow! That worked, now how would I pipe my results into a DataGridView Table?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Removing Blank Lines from Multi-Line Textbox

Post by jvierra »

It is just an array of text. Why put it in a DGV? Just display it in a textbox.

If you really wish to us a DGV then just add to the DGV one line at a time.
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked