在Powershell命令窗口中从函数写入输出

时间:2019-07-10 19:17:03

标签: function powershell

我有一个GUI,它根据所按的按钮调用一个函数。我希望在运行GUI时在powershell命令窗口中显示该函数的输出。下面的代码包含5个按钮,当我运行powershell脚本并单击5个按钮中的任何一个时,什么都没有发生,它只是挂起,直到我关闭它为止。

    # 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 test2
        ping test3
        ping test4
        ping test5
    }
# 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 ▲

[void]$Form.ShowDialog()

1 个答案:

答案 0 :(得分:0)

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()