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

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

提前致谢。

最佳答案

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

$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
}

关于winforms - 使用 powershell 安装时显示自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39203685/

相关文章:

接受正则表达式组的前瞻部分

Powershell move 项目访问被拒绝

powershell - 检查用户是否是远程服务器上本地管理员组的成员

c# - 如何在 C# 中的不同线程上运行新窗体?

powershell - 如何使用powershell更改文件内容并将其保存到另一个文件?

.net - 所有者绘制的 tabcontrol 具有更宽的选项卡

c# - 如何使用 Vlc.DotNet 在 Windows Forms c# 中流式传输 RTSP 摄像头源

powershell - 未引用的关键规则和最佳实践

c# - c#本地存储 "remember me"信息(总计新手)

c# - DataGridView 最后一列不能调整得更宽