从使用runas或作为计划任务打开的命令提示符中启动脚本时,启动过程-等待不起作用 [英] Start-Process -wait doesn't work when script is launched from command prompt opened with runas or as a scheduled task

查看:327
本文介绍了从使用runas或作为计划任务打开的命令提示符中启动脚本时,启动过程-等待不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,希望将其作为计划任务运行,但它没有执行应有的功能.我正在尝试使用Start-Process和-Wait开关调用可执行文件,然后再继续.线是

I've got a script that's I want to run as a scheduled task, but it's not doing the thing it's supposed to. I'm trying to call an executable with Start-Process and the -Wait switch before continuing. Line is

开始处理-FilePath"C:\ Pfx Engagement \ Admin \ Utilities \ Backup Restore \ BackupRestoreUtil.exe" -ArgumentList"/f "$backup_directory""-等待

如果我从命令提示符下调用它,即:

If I call it from a command prompt, ie:

powershell.\ script.ps1

powershell .\script.ps1

有效.它运行命令并等待其完成后再继续操作.该命令完成后,还需要运行脚本的更多内容.问题在于,当它是计划任务时,它不会等待.在进行一些基本的故障排除时,我首先尝试使用名为脚本"的计划任务帐户打开带有runas的cmd窗口.所以我跑

it works. It runs the command and waits for it to finish before moving on. There's more to the script that has to be run after that command is finished. The problem is that when it's a scheduled task, it doesn't wait. Doing some basic troubleshooting, I first tried opening a cmd window with runas using the scheduled task account, named "Scripts." So I run

runas/env/user:脚本cmd

runas /env /user:Scripts cmd

使用任务帐户打开命令提示符窗口.在该命令提示符下,我再次尝试"powershell.\ script.ps1",这一次,它不再等待.它运行命令并在命令完成之前立即继续.因此,我认为这可能是脚本"帐户的问题,直到我用runas Administrator打开新的命令提示符

to open a command prompt window with the task account. From that command prompt, I try again the "powershell .\script.ps1" and this time, it doesn't wait. It runs the command and moves on immediately before the command is finished. So I thought it might be an issue with the "Scripts" account, until I opened a new command prompt with runas Administrator

runas/env/user:管理员cmd

runas /env /user:Administrator cmd

当我从管理员命令提示符下调用脚本时,-Wait开关也将被忽略,并且脚本在调用后立即移动,而无需等待脚本结束.

When I call the script from this Administrator command prompt, the -Wait switch is also ignored, and the script moves along immediately after calling it without waiting for it to finish.

关于这一点的奇怪之处在于,当我从管理员帐户的命令提示符处调用它而不执行runas时,它可以工作.同一帐户,两个不同的结果.关于这里到底发生了什么的任何想法,以及同样重要的是,如何解决它?

The odd part about this is that when I call it from the command prompt from Administrator account without doing runas, it works. Same account, two different results. Any ideas as to what the hell is going on here, and equally importantly, how to fix it?

OS是运行Powershell 3.0的Server 2008 R2

OS is Server 2008 R2, running powershell 3.0

推荐答案

无法告诉您这样做的原因,但我认为这可能会解决此问题:

Can't tell you why it's doing it, but I think this might work around it:

$proc = Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory"" -Passthru
do {start-sleep -Milliseconds 500}
until ($proc.HasExited)

-Passthru开关将使其返回该流程的Process对象,您可以对其进行测试以查看该流程何时退出.

The -Passthru switch will make it return a Process object for the process, and you can test that to see when the process has exited.

这篇关于从使用runas或作为计划任务打开的命令提示符中启动脚本时,启动过程-等待不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆