Invoke-Command and RichTextBox

Welcome Forums General PowerShell Q&A Invoke-Command and RichTextBox

This topic contains 4 replies, has 4 voices, and was last updated by

cm
 
Participant
3 weeks, 3 days ago.

  • Author
    Posts
  • #114456
    cm

    Participant
    Points: 2
    Rank: Member

    How would I get the following to display to RichTextBox?

    $Servers = Get-ADComputer -Filter * SearchBase "OU=IT,DC=Contoso,DC=Com" | Select-Object -ExpandProperty Name

    Invoke-Command -ComputerName $Server.name -ScriptBlock { Get-ItemProperty "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } | select PSComputerName, DisplayVersion | where ( $_.DisplayName -match "Symantec Endpoint Encryption" } | Out-String

    I had it in a foreach loop before realizing it wasn't necessary.

  • #114546
    Kaj

    Participant
    Points: 1
    Rank: Member
  • #114555

    Participant
    Points: 519
    Helping Hand
    Rank: Major Contributor

    @cm is this question a continuation of

    https://powershell.org/forums/topic/script-works-in-poweshell-but-not-ps-studio/

    ?

    You could have asked for an update in the same thread.

    You can save the output of Invoke-Command to a variable and use the variable to set the richtextbox.

    Do you have any code that you have already tried to add it to the RichTextBox ?

    • #114588
      cm

      Participant
      Points: 2
      Rank: Member

      @kevprassoon Hello. I have already tried trapping the invoke-command in a variable and then outputting to a richtextbox. When I run the script nothing will display.

      I did $Results = Invoke-Command... and then

      Richtextbox_output.text = $Results

      As an alternative or work around I can search and see if the service is running with Get-Service.

  • #114558

    Participant
    Points: 17
    Rank: Member

    Lot of information missing here such as what you're currently working with in your GUI script.  Here's a crude example that shows how to make a button click run a command and output the text into a rich textbox.

     

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    #region begin GUI{
    
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '800,400'
    $Form.text                       = "Form"
    $Form.TopMost                    = $false
    
    $TextBox1                        = New-Object System.Windows.Forms.RichTextBox
    $TextBox1.multiline              = $true
    $TextBox1.width                  = 706
    $TextBox1.height                 = 206
    $TextBox1.location               = New-Object System.Drawing.Point(40,45)
    $TextBox1.Font                   = 'Lucida Console,10'
    
    $Button1                         = New-Object system.Windows.Forms.Button
    $Button1.text                    = "Go"
    $Button1.width                   = 60
    $Button1.height                  = 30
    $Button1.location                = New-Object System.Drawing.Point(37,280)
    $Button1.Font                    = 'Microsoft Sans Serif,10'
    
    $Form.controls.AddRange(@($TextBox1,$Button1))
    
    
    
    #Write your logic code here
    $Button1.Add_Click({$TextBox1.text = "Running your ScriptBlock..."
    		            $Servers = Get-ADComputer -Filter * SearchBase "OU=IT,DC=Contoso,DC=Com" | Select-Object -ExpandProperty Name
    			        $result = Invoke-Command -ComputerName $Server.name -ScriptBlock { Get-ItemProperty "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } | select PSComputerName, DisplayVersion | where ( $_.DisplayName -match "Symantec Endpoint Encryption" } | Out-String
    		            $TextBox1.text = $result
    		          })
    
    #
    [void]$Form.ShowDialog()
    
    

     

You must be logged in to reply to this topic.