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.
Locked
User avatar
mloftis
Posts: 3
Joined: Tue Jan 30, 2018 9:41 am

Removing Blank Lines from Multi-Line Textbox

Post by mloftis » Wed May 08, 2019 11:38 am

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.  
  3.     #TODO: Place custom script here
  4.  
  5.     $Computers = $Hostnames.Lines  
  6.  
  7.    
  8.  
  9.     $Results = foreach ($one in $computers)
  10.  
  11.     {
  12.  
  13.         $one = $one.ToUpper()
  14.  
  15.         $OU = Get-ADComputer $one | select -ExpandProperty DistinguishedName
  16.  
  17.        
  18.  
  19.         If ($OU -like "*OU=EXAM01*")
  20.  
  21.         {
  22.  
  23.             $BuildType = "EXAM01"
  24.  
  25.         }
  26.  
  27.         ElseIf ($OU -like "*OU=INP01*")
  28.  
  29.         {
  30.  
  31.             $BuildType = "INP01"
  32.  
  33.         }
  34.  
  35.         ElseIf ($OU -like "*OU=PREOP01*")
  36.  
  37.         {
  38.  
  39.             $BuildType = "PREOP01"
  40.  
  41.         }
  42.  
  43.         ElseIf ($OU -like "*OU=ORO1*")
  44.  
  45.         {
  46.  
  47.             $BuildType = "ORO1"
  48.  
  49.         }
  50.  
  51.         Else
  52.  
  53.         {
  54.  
  55.             $BuildType = "NOT SSO"
  56.  
  57.         }
  58.  
  59.        
  60.  
  61.         $hash = @{
  62.  
  63.             "SSO BUILD TYPE" = $BuildType
  64.  
  65.             HOSTNAME = $one
  66.  
  67.         }
  68.  
  69.        
  70.  
  71.         New-Object -TypeName System.Management.Automation.PSObject -Property $hash
  72.  
  73.     }
  74.  
  75.    
  76.  
  77.     $Results | Out-GridView

jvierra
Posts: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Removing Blank Lines from Multi-Line Textbox

Post by jvierra » Wed May 08, 2019 11:54 am

Start with this:

$list = $textbox.Lines | Where{$_} | ForEach{ $_.Trim() }

User avatar
mloftis
Posts: 3
Joined: Tue Jan 30, 2018 9:41 am

Re: Removing Blank Lines from Multi-Line Textbox

Post by mloftis » Thu May 09, 2019 5:54 am

Wow! That worked, now how would I pipe my results into a DataGridView Table?

jvierra
Posts: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Removing Blank Lines from Multi-Line Textbox

Post by jvierra » Thu May 09, 2019 7:05 am

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.

Locked