[玩转系统] 更好的 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"
现在我有一个工具,带有别名,默认值适合我,但如果我需要查看其他内容,我可以根据我的参数调整输出。如果您想尝试此操作,请将函数和类型信息保存到同一个脚本文件中。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag