Windows 进度条powershell脚本没有';从批处理脚本调用时无法工作

Windows 进度条powershell脚本没有';从批处理脚本调用时无法工作,windows,powershell,batch-file,Windows,Powershell,Batch File,我面临的情况是,当我从PowershellISE运行我的powershell脚本时,它会按预期工作。但是,当从批处理文件调用同一脚本时,会写入带有progressbar函数的powershell脚本部分,它不会显示任何响应。我无法找出我错过了什么。 powershell脚本是: [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null [System.Reflection.Assemb

我面临的情况是,当我从PowershellISE运行我的powershell脚本时,它会按预期工作。但是,当从批处理文件调用同一脚本时,会写入带有progressbar函数的powershell脚本部分,它不会显示任何响应。我无法找出我错过了什么。 powershell脚本是:

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


#FUNCTION- progress bar
function progress-bar {
$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell = [System.Management.Automation.PowerShell]::Create()
$PowerShell.runspace = $Runspace
$Runspace.Open()

[void]$PowerShell.AddScript({

$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "text"
$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"

$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Maximum = 100
$ProgressBar.Minimum = 0
$ProgressBar.Location = new-object System.Drawing.Size(10,70)
$ProgressBar.size = new-object System.Drawing.Size(967,10)
$ProgressBar.style = 'Marquee'
$MessagesLabel = New-Object System.Windows.Forms.Label
$MessagesLabel.AutoSize = $true
$MessagesLabel.Location = New-Object System.Drawing.Point(10,45)
$MessagesLabel.Text = "message for user"

$ShownFormAction = {
    $Form.Activate()

   # $Form.Dispose()
}

$Form.Add_Shown($ShownFormAction)
$Form.Controls.Add($MessagesLabel)
$Form.Controls.Add($ProgressBar)

$Form.ShowDialog()

})
$PowerShell.BeginInvoke()
}
#begin main process
progress-bar
Get-Date
Start-Sleep 5
#end main process
我用来调用上述脚本的批处理文件包含:

echo off
SET HomeDir=%~dp0
SET PowerShellScriptPath=%HomeDir%
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%HomeDir%\1.ps1'";
exit

请使用任何指向解决方案的指针提供帮助。

尝试在批处理脚本中使用start命令,例如

start powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%HomeDir%\1.ps1'";

这将在一个新窗口中打开powershell进程并关闭其后面的批处理,希望确保您使用的任何功能都能正确显示。

不是100%确定,但我假设CMD没有能力显示进度条。它是windows保留向后兼容性的传统部分,PowerShell是作为其替代品开发的。为什么要通过批处理文件运行PS?有什么原因不直接运行脚本吗?powershell脚本适用于可以在桌面上运行脚本的用户。因此,他们只需双击批处理文件即可执行ps脚本。