首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在powershell命令窗口中写入函数的输出

在powershell命令窗口中写入函数的输出
EN

Stack Overflow用户
提问于 2019-07-10 19:17:03
回答 1查看 669关注 0票数 0

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

代码语言:javascript
复制
    # 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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-16 21:23:49

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56977291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档