Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 组托管服务帐户-在计划任务中运行Powershell命令时出现问题_Windows_Powershell_Scheduled Tasks - Fatal编程技术网

Windows 组托管服务帐户-在计划任务中运行Powershell命令时出现问题

Windows 组托管服务帐户-在计划任务中运行Powershell命令时出现问题,windows,powershell,scheduled-tasks,Windows,Powershell,Scheduled Tasks,我一直在调查(gmsa)帐户,并使用它们在服务器2012R2和PowerShell 5.0.10586.117上运行计划任务 在我使用它们一段时间后,我遇到了一些非常奇怪的行为 当任务作为gmsa帐户运行时,问题似乎是某种时间问题/竞争条件。运行脚本时,某些核心命令可能不存在/无法加载 例如: 在任务C:\temp\breaked task\test.cmd中调用的脚本 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Exec

我一直在调查(gmsa)帐户,并使用它们在服务器2012R2和PowerShell 5.0.10586.117上运行计划任务

在我使用它们一段时间后,我遇到了一些非常奇怪的行为

当任务作为gmsa帐户运行时,问题似乎是某种时间问题/竞争条件。运行脚本时,某些核心命令可能不存在/无法加载

例如:

在任务
C:\temp\breaked task\test.cmd中调用的脚本

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy "ByPass" -File "C:\temp\broken-task\test.ps1" > "C:\temp\broken-task\test.cmd.txt"
具有奇怪行为的实际脚本
C:\temp\breaked task\test.ps1

<#

.SYNOPSIS
Broken task.

.DESCRIPTION
Broken task.

#>

[CmdletBinding()] Param ()

Process {
    $ErrorActionPreference
    Set-Variable -Name "ErrorActionPreference" -Scope "Script" -Value "Stop"
    Set-Variable -Name "ErrorActionPreference" -Scope "Script" -Value "Stop"
    $ErrorActionPreference
}
$Action = New-ScheduledTaskAction -Execute "C:\Windows\System32\cmd.exe" -Argument "/C C:\temp\broken-task\test.cmd"
$Principal = New-ScheduledTaskPrincipal -UserID "my-gmsa-user$" -LogonType "Password"
New-ScheduledTask -Action $Action -Principal $Principal | Register-ScheduledTask -TaskPath "\test\" -TaskName "test123" | Out-Null
Start-ScheduledTask -TaskPath "\test\" -TaskName "test123"
任务完成后,
C:\temp\breaked task\test.cmd.txt
的内容如下:

Continue
Set-Variable : The term 'Set-Variable' is not recognized as the name of a cmdle
t, 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.
At C:\temp\broken-task\test.ps1:15 char:5
+     Set-Variable -Name "ErrorActionPreference" -Scope "Script" -Value ...
+     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Set-Variable:String) [], Comman 
   dNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Stop
上面发生的情况是,
Set变量
不存在于
test.ps1
中进程块的第2行,而存在于第3行(我们可以看到,
$ErrorActionPreference
已更改)

如果我以自己(普通用户)的身份运行cmd脚本或ps1,我将获得预期的输出,不会出现错误:

Continue
Stop
这是怎么回事?
Set Variable
命令位于
Microsoft.PowerShell.Utility
模块中。模块是否在第一次和第二次调用之间加载?其他命令也会出现这种情况,如
Get Item“C:\”

更糟糕的是,这种情况并不总是发生。我看到一个任务在一次运行中正常工作,而在另一次运行中失败

还要注意的是,我发现很难创建好的示例,并且这并不总是产生相同的奇怪结果(但是上面的示例目前在我的测试中毫无例外地失败了)

powershell、gmsa和任务调度器的组合是否以某种方式被破坏

我真的不能相信系统会像我一样做,当它有这种不一致的行为时

解决方法可能是在调用
设置变量之前显式加载
Microsoft.PowerShell.Utility
模块,但我不确定这是否是由于时间或其他原因造成的