Trouble Converting a multi-line string to a single line

Ask your PowerShell-related questions, including questions on cmdlet development!
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 1 year and 2 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
EZSecurity
Posts: 3
Last visit: Wed Dec 20, 2023 4:58 pm

Trouble Converting a multi-line string to a single line

Post by EZSecurity »

  1. #region Import the Assemblies
  2.     #----------------------------------------------
  3.     [void][reflection.assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  4.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  5.     #endregion Import Assemblies
  6.  
  7.     #----------------------------------------------
  8.     #region Generated Form Objects
  9.     #----------------------------------------------
  10.     [System.Windows.Forms.Application]::EnableVisualStyles()
  11.     $formChildForm = New-Object 'System.Windows.Forms.Form'
  12.     $treeview1 = New-Object 'System.Windows.Forms.TreeView'
  13.     $buttonOK = New-Object 'System.Windows.Forms.Button'
  14.     $buttonCancel = New-Object 'System.Windows.Forms.Button'
  15.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  16.     #endregion Generated Form Objects
  17.  
  18.     #----------------------------------------------
  19.     # User Generated Script
  20.     #----------------------------------------------
  21.    
  22.    
  23.     $formChildForm_Load={
  24.         #TODO: Initialize Form Controls here
  25.        
  26.     }
  27.    
  28.    
  29.     #region Control Helper Functions
  30.     function Get-CheckedNode
  31.     {
  32.         param (
  33.                 [ValidateNotNull()]
  34.                 [System.Windows.Forms.TreeNodeCollection]$NodeCollection,
  35.                 [ValidateNotNull()]
  36.                 [System.Collections.ArrayList]$CheckedNodes
  37.         )
  38.        
  39.         foreach ($Node in $NodeCollection)
  40.         {
  41.             if ($Node.Checked)
  42.             {
  43.                 [void]$CheckedNodes.Add($Node)
  44.             }
  45.             Get-CheckedNode $Node.Nodes $CheckedNodes
  46.         }
  47.     }
  48.    
  49.    
  50.     #endregion
  51.    
  52.     $buttonOK_Click={
  53.         #TODO: Place custom script here
  54.        
  55.         $CheckedNodes = New-Object System.Collections.ArrayList
  56.         Get-CheckedNode $treeview1.Nodes $CheckedNodes
  57.         foreach ($node in $CheckedNodes)
  58.         {
  59.             $Notif = $node.Name
  60.             $Notif = $Notif.Replace("`r`n", ",")
  61.             Write-Host $Notif
  62.         }
  63.     }
I'm new to Powershell so please bear with me.
I have here a Tree form with checkboxes. I'm trying to make it so that after you click OK, the Name of each checked node is added to one variable, with each value separated by a comma. This variable is going to be used later on in an Invoke-Sqlcmd command. My problem is that I can't seem to get all the Names in the variable to stay on the same line and also be separated by a comma. I've tried .Replace("`r`n", ",") and -join ',' and a bunch of other stuff and nothing seems to work.
Attachments
Script.ps1
(72.82 KiB) Downloaded 41 times
Powershell Studio Project.zip
(42.6 KiB) Downloaded 41 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trouble Converting a multi-line string to a single line

Post by jvierra »

To turn an array into a list do the following:

$array -join '.'

This will separate each element of the array into a comma separated list.

I recommend taking some time to learn how to work with strings in PowerShell. The following free book will help you learn PowerShell basics.

https://www.sapien.com/books_training/W ... werShell-4
EZSecurity
Posts: 3
Last visit: Wed Dec 20, 2023 4:58 pm

Re: Trouble Converting a multi-line string to a single line

Post by EZSecurity »

Thank you, and I will definitely read that. However, I've already tried $array -join ',' and nothing changed.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trouble Converting a multi-line string to a single line

Post by jvierra »

Then you likely don't have an array.
EZSecurity
Posts: 3
Last visit: Wed Dec 20, 2023 4:58 pm

Re: Trouble Converting a multi-line string to a single line

Post by EZSecurity »

I figured out a way to do it. Thank you for all the help.
  1. foreach ($node in $CheckedNodes) { $Notif = $Notif + "," + $Node.Name }
  2.    
  3.     $Notif = $Notif.TrimStart(",")
  4.     Write-Host $Notif
This topic is 1 year and 2 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