从使用runas打开的命令提示符或作为计划任务启动脚本时,Start-Process -wait不起作用

时间:2022-08-03 02:32:03

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

我有一个脚本,我想作为一个预定的任务运行,但它没有做它应该做的事情。我正在尝试使用Start-Process和-Wait开关调用可执行文件,然后再继续。线是

Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory"" -Wait

Start-Process -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

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

有用。它运行命令并在继续之前等待它完成。该命令完成后必须运行的脚本还有更多内容。问题是当它是一个计划任务时,它不会等待。做一些基本的故障排除,我首先尝试使用名为“Scripts”的计划任务帐户打开带有runas的cmd窗口。所以我跑了

runas /env /user:Scripts cmd

runas / env / user:脚本cmd

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

使用任务帐户打开命令提示符窗口。从那个命令提示符,我再次尝试“powershell。\ script.ps1”,这一次,它不会等待。它运行命令并在命令完成之前立即继续运行。所以我认为这可能是“Scripts”帐户的一个问题,直到我用runas Administrator打开一个新的命令提示符

runas /env /user:Administrator cmd

runas / env / user:Administrator cmd

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.

当我从此管理员命令提示符调用脚本时,也会忽略-Wait开关,并且在调用脚本后立即移动脚本而不等待它完成。

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?

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

OS is Server 2008 R2, running powershell 3.0

操作系统是Server 2008 R2,运行PowerShell 3.0

1 个解决方案

#1


8  

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)

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.

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

#1


8  

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)

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.

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