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

Winforms 使用powershell安装时显示自定义消息,winforms,powershell,Winforms,Powershell,我无法找出我所犯的错误。下面的脚本将调用一组批处理文件,当批处理文件在那里执行任务时,进度将与脚本名称一起显示在进度栏中。我想要实现的是在安装过程中显示自定义消息。我无法找出错误,非常感谢您的帮助 [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing"

我无法找出我所犯的错误。下面的脚本将调用一组批处理文件,当批处理文件在那里执行任务时,进度将与脚本名称一起显示在进度栏中。我想要实现的是在安装过程中显示自定义消息。我无法找出错误,非常感谢您的帮助

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

提前感谢。

您正在对邮件列表和显示邮件本身的标签重新使用相同的变量名:

$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
):


由于每一步都将
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
}
foreach ($script in $ScriptsHome) {
    $ProgressBar.Increment(1)
    $MessageLabel.Text = $Messages[$ProgressBar.Value - 1]
    $Label.Text     = "$($script.Name)"   
    Start-Process $script.FullName -Wait -WindowStyle Hidden
}