Object PowerShell计划任务注册表任务方法
我正在尝试使用powershell v2.0使用Schedule.Service Com对象创建计划任务。 我已经创建了ps1文件,但是当我们执行它时,我们得到了一个我无法理解的错误。 代码如下:Object PowerShell计划任务注册表任务方法,object,powershell,com,task,scheduler,Object,Powershell,Com,Task,Scheduler,我正在尝试使用powershell v2.0使用Schedule.Service Com对象创建计划任务。 我已经创建了ps1文件,但是当我们执行它时,我们得到了一个我无法理解的错误。 代码如下: param( [string]$xmlFilePath = $(throw "-xmlFilePath is required"), [string]$server = "localhost", [string]$taskFolderName = "\" ) try {
param(
[string]$xmlFilePath = $(throw "-xmlFilePath is required"),
[string]$server = "localhost",
[string]$taskFolderName = "\"
)
try {
$xmlContent = [xml] (Get-Content $xmlFilePath);
$taskScheduler = New-Object -ComObject Schedule.Service
$taskScheduler.Connect($server)
$taskFolder = $taskScheduler.GetFolder($taskFolderName);
$taskFolder.RegisterTask($xmlFilePathl, $xmlContent, 6, "<user name>", "<password>", 1);
}
catch {
$Exception = $_.Exception;
while ($Exception.Message -ne $null)
{
Write-Error $Exception.Message;
$Exception = $Exception.InnerException;
}
return;
}
param(
[string]$xmlFilePath=$(需要抛出“-xmlFilePath”),
[string]$server=“localhost”,
[字符串]$taskFolderName=“\”
)
试一试{
$xmlContent=[xml](获取内容$xmlFilePath);
$taskScheduler=新对象-ComObject Schedule.Service
$taskScheduler.Connect($server)
$taskFolder=$taskScheduler.GetFolder($taskFolderName);
$taskFolder.RegisterTask($xmlFilePathl,$xmlContent,6,“,”,1);
}
抓住{
$Exception=$\异常;
while($Exception.Message-ne$null)
{
写入错误$Exception.Message;
$Exception=$Exception.InnerException;
}
返回;
}
在本地或远程运行此命令会得到相同的结果。
错误如下:
C:\temp\CreateScheduledTaskFromXML.ps1:使用“6”参数调用“RegisterTask”时发生异常:(1,2):“
第1行字符:33
+.\CreateScheduledTaskFromXML.ps1如果您只需键入:
$taskScheduler = New-Object -ComObject Schedule.Service
$taskScheduler.Connect("localhost")
$taskFolder = $taskScheduler.GetFolder("\")
$taskFolder.RegisterTask
您将收到:
IRegisteredTask RegisterTask (string, string, int, Variant, Variant, _TASK_LOGON_TYPE, Variant)
有7个论点,这意味着你错过了一个论点。如果你看了一下电话,就会看到这样的画面:
HRESULT RegisterTask(
[in] BSTR path,
[in] BSTR xmlText,
[in] LONG flags,
[in] VARIANT userId,
[in] VARIANT password,
[in] TASK_LOGON_TYPE logonType,
[in, optional] VARIANT sddl,
[out] IRegisteredTask **ppTask
);
因此,我将尝试添加$null作为最后一个参数(安全描述符):
$taskFolder.RegisterTask($xmlFilePathl,$xmlContent,6,“,”,1,$null)
我能够解决这个问题。第一个是RegisterTask中的第一个参数。我将其解释为任务计划程序中的文件夹。但是,这不是文件夹,而是任务名称。其次,在一些注释和验证标志的帮助下,我发现第二个参数需要是字符串类型,而不是xml类型。最后,我不得不添加第7个参数null来填充方法签名:谢谢你的帮助
以下是有效的更新代码:
param(
[string]$taskName = $(throw "-taskName is required"), #complete path for the scheduled task
[string]$xmlFilePath = $(throw "-xmlFilePath is required"),
[string]$server = "localhost", # Only works with Servers it can access. Use WinRM for cross domain request
[string]$taskFolderName = "\"
)
$value = $null;
try {
$xmlContent = [string](Get-Content $xmlFilePath);
$taskScheduler = New-Object -ComObject Schedule.Service
$taskScheduler.Connect($server)
$taskFolder = $taskScheduler.GetFolder($taskFolderName);
$value = $taskFolder.RegisterTask($taskName, $xmlContent, 6, "<username>", "<password>", 1, $null);
}
catch {
$Exception = $_.Exception;
while ($Exception.Message -ne $null)
{
Write-Error $Exception.Message;
$Exception = $Exception.InnerException;
}
return;
}
param(
[string]$taskName=$(抛出“-taskName是必需的”),#计划任务的完整路径
[string]$xmlFilePath=$(需要抛出“-xmlFilePath”),
[string]$server=“localhost”,仅适用于它可以访问的服务器。使用WinRM进行跨域请求
[字符串]$taskFolderName=“\”
)
$value=$null;
试一试{
$xmlContent=[string](获取内容$xmlFilePath);
$taskScheduler=新对象-ComObject Schedule.Service
$taskScheduler.Connect($server)
$taskFolder=$taskScheduler.GetFolder($taskFolderName);
$value=$taskFolder.RegisterTask($taskName,$xmlContent,6,“,”,1,$null);
}
抓住{
$Exception=$\异常;
while($Exception.Message-ne$null)
{
写入错误$Exception.Message;
$Exception=$Exception.InnerException;
}
返回;
}
您对SCHTASK有何反感?另外,请注意,如果它是任务中的启动触发器,则您传递的用户必须位于服务器(远程或本地)的administrators组中。您还可以使用标志1而不是6来运行,以验证语法,而无需创建任务,以查看给出任何指针的iff。我尝试使用validate标志(1),同时添加$null来填充7参数,如下一篇文章中所述,以及前面的注释中所指定的,它产生了相同的错误。我将GC的类型改为字符串而不是xml,这并没有因为validate而失败。然后我将标志改回了6,现在得到了带有“7”参数的“Exception calling”RegisterTask:“参数是incorrect。(HRESULT的例外:0x80070057(E_INVALIDARG))。问题是哪一个。我将继续深入研究这一过程。我试图在第七个参数中使用7个参数和$null,但语句仍然失败,出现相同的错误阅读您的答案,这一个似乎很有用。
param(
[string]$taskName = $(throw "-taskName is required"), #complete path for the scheduled task
[string]$xmlFilePath = $(throw "-xmlFilePath is required"),
[string]$server = "localhost", # Only works with Servers it can access. Use WinRM for cross domain request
[string]$taskFolderName = "\"
)
$value = $null;
try {
$xmlContent = [string](Get-Content $xmlFilePath);
$taskScheduler = New-Object -ComObject Schedule.Service
$taskScheduler.Connect($server)
$taskFolder = $taskScheduler.GetFolder($taskFolderName);
$value = $taskFolder.RegisterTask($taskName, $xmlContent, 6, "<username>", "<password>", 1, $null);
}
catch {
$Exception = $_.Exception;
while ($Exception.Message -ne $null)
{
Write-Error $Exception.Message;
$Exception = $Exception.InnerException;
}
return;
}