除非我连接到GUI,否则计划的任务不会运行Hyper-V VM
我有一个脚本,可以在虚拟机上启动应用程序并记录该应用程序的一些数据。由于 powershell 脚本不允许我在前台运行应用程序,我决定在 2 分钟后安排一个任务,然后继续轮询任务完成情况。当我连接到 Hyper-V 虚拟机时,此功能完美运行。但是,一旦我通过自动化(无 GUI)运行此脚本,它就不会运行此计划任务。
所有其他 ps1 脚本都可以很好地完成其工作 - 安装产品并复制一些文件。
以下是我在虚拟机上运行的 powershell 命令:
$password= "password" | ConvertTo-SecureString -asPlainText -Force;
$username = "name";
$credential = New-Object System.Management.Automation.PSCredential($username,$password);
Invoke-Command -VMName INSTANCE_ID -Credential $credential -ScriptBlock
{
$gettime = (Get-Date).AddMinutes(2);
$run = $gettime.ToString('HH:mm');
$action = New-ScheduledTaskAction -Execute 'C:\logging.bat';
$trigger = New-ScheduledTaskTrigger -Once -At $run;
$principal = New-ScheduledTaskPrincipal -GroupID "BUILTIN\Administrators" -RunLevel Highest;
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName "ID_Logging_Task" -Description "my description"
}
然后我在代码中等待 4 分钟。在我开始轮询我的工作状态之前。
$password= "password" | ConvertTo-SecureString -asPlainText -Force;
$username = "name";
$credential = New-Object System.Management.Automation.PSCredential($username,$password);
Invoke-Command -VMName INSTANCE_ID -Credential $credential -ScriptBlock
{
$timer = [Diagnostics.Stopwatch]::StartNew();
$timeout = 2000;
while (((Get-ScheduledTask -TaskName 'ID_Logging_Task').State -eq 'Running') -and ($timer.Elapsed.TotalSeconds -lt $timeout))
{
Write-Verbose -Message 'Waiting on scheduled task...';
Start-Sleep -Seconds 3;
}
$timer.Stop();
}
通过添加日志,我确认轮询器几乎只轮询 2 秒。而且我的批处理文件也没有在 4 分钟内完成工作,因为它没有产生任何输出(它应该在每次运行时创建一个基本输出)。当我未连接到虚拟机的 GUI 时,计划任务永远不会从“就绪”状态进入“运行”状态。
这里可能出现什么问题,我该如何解决这个问题?
更新
问题确实是,我设置的主体仅在用户登录时才在管理模式下运行我的任务。为了解决这个问题,我尝试提供 LogonType 值的各种组合,但无济于事。
安排任务创建选项“无论用户是否登录都运行”使用powershell
上面的答案说,如果我们在脚本中传递密码,则可以在不登录的情况下运行。我很乐意这样做。任何人都可以帮助我使用允许管理模式且不允许登录的脚本。我还查看了 任务计划程序不支持选项“以最高权限运行”和“运行天气用户是否登录”但这也不提供任何解决方案。我也不需要以任何方式访问网络。
添加了更多信息 - BUILTIN\Administrators 和普通管理员组之间的差异
I have a script which launches an app on the VM and logs some data for the app. As powershell script does not allow me to run the app in foreground I decided to schedule a task after 2 mins and then keep polling for the task completion. This worked flawlessly when I am connected to the Hyper-V VM. But as soon as I run this script via my automation (no gui) it does not run this scheduled task.
All other ps1 scripts do their job just fine - installing the product and copying some files.
Below are the powershell commands I run on the VM:
$password= "password" | ConvertTo-SecureString -asPlainText -Force;
$username = "name";
$credential = New-Object System.Management.Automation.PSCredential($username,$password);
Invoke-Command -VMName INSTANCE_ID -Credential $credential -ScriptBlock
{
$gettime = (Get-Date).AddMinutes(2);
$run = $gettime.ToString('HH:mm');
$action = New-ScheduledTaskAction -Execute 'C:\logging.bat';
$trigger = New-ScheduledTaskTrigger -Once -At $run;
$principal = New-ScheduledTaskPrincipal -GroupID "BUILTIN\Administrators" -RunLevel Highest;
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName "ID_Logging_Task" -Description "my description"
}
Then I wait for 4 mins in my code. Before I start polling for my job status.
$password= "password" | ConvertTo-SecureString -asPlainText -Force;
$username = "name";
$credential = New-Object System.Management.Automation.PSCredential($username,$password);
Invoke-Command -VMName INSTANCE_ID -Credential $credential -ScriptBlock
{
$timer = [Diagnostics.Stopwatch]::StartNew();
$timeout = 2000;
while (((Get-ScheduledTask -TaskName 'ID_Logging_Task').State -eq 'Running') -and ($timer.Elapsed.TotalSeconds -lt $timeout))
{
Write-Verbose -Message 'Waiting on scheduled task...';
Start-Sleep -Seconds 3;
}
$timer.Stop();
}
By adding logs I have confirmed that the poller barely polls for 2 seconds. And it is also not the case that my batch file has finished working in 4 minutes as it has produced no outputs(it should create a basic output every time it runs). The scheduled task never goes from "Ready" state to "Running" state when I am not connected to the GUI of the VM.
What can be the issue here and how can I fix this?
UPDATE
The issue indeed is that the Principal I have set runs my task in Admin mode only when the user is logged in. To solve this I tried to provide various combinations of LogonType values but to no avail.
Schedule task creation with option ""Run whether user is logged in or not"" using powershell
The above answer says it is possible to run without logging in if we pass password in script. I am fine with doing this. Can anyone help me with the script which allows both Admin mode and no logging in. I also looked at Task Scheduler is not supporting option "Run with highest Privilege" and "Run weather user is logged on or not" but this also does not provide any solutions. Also I do not need to access the Network in any way.
Added more information on - Difference between BUILTIN\Administrators and normal Administrators Group
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论