使用PowerShell安装时显示自定义消息

时间:2016-08-29 10:10:18

标签: winforms powershell

我无法弄清楚我正在做的错误。以下脚本将调用一堆批处理文件,当批处理文件正在执行该作业时,进度将显示在进度条中以及名称剧本。我想要实现的是在安装过程中显示自定义消息。我无法找出错误,任何帮助都深表赞赏。

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

Set-Location $PSScriptRoot

#Call scripts for installation
$ScriptsHome = Get-Item '.\test\forPS\*'

#Define Form
# Init Form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "** Installation in Progress-PLEASE DO NOT CLOSE THIS WINDOW**"
$Form.Font = New-Object System.Drawing.Font("Times New Roman" ,12, [System.Drawing.FontStyle]::Regular)
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.StartPosition = "CenterScreen"
$Form.Opacity = .8
$Form.BackColor = "Gray"

#Define ICON for Form
$Icon = New-Object System.Drawing.Graphics (".\ICON.jpg")
$Form.Icon = $Icon

# Init ProgressBar
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Maximum = $ScriptsHome.Count
$ProgressBar.Minimum = 0
$ProgressBar.Location = new-object System.Drawing.Size(10,70)
$ProgressBar.size = new-object System.Drawing.Size(967,10)
$Form.Controls.Add($ProgressBar)
$Form.Controls.Add($Messages)

#Running Script Name
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Point(10,50) 
$Form.Controls.Add($Label)

#Define Array messages
#Array
$Messages = @("Preparing to install patch set..Preparing to stop all related processes",
                "Upgrading the application",
                "Copying the missing folder",
                "Applying the patch",
                "Starting all previously stopped Services",
                "Checkcing healthyness of the system after the installation this is may take up to half an hour...",
                "Rebooting Server"
                )
$Messages = New-Object System.Windows.Forms.Label
$Messages.AutoSize = $true
$Messages.Location = New-Object System.Drawing.Point(10,50)
$Form.Controls.Add($Messages)


# Add_Shown action    
$ShownFormAction = {
    $Form.Activate()

    foreach ($script in $ScriptsHome) {
        $ProgressBar.Increment(1)
        #$Messages.Text = $Messages[$Messages]
        $Label.Text     = "$($script.Name)"   
        Start-Process $script.FullName -Wait -WindowStyle Hidden
    }
    $Form.Dispose()
}
$Form.Add_Shown($ShownFormAction)

# Show Form
$Form.ShowDialog()

提前致谢。

1 个答案:

答案 0 :(得分:3)

您正在为消息列表和显示消息本身的标签重复使用相同的变量名称:

$Messages = @("Preparing to install patch set..Preparing to stop all related processes",
                "Upgrading the application",
                "Copying the missing folder",
                "Applying the patch",
                "Starting all previously stopped Services",
                "Checkcing healthyness of the system after the installation this is may take up to half an hour...",
                "Rebooting Server"
                )
$Messages = New-Object System.Windows.Forms.Label
$Messages.AutoSize = $true
$Messages.Location = New-Object System.Drawing.Point(10,50)

重命名其中一个(即使用$MessageLabel作为标签):

$MessageLabel = New-Object System.Windows.Forms.Label
$MessageLabel.AutoSize = $true
$MessageLabel.Location = New-Object System.Drawing.Point(10,50)

由于每步都将ProgressBar递增1,您可以重复使用进度条值来索引$Messages数组:

foreach ($script in $ScriptsHome) {
    $ProgressBar.Increment(1)
    $MessageLabel.Text = $Messages[$ProgressBar.Value - 1]
    $Label.Text     = "$($script.Name)"   
    Start-Process $script.FullName -Wait -WindowStyle Hidden
}