Share via


Write Output of a function in powershell window

Question

Wednesday, July 10, 2019 8:24 PM

I have a GUI and it calls a function depending on the button pressed. I would like for the output of the function to show in the powershell command window when I run the GUI. The code below contains 5 buttons, when I run the powershell script and click on any of the 5 buttons, nothing happens and it just hangs, until i close out of it.

# This is code for the GUI ▼
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '406,414'
$Form.text                       = "Post DC Patching Checker"
$Form.TopMost                    = $false

$Check_NetLogon                  = New-Object system.Windows.Forms.Button
$Check_NetLogon.text             = "Check Netlogon"
$Check_NetLogon.width            = 340
$Check_NetLogon.height           = 50
$Check_NetLogon.location         = New-Object System.Drawing.Point(15,17)
$Check_NetLogon.Font             = 'Microsoft Sans Serif,10'

$Ping                            = New-Object system.Windows.Forms.Button
$Ping.text                       = "Ping Servers / Workstations"
$Ping.width                      = 340
$Ping.height                     = 50
$Ping.location                   = New-Object System.Drawing.Point(16,97)
$Ping.Font                       = 'Microsoft Sans Serif,10'

$ShowReplication                 = New-Object system.Windows.Forms.Button
$ShowReplication.text            = "Show Replication"
$ShowReplication.width           = 340
$ShowReplication.height          = 50
$ShowReplication.location        = New-Object System.Drawing.Point(16,183)
$ShowReplication.Font            = 'Microsoft Sans Serif,10'

$DiskSpace                       = New-Object system.Windows.Forms.Button
$DiskSpace.text                  = "Disk Space"
$DiskSpace.width                 = 340
$DiskSpace.height                = 50
$DiskSpace.location              = New-Object System.Drawing.Point(15,267)
$DiskSpace.Font                  = 'Microsoft Sans Serif,10'

$CheckDNSsuffix                  = New-Object system.Windows.Forms.Button
$CheckDNSsuffix.text             = "Check IP Configuration"
$CheckDNSsuffix.width            = 340
$CheckDNSsuffix.height           = 50
$CheckDNSsuffix.location         = New-Object System.Drawing.Point(17,350)
$CheckDNSsuffix.Font             = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Check_NetLogon,$Ping,$ShowReplication,$DiskSpace,$CheckDNSsuffix))

$Check_NetLogon.Add_Click({ CheckNetLogon })
$Ping.Add_Click({ PingServersAndWorkstations })
$ShowReplication.Add_Click({ ShowReplicationOnServers })
$DiskSpace.Add_Click({ ShowDiskSpace })
$CheckDNSsuffix.Add_Click({ ShowIPconfig })
# This is code for the GUI ▲

# Check the netlogon service ▼
function CheckNetLogon { 
    $netLogon =Get-Service -DisplayName netlogon 
        if ($netLogon.Status -eq "Running"){
        $netLogon.DisplayName + 'Service is running already'}
    }
# Check the netlogon service ▲

# Ping's several workstations and servers ▼
function PingServersAndWorkstations {
        ping Test1
        ping Test2

    }
# Ping's several workstations and servers ▲

# Shows replication ▼
function ShowReplicationOnServers {
        repadmin /showrepl
    } 
# Shows replication ▲

# Shows disk space ▼
function ShowDiskSpace {
        Get-WmiObject -Class Win32_logicaldisk  | 
        Select-Object -Property DeviceID, DriveType, VolumeName, 
        @{L='FreeSpaceGB';E={"{0:N2}" -f ($_.FreeSpace /1GB)}}
    }
# Shows replication ▲

# Shows IP config ▼
function ShowIPconfig {
        ipconfig
   }
# Shows IP config ▲

Write-Output $ping

[void]$Form.ShowDialog()

All replies (4)

Wednesday, July 10, 2019 10:06 PM ✅Answered | 1 vote

This is as close as you can get.  Normally we output to a textbox in the form.

This is how to post code in a forum.

Add-Type -AssemblyName System.Windows.Forms

$formTest = New-Object system.Windows.Forms.Form
$formTest.Size = '406,414'
$formTest.StartPosition = 'CenterScreen'
$formTest.text = "Post DC Patching Checker"

$buttonPing = New-Object system.Windows.Forms.Button
$formTest.Controls.Add($buttonPing)
$buttonPing.text = "Ping Servers / Workstations"
$buttonPing.Size = '340,50'
$buttonPing.location = '16, 97'
$buttonPing.Font = 'Microsoft Sans Serif,10'
$buttonPing.Add_Click({
    $this.Enabled = $false
    'google.com', 'test1', 'test2' |
    ForEach-Object{
        Try{
        Test-Connection $_ -Count 1 -ErrorAction Stop| 
            Out-String | 
            Write-Host -ForegroundColor green
        }
        Catch{
            Write-Host $_ -fore Red
        }
    }
    $this.Enabled = $true
})
$formTest.ShowDialog()

\(ツ)_/


Wednesday, July 10, 2019 9:31 PM

https://stackoverflow.com/questions/56977291/write-output-from-a-function-in-powershell-command-window

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Wednesday, July 10, 2019 9:49 PM

You cannot display output while a GUI is displayed.   PowerShell does not allow this.  The closest you can come is to use Write-Host which will output to a console window while the GUI is displayed.

Please edit you original post and post the code using the code posting tool provided.  When posted as you have done the code cannot be correctly copied.  It is also hard to read in many browsers.

\(ツ)_/


Wednesday, July 10, 2019 11:41 PM

There is also a very simple GUI method to display results:

'google.com','xxxx','www.msn.com','omega' | 
      Test-NetConnection  | Out-GridView

\(ツ)_/