Powershell 脚本卡住,从批处理文件调用时不退出 [英] Powershell script gets stuck, doesn't exit when called from batch file
问题描述
我有一个连接到网站的 PowerShell 脚本,并解析其返回的数据(这是关于将先前上传的 SQL 文件导入网站的数据库).PowerShell 脚本使用 wget
,稍后我可能会用本机函数替换它.
I have a PowerShell script that connects to a web site, and parses its returned data (It's about importing a previously uploaded SQL file into the web site's data base). The PowerShell script uses wget
, something I may replace by a native function later.
导入过程嵌入在由名为 scriptFTP 的第 3 方程序执行的脚本中.
The import process is embedded in a script that is executed by a 3rd party program called scriptFTP.
当我像这样从单个 .bat 文件调用脚本时,脚本运行良好:
The script runs fine when I call it from a single .bat file like so:
powershell "& "C:\data\etc\run_import_script.ps1"
exit %ERRORLEVEL%
但是,当我从更大的 ScriptFTP 上下文中调用此 .bat 文件时,会发生以下情况:
However, when I call this .bat file from within the greater ScriptFTP context, the following happens:
- PowerShell 脚本被执行.每次调用远程导入脚本时,我都会向自己发送一封电子邮件,确认这一点.
- PowerShell 似乎没有退出,脚本执行被卡住.我仍然可以使用 Ctrl+C 取消整个操作,但以下命令永远不会执行.
- The PowerShell script is executed. I confirmed this my sending myself an E-Mail each time the remote import script got called.
- PowerShell doesn't seem to exit, and script execution gets stuck. I can still cancel the whole thing using Ctrl+C but the following commands never get executed.
当我将批处理文件更改为以下内容时:
When I change the batch file to the following:
start powershell "& "C:\data\etc\run_import_script.ps1"
exit %ERRORLEVEL%
它工作,在新控制台中运行 PowerShell 脚本,但我无法获取 PowerShell 返回的错误级别.
it works, running the PowerShell script in a new console, but I can't grab the error level that PowerShell returns.
我尝试直接从 ScriptFTP 调用 PowerShell,绕过批处理文件,但结果相同:它只是卡住了.
I have tried calling PowerShell from ScriptFTP directly, bypassing the batch file, but with the same result: It just gets stuck.
我使用 Write-Output
或 Write-Host
使用 PowerShell 脚本执行的任何输出都不会显示.
Any output I have the PowerShell script do using Write-Output
or Write-Host
is not displayed.
所有程序都在同一个用户下运行,我.
All programs run under the same user, me.
有人知道该怎么做吗?
推荐答案
尝试添加/WAIT 参数.它将保持 .bat 等待 PowerShell 脚本完成.
Try adding the /WAIT parameter. It will keep the .bat waiting until the PowerShell script completes.
START /WAIT powershell "& "C:\data\etc\run_import_script.ps1"
这篇关于Powershell 脚本卡住,从批处理文件调用时不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!