display data

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 6 years and 4 weeks 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
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

display data

Post by mqh77777 »

Product, version and build: 5.5.149
32 or 64 bit version of product: 64
Operating system: Windows 10 & 7
32 or 64 bit OS: Both

Hi. The following code works. It reads the local administrators group and then displays the groups in the RichText field. My issue is it displays the groups like this:
Group1 Group2 Group3. How do I make it display like this?

Group1
Group2
Group3

$buttonShowLocalAdmin_Click = {

$Computer1 = $PCNameBox.Text
$ADSIComputer = [ADSI]("WinNT://$Computer1,computer")
$group = $ADSIComputer.psbase.children.find('Administrators', 'Group')
$ShowMembers = $group.psbase.invoke("members") | ForEach{
$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
}
$ShowMembers | FT | Out-String
$richtextbox_Output.AppendText($ShowMembers)
}
cody m

Re: display data

Post by cody m »

[TOPIC MOVED BY MODERATOR]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: display data

Post by jvierra »

Use "Lines" for a list.

Code: Select all

$buttonShowLocalAdmin_Click = {
    $ADSIComputer = [ADSI]('WinNT://' + $PCNameBox.Text)
    $group = $ADSIComputer.psbase.children.find('Administrators', 'Group')
    $members = $group.psbase.invoke('members') | 
        ForEach{
            $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null) 
        }
    $richtextbox_Output.Lines = $members
}
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: display data

Post by mqh77777 »

thanks. That did work. I then tried the same trick on another button and it does not work. This does 2 things wrong. First, it displays the results all on one line. Second, it displays the results in black, not blue.

$buttonADGroupsMachineIsIn_Click={

$statusbar1.text = 'The query of active directory can take time, please wait.'
$richtextbox_Output.Clear()
$richtextbox_Output.SelectionColor = 'Blue'
$textPC = $PCNameBox.Text

$GetResults = Get-ADComputer $textPC -Properties * | Select-Object -ExpandProperty memberof
$GetResults | Out-String
$richtextbox_Output.Lines = $GetResults
$statusbar1.text = 'All done!'
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: display data

Post by jvierra »

Why do you think you have to turn everything into a string. "Lines" requires an array of strings. "Out-String" removes the array.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: display data

Post by jvierra »

Less code is always a better place to start. Try not to code by superstition. Write only code lines that you understand.

Example:

Code: Select all

$buttonADGroupsMachineIsIn_Click={
      $statusbar1.text = 'The query of active directory can take time, please wait.'
      $richtextbox_Output.SelectionColor = 'Blue'
      $richtextbox_Output.Lines = (Get-ADComputer $PCNameBox.Text -Properties memberof).memberof 
      $statusbar1.text = 'All done!'
}
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: display data

Post by mqh77777 »

Thanks. Your code still places everything onto 1 line and it is all black. and this actually ties into another issue we've been dealing with. We write all sorts of stuff to the RichText box. We make everything blue since it seems to look cleaner. When we dump a lot of data into the RichTextBox the first 40 or 50 lines are blue and then it changes to black.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: display data

Post by jvierra »

Well you will need to spend some time researching how to use an RTB. You can just add RTF lines to the box and format the color and typeface as well as many other things.

If you always want blue text then use a textbox and set the color to blue.

I see no issues with the code I posted. Using Out-String or Format-Table will break the textbox or RTB because booth output strings and not arrays of lines.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: display data

Post by jvierra »

Here is a sampler of methods for using an RTB:
Attachments
Demo-RTFColors.psf
(28.47 KiB) Downloaded 173 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: display data

Post by jvierra »

Note also that "Multiline" must be set to "True"

Here is a slightly better sample that shows how to set the color globally.
Attachments
Demo-RTFColors.psf
(29.12 KiB) Downloaded 135 times
This topic is 6 years and 4 weeks 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