开发者

Windows powershell script triggered by PSExec is not killing Powershell process when it finishes running

开发者 https://www.devze.com 2023-01-21 04:57 出处:网络
I need to complete a series of tasks across several Windows 2008 servers that need elevated permissions and as such I\'ve had to create a series of scheduled tasks that i\'m running via psexec. Since

I need to complete a series of tasks across several Windows 2008 servers that need elevated permissions and as such I've had to create a series of scheduled tasks that i'm running via psexec. Since they have to run in sequence, I found and modified a powershell script that 'stalls' until scheduled tasks are completed on remote machines. The problem I have is that when I launch the script with psexec on the remote machine, once it completes running (indicated by a message in the console output) PowerShell.exe doesn't exit cleanly, but rather hangs out and holds up the ent开发者_运维知识库ire process. I need powershell to quit after the delay script exits, but even with an exit keyword at the end it stays in memory and prevents the process from finishing. I'm not terribly experienced with powershell so I'll attach my script in case I'm doing something foolish in it:

while($true) {
$status = schtasks /query /tn "AutoDeploy"| select-string -patt "AutoDeploy"
if($status.tostring().substring(64,7) -eq "Running") { "Waiting for task to complete..." } else { break }
start-sleep -s 5
}
"Task complete."
exit

Thanks in advance for any insight.


This works for me (using a different task name) and doesn't hang psexec:

$taskName = "AutoDeploy"
while (1)
{
    $stat = schtasks /query /tn $taskName | 
                Select-String "$taskName.*?\s(\w+)\s*$" | 
                Foreach {$_.Matches[0].Groups[1].value}
    if ($stat -ne 'Running')
    {
        "Task completed"
        break
    }
    "Waiting for task to complete"
    Start-Sleep 5
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号