在宏VBA中将操作参数传递给SchTasks [英] Passing Action Parameter to SchTasks in Macro VBA
问题描述
我正在尝试找到一种在任务计划程序中创建任务的方法,该任务将在以下提到的日期运行并使用命令执行Powershell
I am trying to find a way to create a task in my task scheduler that would run on the mentioned below dates and that would execute a powershell with commands
strCommand = "schtasks /create /tn HasanChTesst /tr PowerShell.exe /sc weekly /d wed /st 13:00:00"
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
我尝试按如下所示将参数添加到powershell旁边,但出现编译错误
I have tried adding the parameters next to powershell as follows and i am getting a compilation error
strCommand = "schtasks /create /tn HasanChTesst /tr "PowerShell.exe Out-File Hello.txt" /sc weekly /d wed /st 13:00:00"
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
有人对如何执行此操作有想法吗?
Does anyone have an idea on how to do this ?
谢谢
注意:我知道我可以直接运行powershell ..但是我想运行任务调度程序,并让调度程序运行带有参数的powershell.
Note: i know that i can run powershell directly .. however i want to run task scheduler and let the scheduler run my powershell with parameters.
推荐答案
请勿在家尝试...请参阅以下原因::
使用双引号-""
是必需的,以便转义单引号"
:
The usage of double quotes - ""
is needed, in order to escape the single quote "
:
Sub WithParams()
Dim command As String
Dim wsShell As Object
command = "schtasks /create /tn HasanChTesst /tr ""PowerShell.exe Out-File Hello.txt"" /sc weekly /d wed /st 13:00:00"
Set wsShell = CreateObject("WScript.Shell")
wsShell.Run (command)
End Sub
因为这样,您将需要此:
为了从预定任务中删除任务,这是代码:
And in order to remove the task from the scheduled tasks, this is the code:
Sub RemoveTask()
Dim wsShell As Object
Set wsShell = CreateObject("WScript.Shell")
wsShell.Run ("powershell -noexit Unregister-ScheduledTask -TaskName ""HasanChTesst""")
End Sub
这篇关于在宏VBA中将操作参数传递给SchTasks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!