将 schtasks.exe 输出传递到 PowerShell 中的 foreach 循环
我正在使用 powershell5.1,我试图获取远程服务器上运行的一些任务,看看是否有任何任务正在运行。如果有任何正在运行的任务,那么我将使脚本休眠一分钟,然后再次检查,直到我正在检查的所有任务都处于“就绪”状态。
我用什么来查询远程服务器:
$servers = "Server1"
""
foreach($server in $servers)
{
Invoke-Command -ComputerName $server -ScriptBlock{
schtasks /query /fo list /tn RandomTaskName
}
}
然后输出这个结果:
HostName: $server
TaskName: \RandomTaskName
Next Run Time: 3/1/2022 11:40:00 AM
Status: Running
Logon Mode: Interactive/Background
所以我向 FINDSTR“状态:正在运行”添加一个管道,
schtasks /query /fo list /tn RandomTaskName | FIDNSTR "Status: Running"
它只返回字符串。 然后我尝试将输出添加到变量并执行 foreach 循环以查看字符串是否包含“Status: Running”,
$servers = "Server1"
foreach($server in $servers)
{
Invoke-Command -ComputerName $provider -ScriptBlock{
schtasks /query /fo list /tn RandomTaskName
}
$task = $_
if ("Status: Running" -ccontains $task)
{
Wrtite-host "Task is still running"
}
else
{
Write-Host "Task is NOT running"
}
}
这不会产生任何结果。当我注释掉“if”和“else”语句时,只有“$task=$_”位于末尾,然后它会导致“状态:正在运行”。
任何人都可以提供有关如何获取远程服务器列表的运行状态的任何见解?
I am using powershell5.1 and I am trying to grab a few tasks running on a remote server and see if any are running. IF there are any running then I will have the script sleep for a minute and then check again until all the tasks I am checking are in Ready status.
What I use to query the remote server:
$servers = "Server1"
""
foreach($server in $servers)
{
Invoke-Command -ComputerName $server -ScriptBlock{
schtasks /query /fo list /tn RandomTaskName
}
}
Which then outputs this result:
HostName: $server
TaskName: \RandomTaskName
Next Run Time: 3/1/2022 11:40:00 AM
Status: Running
Logon Mode: Interactive/Background
So I add a pipe to FINDSTR "Status: Running"
schtasks /query /fo list /tn RandomTaskName | FIDNSTR "Status: Running"
That returns just the string.
Then I try to add the output to a variable and do a foreach loop to see if a string contains "Status: Running"
$servers = "Server1"
foreach($server in $servers)
{
Invoke-Command -ComputerName $provider -ScriptBlock{
schtasks /query /fo list /tn RandomTaskName
}
$task = $_
if ("Status: Running" -ccontains $task)
{
Wrtite-host "Task is still running"
}
else
{
Write-Host "Task is NOT running"
}
}
Which results in nothing. When I comment out the "if" and "else" statements so only "$task=$_" is at the end, then it results in "Status: Running".
Anyone can give any insight as to how to grab the running status for a list of remote servers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我看到这个问题的时候,我的好奇心真的达到了顶峰。我更多地使用工作站而不是服务器,但一切都应该大致相同。
这完全是我解决自己问题的方法,因此可能不适合您的需求 - 抱歉。但也许我的代码可以给你一些关于如何做你想做的事情的想法。
此代码:
使用示例:
管道上返回的示例结果:
When I saw this question, it really peaked my curiosity. I'm working more with workstations than servers, but everything should be about the same.
This is entirely my approach to solve my own problems, so it may not fit your needs - sorry. But perhaps my code can give you some ideas on how to do what you want.
This code:
Example use:
Example results returned on the Pipeline: