使用状态

时间:2017-03-27 07:02:32

标签: powershell powershell-v4.0

我正在尝试使用下面返回的以下状态。

TaskName                                 Next Run Time          Status         
======================================== ====================== ===============
Avl3AutomationExecutor                   N/A                    Ready          

这是通过运行以下命令生成的: schtasks.exe / Query / S $ computer / TN \ TaskX

我想要做的是下面的内容,如果状态返回为“运行”,则PowerShell脚本将等待状态恢复为准备再次执行计划任务。

#Computer List:
$computers = "ComputerA"


foreach ($computer in $computers) {
    if (test-Connection -Cn $computer -quiet)
        {
         $CheckRunning = schtasks.exe /Query /S $computer /TN \TaskX
        While ($CheckRunning Stauts = Running)
        {
        Start-Sleep 30
        }
        else
        {
        Schtasks /run /tn \TaskX /s $computer  /u Username  /p Password
         }

else
        {
        }

1 个答案:

答案 0 :(得分:1)

试试这个,稍微修改一下你的代码:

$computers = "ComputerA"
foreach ($computer in $computers) {
    if (test-Connection -Cn $computer -quiet)
        {
            $CheckRunning = schtasks.exe /Query /FO csv /S $computer /TN "\TaskX" | ConvertFrom-Csv
            While ($CheckRunning.Status -eq "Ready")
                {
                    Start-Sleep 30
                    $CheckRunning = schtasks.exe /Query /FO csv /S $computer /TN "\TaskX" | ConvertFrom-Csv
                }
        else
                {
                Schtasks /run /tn \TaskX /s $computer  /u Username  /p Password
                }

        }
 }