gpt4 book ai didi

powershell - Powershell 计划作业 InstanceIds 如何工作?

转载 作者:行者123 更新时间:2023-12-03 09:46:01 25 4
gpt4 key购买 nike

我正在尝试使用 Get-Job 和 Wait-Job 等作业 cmdlet 来管理计划作业的实例。我遇到了问题,感觉缺少一些基本的东西。

我发现,如果我想使用 Job cmdlet 处理计划的作业实例,我应该导入 PSScheduledJob 模块。

NOTE: To use the Job cmdlets on instances of scheduled jobs, the PSScheduledJob module must be imported into the session. To import the PSScheduledJob module, type "Import-Module PSScheduledJob" (without quotation marks) or use any Scheduled Job cmdlet, such as Get-ScheduledJob.

https://technet.microsoft.com/en-us/library/hh850536.aspx

我还发现,尽管如我所料,作业实例上的 Id 属性并未在全局范围内识别它 - 而是在单个 Powershell session 中。

The ID is an integer that uniquely identifies the job within the current session. It is easier to remember and to type than the instance ID, but it is unique only within the current session.

https://technet.microsoft.com/en-us/library/hh849693.aspx

InstanceId 应该是全局唯一的,这听起来不错......

An instance ID is a GUID that uniquely identifies the job on the computer.

https://technet.microsoft.com/en-us/library/hh849693.aspx

...但我似乎得到了返回不同 InstanceId 的相同作业实例。以下脚本直接或通过远程获取作业的 InstanceId。

Import-Module PSScheduledJob;
$localInstanceIds = ( get-job -Name $jobName).InstanceId.Guid

$remoteInstanceIds = (invoke-command -ComputerName . -ArgumentList $jobName -ScriptBlock {
param([string]$jobName)

Import-Module PSScheduledJob;
(get-job -Name Midwinter.AdviceOS.Reports.SsfsTestA).InstanceId.Guid
})

Compare-Object $localInstanceIds $remoteInstanceIds

请注意输出中每个方法返回了两个作业,但没有一个 InstanceId 匹配。

InputObject                                                      SideIndicator                                                   
----------- -------------
780df0f3-bfc1-4fef-80f5-730e8cbcc548 =>
44109202-cb9b-43cb-8447-7e48905f10a7 =>
4fe8efaa-3107-43cd-a55e-b457b3be5993 <=
ad3bac3f-9f80-4ccd-b8d9-60c1b49fcf6a <=

我还发现,计划的作业实例保存在创建它们的用户配置文件内的文件系统中。

When you create a scheduled job, Windows Powershell creates a directory for the scheduled job in the $home\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs directory on the local computer. The directory name is the same as the job name.

https://technet.microsoft.com/en-us/library/hh849673.aspx

以下脚本从该路径中的给定作业的 Status.xml 文件中检索所有 InstanceId,并将它们与使用 Get-Job cmdlet 返回的 InstanceId 进行比较。

Import-Module PSScheduledJob;
$paths = "C:\Users\$userName\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\$jobName\Output\*\Status.xml";

$namespace = @{ns = 'http://schemas.datacontract.org/2004/07/Microsoft.PowerShell.ScheduledJob'};

$instanceIdsFromFileSystem = (Select-Xml -Path $paths -XPath '/ns:StatusInfo/Status_InstanceId/text()' -Namespace $namespace).Node.Value

$instanceIdsFromCmdlet = (Get-Job -Name $jobName).InstanceId

compare-object $instanceIdsFromFileSystem $instanceIdsFromCmdlet -IncludeEqual

输出再次显示没有匹配项。

InputObject                                                      SideIndicator                                                   
----------- -------------
4fe8efaa-3107-43cd-a55e-b457b3be5993 =>
ad3bac3f-9f80-4ccd-b8d9-60c1b49fcf6a =>
e81731dd-4159-476d-937e-7a066d082eb6 <=
5ea34723-ab39-4949-b302-c4be1b1588bb <=

有人可以帮我解释一下吗?

最佳答案

InstanceId 是在运行时生成的 GUID - 它确实标识作业(在主机上“全局”,但不是通用),但不标识作业的内容。

您执行的任何作业都将分配一个随机 GUID,它们并非旨在跨 session 、主机甚至单个执行持续识别任何作业。如果您两次执行同​​一个作业,它们将具有不同的 InstanceId 属性。这是设计使然。

如果你想为一个 Job 做一个永久标识符,你可以使用 Name 属性。

关于powershell - Powershell 计划作业 InstanceIds 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31077213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com