Get-ScheduledTask 命令在 Windows Server 2008 R2 中不起作用 [英] Get-ScheduledTask command does not work in Windows Server 2008 R2

查看:62
本文介绍了Get-ScheduledTask 命令在 Windows Server 2008 R2 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找名称中具有特定模式的计划任务,在我的情况下,计划任务的名称中应包含此模式ServiceNow".为此,我使用 Get-ScheduledTask 命令,但此命令在 Windows Server 2008 R2 中失败.

I am trying to find a scheduled task with having certain pattern in it's name, in my case the scheduled task should have this pattern "ServiceNow" in its name. For this I am using Get-ScheduledTask command, but this command fails in Windows Server 2008 R2.

$taskPattern = "ServiceNow"
$taskDetails = Get-ScheduledTask | Where-Object {$_.TaskName -match $taskPattern  }
$taskName = $taskDetails.TaskName

此脚本失败并出现错误:

This script fails with error :

"Get-ScheduledTask : 术语Get-ScheduledTask"不被识别为cmdlet、函数、脚本文件或可运行程序的名称.检查名称的拼写,或者如果包含路径,请验证路径正确,然后重试."

"Get-ScheduledTask : The term 'Get-ScheduledTask' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

然后我尝试了这种方法,它给了我(任务名称日期和时间状态)",但我不知道如何只从它的输出中提取任务名称.

Then I tried this approach which gives me the "(taskname Date&Time State)" but I don't know how to only extract the taskname from it's output.

$taskPattern = "ServiceNow" 
$taskExists = schtasks /query | select-string -patt $taskPattern 
$taskExists

谁能告诉我如何仅通过提取实际任务名称使其在 Windows Server 2008 R2 中工作?提前致谢

Can anyone please tell me how to make it work in Windows Server 2008 R2 by only extracting the actual taskname? Thanks in Advance

推荐答案

您无法在 Windows 2008 R2 中获得 Get-ScheduledTask,因为它是为 Windows 2012 引入的,并且没有人向后移植它.有关更多信息,请参阅 github 上的链接.

You can't get Get-ScheduledTask at windows 2008 R2 as it was introduced for Windows 2012 and nobody backported it. For more see the link at github.

最能描述情况的评论是:

The comment that describes the situation the best is:

嗨@dronkoff - PS 4.0 支持此模块.然而问题不在 PowerShell 的版本中.问题是此资源所依赖的 ScheduledTasks 模块未构建进入 Windows Server 2008R2.它首先包含在 Windows Server 中2012.

Hi @dronkoff - This module is supported on PS 4.0. However, the problem isn't in the version of PowerShell. The issue is that the ScheduledTasks module, which this resource depends on, is not built into Windows Server 2008R2. It was first included in Windows Server 2012.

许多 PowerShell 模块都是这种情况:它们丢失了来自旧版本的 Windows(例如网络).在奇怪的有时有一个简单的解决方法.但是在这种情况下转换没有资源就不可能使用 schtasks.exe完全重写,可能会导致一些真正的东西不稳定和有问题(更不用说会出现的问题了schtasks 的不同本地化版本).我真的看不到这个不幸发生(除非社区中的其他人已经关于如何做到这一点的一些想法).

This is the case with many of the PowerShell modules: They are missing from older versions of Windows (Networking for example). In the odd occasion there is an easy work around. However in this case converting the resource over to use schtasks.exe is not possible without a complete rewrite and would probably result in something really unstable and buggy (not to mention the problems that would arise with different localized versions of schtasks). I really can't see this happening unfortunately (unless someone else from the community has some ideas on how to do this).

但是你说得对,这个应该提到Windows Server目前不支持 2008R2.

But you are correct, this should be mentioned that Windows Server 2008R2 is not currently supported.

解决方法

解决方法是使用带有正确开关的 schtask.

如果你在 Microsoft 文件夹中有它,就像我在例子中一样,你只需要运行:

If you have it in the Microsoft folder as I have it in the example you just need to run:

schtasks /tn \Microsoft\ServiceNow /query /fo LIST

输出:

Folder: \Microsoft
HostName:      XXXXXACL06
TaskName:      \Microsoft\ServiceNow
Next Run Time: 7/4/2018 8:16:22 AM
Status:        Ready
Logon Mode:    Interactive only

如果您想了解更多详细信息,可以使用 /v(详细)开关:

If you want more details you can use the /v (verbose) switch:

schtasks /tn \Microsoft\ServiceNow /query /v > service_now_details.txt

编辑 - 找到一个模式

如果您只有要搜索的模式,则必须使用一些其他工具来搜索字符串.Windows 本身支持 findstr(或 Select-String(powershell 中的短sls)):

If you have only pattern to search you have to use some additional tool to search the string. The windows natively supports findstr (or Select-String (short sls) in powershell):

要查找您可以使用的任务名称,请执行以下操作:

To find the task name you can use then:

schtasks /query /fo LIST | findstr "ServiceNow"

或者即使有狂野宪章

schtasks /query /fo LIST | findstr "ServiceNo*"

powershell 方式:

schtasks /query /fo LIST | sls 'ServiceNo*'

所有情况下的输出都将是这样的(因为我的任务完全命名为 ServiceNow):

The output in all cases will be something like this (since my task is named exactly ServiceNow):

任务名称:\Microsoft\ServiceNow

Edit2区分大小写

如果您要搜索不区分大小写的字符串,则:对于 findstr,您必须添加 /I 以使其不敏感.powershell 的 select-string (sls) 自然是不敏感的.

If you are searching for case insensitive string then: for findstr you have to add /I to make it insensitive. The powershell's select-string (sls) is naturally insensitive.

这篇关于Get-ScheduledTask 命令在 Windows Server 2008 R2 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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