【发布时间】:2017-12-10 19:07:07
【问题描述】:
Powershell 新手在这里。我正在尝试编写一个有用的工具,它有 3 个按钮和一个文本窗口。每个按钮调用一个不同的 powershell (.ps1) 脚本来执行各种操作。如果可以的话,我想将这些脚本的输出写入 GUI 文本窗口,但这似乎比人们想象的要难。如果这太多了,那么至少我想在文本窗口中显示脚本已经运行完成。类似“脚本已运行”。
这是我目前所拥有的。
Add-Type -AssemblyName System.Windows.Forms
function call_Clean
{
# Here the path to call your script
. "C:\Scripts\Script1.ps1"
}
function call_CreateTestLabConfig
{
# Here the path to call your script
. "C:\Scripts\Script2.ps1"
}
function call_LocalDeploy
{
# Here the path to call your script
. "C:\Scripts\Script3.ps1"
}
function CreateFormButton ( $locationheight, $locationwidth, $sizeheight, $sizewidth, $fieldname, $functionname ) {
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size($locationheight, $locationwidth)
$Button.Size = New-Object System.Drawing.Size($sizeheight, $sizewidth)
$Button.Text = $fieldname
$Button.Add_Click( $functionname )
$Form.Controls.Add($Button)
}
function CreateStartPosition ( $FormSize, $FormLocation ) {
$Form.Size = New-Object System.Drawing.Size ($varFrmMSizeWidth, $varFrmMSizeHeight)
$Form.Location = New-Object System.Drawing.Point($varFrmMLocationX, $varFrmMLocationY)
$Form.Controls.Add($TextWindow)
}
function CreateTextWindow ( $locationHeight, $LocationWidth, $TextBoxHeight, $TextBoxWidth ) {
$TextWindow = new-object System.Windows.Forms.ListView
$TextWindow.Size = New-Object System.Drawing.Size($textBoxHeight,$textBoxWidth)
$TextWindow.location = new-object system.drawing.point($locationHeight,$LocationWidth)
$Form.Controls.Add($TextWindow)
}
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Test Lab Tool"
#$Form.TopMost = $true
$Form.Size = New-Object System.Drawing.Size (475, 600)
$Form.Location = New-Object System.Drawing.Point(4000, 300)
CreateFormButton 20 100 120 40 'Clean' ${function:call_Clean}
CreateFormButton 170 100 120 40 'Create TestLab Config' ${function:call_CreateTestLabConfig}
CreateFormButton 315 100 120 40 'Local Deploy'
CreateTextWindow 20 160 415 375 'Test'
$Form.ShowDialog()
【问题讨论】:
-
替换你的函数来做
& "Script.ps1"。所有 dot-sourcing 所做的就是将脚本的内容转储到这些功能块中。 -
这对输出到文本窗口有帮助吗?
-
它将帮助您的脚本运行。除非您重定向或捕获输出,否则这些脚本的输出将转到输出流。
标签: powershell user-interface scripting automation