windows - 从批处理脚本调用时,进度条 powershell 脚本不起作用

标签 windows powershell batch-file

我面临这种情况,当我从 PowershellISE 运行我的 powershell 脚本时,它会按预期工作。但是当从批处理文件调用相同的脚本时,编写了带有进度条函数的 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 powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%HomeDir%\1.ps1'";

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

关于windows - 从批处理脚本调用时,进度条 powershell 脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683991/

相关文章:

azure - 用于在 blob 容器内创建子目录并向其中插入一些数据的 PowerShell 脚本

windows - VBS : Check IP address and open URL (but only once)

windows - 在 Windows 上使用 FFMPEG 从图像流式传输视频

windows - 为什么 WaitForSingleObject 会返回 WAIT_FAILED

c++ - 语法错误 : missing ';' before identifier 'PVOID64' when compiling winnt. h

xml - 比较两个文件列表及其内容

bash - 无法在 Azure Powershell 中键入密码

variables - 可变批处理循环问题

windows - 在批处理文件中使用 "If exist/go to"

for-loop - 带有填充数字的/L 循环