我正面临这种情况,当我从PowershellISE运行我的PowerShell脚本,它的工作原理。 但是当从一个batch file中调用相同的脚本时,带有进度条function的部分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
和我用来调用这个脚本的batch file包含:
echo off SET HomeDir=%~dp0 SET PowerShellScriptPath=%HomeDir% PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%HomeDir%\1.ps1'"; exit
请帮助任何指向解决scheme。