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

时间:2017-10-11 08:57:16

标签: windows powershell batch-file

我正面临这样的情况:当我从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

请帮助解决任何解决方案。

1 个答案:

答案 0 :(得分:0)

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

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

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