加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

为什么使用其他用户通过PowerShell创建的每周任务失败,错误为0x4

发布时间:2020-12-14 04:14:42 所属栏目:Windows 来源:网络整理
导读:我们有一些脚本可以使用Power Shell创建预定作业,作为我们应用程序的一部分.最近测试它们时,我注意到它们中的一些总是立即失败,并且没有产生任何输出(它们甚至不出现在Get-Job列表中). 经过多天的调整,我们已经设法将它隔离到任何设定为每周运行的工作.下面
我们有一些脚本可以使用Power Shell创建预定作业,作为我们应用程序的一部分.最近测试它们时,我注意到它们中的一些总是立即失败,并且没有产生任何输出(它们甚至不出现在Get-Job列表中).

经过多天的调整,我们已经设法将它隔离到任何设定为每周运行的工作.下面是一个脚本,它创建两个完全相同的作业.当我们在我们的域上运行它,并提供域用户的凭据,然后强制两个作业在任务计划程序GUI中运行(右键单击 – >运行),每日运行正常(0x0结果)和每周一个失败(0x41306).

注意:如果我不提供-Credential参数,则两个作业都可以正常工作.如果任务是每周一次,并且以该域用户身份运行,则作业仅会失败.

我无法找到有关这种情况发生的原因的信息,也没有想到任何原因会导致每周工作的表现不同.任务计划程序中的“历史记录£”选项卡几乎没有任何有用的信息,只是“由于用户请求而停止的任务”和“任务已终止”,两者都没有有用的信息:

Task Scheduler terminated “{eabba479-f8fc-4f0e-bf5e-053dfbfe9f62}”
instance of the “MicrosoftWindowsPowerShellScheduledJobsTest1″
task. Task Scheduler stopped instance
“{eabba479-f8fc-4f0e-bf5e-053dfbfe9f62}” of task
“MicrosoftWindowsPowerShellScheduledJobsTest1” as request by
user “MyDomainSomeUser” .

怎么了?为什么每周任务的运行方式不同,我该如何解决这个问题呢?

这是Windows Server 2008 R2上的PowerShell v3.我一直无法在本地重现这一点,但我没有像我们的生产域中那样设置用户(我正在研究这个,但我想发布这个ASAP希望有人知道发生了什么!).

Import-Module PSScheduledJob

$Action =
{
    "Executing job!"
}

$cred = Get-Credential "MyDomainSomeUser"

# Remove previous versions (to allow re-running this script)
Get-ScheduledJob Test1 | Unregister-ScheduledJob
Get-ScheduledJob Test2 | Unregister-ScheduledJob

# Create two identical jobs,with different triggers
Register-ScheduledJob "Test1" -ScriptBlock $Action -Credential $cred -Trigger (New-JobTrigger -Weekly -At 1:25am -DaysOfWeek Sunday)
Register-ScheduledJob "Test2" -ScriptBlock $Action -Credential $cred -Trigger (New-JobTrigger -Daily -At 1:25am)

编辑:按照snover的建议添加到Connect:

https://connect.microsoft.com/PowerShell/feedback/details/776801/weekly-tasks-created-via-powershell-using-a-different-user-immediately-fail-with-error-0x41306

编辑:杰夫希克斯的一些额外信息

I used your code to create the same jobs on my 2008 R2 box running PS
v3. Both jobs ran fine from PowerShell using Start-Job. But in the
GUI,I got the same error for the weekly job.

I get the same result on Windows 8. Something is telling the task
service to abort. I tested some other settings but they had no effect.
I looked through all of the logs I could think of and all they show is
the job starting,PowerShell loading and then the task scheduler
cancelling.

I reset the weekly task to run today a little bit ago and it still
failed. I also tested a weekly task doing something other than
PowerShell and it ran just fine.

I changed the weekly job to use the same account as the current user
and it ran just fine. Changed it back to the other account and it
failed again. I have no idea about the correlation between the trigger
and account.

我在创建计划任务时遇到了类似的问题,但我不记得它是否基于日程安排.我发现转换到gMSA帐户来运行我们的计划任务?这使我们能够设置运行用户是否登录的任务.否则,您必须提供用户名和密码,并且无法选择是否登录用户.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读