当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 更好的 PowerShell 获取预定作业结果

作者:精品下载站 日期:2024-12-14 07:39:42 浏览:14 分类:玩电脑

更好的 PowerShell 获取预定作业结果


昨天,我发布了代码的快速更新,以获取 PowerShell 中最新的计划作业结果。我一直在使用一个简单的脚本。但我想得越多,我就越意识到我确实需要将它变成一个更灵活的函数。创建基于 PowerShell 的工具时,您需要考虑谁可能在使用它。尽管我只想要所有已启用作业的最后结果,但在某些情况下我可能想说最后 2 或 3 个。也许我只想获得特定作业的结果。或者也许是所有工作。最重要的是我需要更多的灵活性。现在我有了 Get-ScheduledJobResult 函数。

#requires -version 3.0

Function Get-ScheduledJobResult {

<#
.Synopsis
Get scheduled job result.
.Description
This command will retrieve the last job result for a given scheduled job. By
default, when you create a scheduled job, PowerShell will retain the last 32 job
results. This command can help you get the most recent job result. The default
behavior is to retrieve the newest job for all enabled scheduled jobs. But you
can specify scheduled jobs by name, indicate if you want to see all jobs, and 
also the number of recent jobs.

The command uses a custom type definition based on the Job object.
.Parameter All
Display all scheduled jobs. The default is enabled only.

.Example
PS C:\> Get-ScheduledJobResult 
Name  : Download PowerShell v3 Help
ID    : 87
State : Completed
Run   : 00:01:40.9564095
Start : 9/16/2013 6:00:09 AM
End   : 9/16/2013 6:01:50 AM

Name  : Daily Work Backup
ID    : 55
State : Completed
Run   : 00:00:53.8172548
Start : 9/15/2013 11:55:05 PM
End   : 9/15/2013 11:55:59 PM
.Example
PS C:\> Get-ScheduledJobResult download* -Newest 5 | Select ID,State,Location,Start,End,Run,HasMoreData | Out-GridView -title "Download Help Job"

Get the newest 5 job results for the Download* scheduled job, selecting a few 
properties and displaying the results in Out-Gridview.
.Notes
Last Updated: 9/16/2013
Version     : 0.9

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
.Link
https://jdhitsolutions.com/blog/2013/09/a-better-powershell-get-scheduled-job-results
.Link
Get-Job
Get-ScheduledJob

.Outputs
Custom ScheduledJob Object
#>

[cmdletbinding()]
Param(
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
[string]$Name="*",
[validatescript({$_ -gt 0})]
[int]$Newest=1,
[switch]$All
)

#only show results for Enabled jobs
Try {
  Write-Verbose "Getting scheduled jobs for $name"
  $jobs = Get-ScheduledJob -Name $name -ErrorAction Stop -ErrorVariable ev
}
Catch {
    Write-Warning $ev.errorRecord.Exception 
}

if ($jobs) {
#filter unless asking for all jobs
  Write-Verbose "Processing found jobs"
  if ($All) {
    Write-Verbose "Getting all jobs"
  }
  else {
    Write-Verbose "Getting enabled jobs only"
    $jobs = $jobs | where Enabled
  }

Write-Verbose "Getting newest $newest job results"
$data = $jobs | foreach { 
 #get job and select all properties to create a custom object
 Get-Job $_.name -newest $Newest | Select * | foreach {
    #insert a new typename
    $_.psobject.typenames.Insert(0,'My.ScheduledJob.Result')
    #write job to the pipeline
    $_
  }
} 
 
#write result to the pipeline
$data | Sort -Property PSEndTime -Descending

} #if $jobs

Write-Verbose "Finished"
} #end function

#define an alias
Set-Alias -Name gsjr -Value Get-ScheduledJobResult

该函数采用的参数可以传递给 Get-ScheduledJob 和 Get-Job。大多数情况下,核心功能保持不变:我获取计划作业的 X 个最新作业。不同之处在于,现在这些值由参数控制。我的修订的另一个好处是错误处理。之前我有一个管道表达式,但现在我使用 Try/Catch 块按名称获取计划作业,使用 * 作为默认值。您还会注意到我正在使用自己的错误变量。如果出现错误,我可以更优雅地处理。我会让你用一个虚假的预定作业名称来测试它,看看会发生什么。

但也许最大的变化是我基于 Job 对象定义了自己的对象类型。

$data = $jobs | foreach { 
 #get job and select all properties to create a custom object
 Get-Job $_.name -newest $Newest | Select * | foreach {
    #insert a new typename
    $_.psobject.typenames.Insert(0,'My.ScheduledJob.Result')
    #write job to the pipeline
    $_
  }
} 

为每个作业结果插入一个新的类型名称。因为我可能有多个对象,所以我需要为每个对象插入类型名。当我从事此工作时,我最初将我的类型名插入到 Microsoft.PowerShell.ScheduledJob.ScheduledJob 对象中。但我的自定义格式不是工作属性,因此我发现通过选择所有属性,这具有创建 Selected.Microsoft.PowerShell.ScheduledJob.ScheduledJob 的效果,我的更改有效。

插入类型名称只是该过程的一部分。在定义该函数的脚本文件中,我包含了利用 Update-TypeData 的代码。在PowerShell 3.0中我们不再需要处理XML文件。现在类型更新可以即时完成。因此,我没有使用 Select-Object 和自定义哈希表创建自定义属性,而是将它们添加为别名属性。我做了类似的事情来创建 Run 属性。

#define default property set for my custom type
$typename = 'My.ScheduledJob.Result'

#define some alias properties
$paramHash= @{
TypeName = $typename
MemberType = "AliasProperty"
MemberName = "Start"
Value = "PSBeginTime"
}
Update-TypeData @paramHash -force

$paramHash.MemberName="End"
$paramHash.Value="PSEndTime"
Update-TypeData @paramHash -Force

#define a custom property
$paramHash.MemberName="Run"
$paramHash.Value={$this.PSEndTime - $this.PSBeginTime}
$paramHash.memberType="ScriptProperty"
Update-TypeData @paramHash -Force

#set default display properties
Update-TypeData -TypeName $typename -DefaultDisplayPropertySet Name,ID,State,Run,Start,End -Force

我修订的最后一部分是定义默认显示属性集。效果是,当我运行我的函数时,如果我不指定任何其他格式,默认情况下我会看到我想要的属性。


PS C:\> Get-ScheduledJobResult
Name  : Download PowerShell v3 Help
ID    : 89
State : Completed
Run   : 00:01:40.9564095
Start : 9/16/2013 6:00:09 AM
End   : 9/16/2013 6:01:50 AM

Name  : Daily Work Backup
ID    : 57
State : Completed
Run   : 00:00:53.8172548
Start : 9/15/2013 11:55:05 PM
End   : 9/15/2013 11:55:59 PM

Name  : Thunderbird Backup
ID    : 94
State : Completed
Run   : 00:51:08.2574182
Start : 9/10/2013 11:59:00 PM
End   : 9/11/2013 12:50:08 AM

我仍然可以访问所有其他属性。

PS C:\> Get-ScheduledJobResult download* -Newest 5 | Select ID,State,Location,Start,End,Run,HasMoreData,Command | Out-GridView -title "Download Help Job"

[玩转系统] 更好的 PowerShell 获取预定作业结果

现在我有一个工具,带有别名,默认值适合我,但如果我需要查看其他内容,我可以根据我的参数调整输出。如果您想尝试此操作,请将函数和类型信息保存到同一个脚本文件中。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯