我正面临这样的情况,当我从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
请帮助任何指向解决方案的指针。
发布于 2018-06-18 14:25:08
尝试在批处理脚本中使用start命令,例如
start powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%HomeDir%\1.ps1'";
这将在一个新窗口中打开powershell进程,并关闭它后面的批处理,希望能确保您正在使用的任何功能都能正确显示。
https://stackoverflow.com/questions/46683991
复制