[玩转系统] PowerShell 提醒作业
作者:精品下载站 日期:2024-12-14 07:42:31 浏览:13 分类:玩电脑
PowerShell 提醒作业
过去,我曾在博客中介绍过启动 PowerShell 时运行的发痒系统。但我经常有短期的烦恼或提醒需求。例如,由于我在家工作,我可能需要记住切换洗衣房或者在下午 2:00 接到电话。当然,我可以在电子邮件客户端中设置日历提醒,但我宁愿使用像 PowerShell 命令一样快速且简单的工具。所以我写了这个脚本,New-Reminderjob.ps1。
#requires -version 3.0
<#
.Synopsis
Create a reminder background job.
.Description
This command uses the MSG.EXE command line tool to send a reminder message to currently logged on user. You can specify how many minutes to wait before displaying the message or you can set the alert to run at a specific date and time.
This command creates a background job in the current PowerShell session. If you close the session, the job will also be removed. This command is intended to set ad-hoc reminders for the current user. The message will automatically dismiss after 1 minute unless you use -Wait.
Even though Start-Job doesn't support -WhatIf, this command does. This script will also add some custom properties to the job object so that you can track that status of your reminders. See the examples.
NOTE: Be aware that each running reminder will start a new PowerShell process.
.Parameter Message
The text to display in the popup.
.Parameter Time
The date and time to display the popup. If you enter just a time, it will default to the current day. See examples.
This parameter has aliases of date and dt.
.Parameter Minutes
The number of minutes to wait before displaying the popup message.
.Parameter Wait
Force the user to acknowledge the popup message.
.Example
PS C:\> c:\scripts\new-reminderjob.ps1 "Switch over laundry" -minutes 40 -wait
This command creates a new job that will display a message in 40 minutes and wait for the user to acknowledge.
.Example
PS C:\> c:\scripts\new-reminderjob.ps1 "Go home" -time "5:00PM" -passthru
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
49 Reminder7 BackgroundJob Running True localhost ...
Create a reminder to be displayed at 5:00PM today. The job object is written to the pipeline because of -Passthru
.Example
PS C:\> get-job remind* | Sort Time | Select ID,Name,State,Message,Time,Wait | format-table -auto
Id Name State Message Time Wait
-- ---- ----- ------- ---- ----
67 Reminder1 Running switch over laundry 5/27/2014 2:34:33 PM True
69 Reminder2 Running Budget meeting 5/27/2014 3:00:00 PM False
71 Reminder3 Running reboot WSUS 5/27/2014 3:21:33 PM False
In this example, PowerShell is getting all reminder jobs sorted by the time they will "kick off" and displays the necessary properties.
.Notes
Last Updated: 5/27/2014
Version : 0.9
Author : Jeff Hicks (@JeffHicks)
https://jdhitsolutions.com/blog
Learn more:
PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
PowerShell Deep Dives (http://manning.com/hicks/)
Learn PowerShell 3 in a Month of Lunches (http://manning.com/jones3/)
Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
****************************************************************
* 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/2014/05/powershell-reminder-jobs
.Link
msg.exe
Start-Sleep
Start-Job
.Inputs
None
.Outputs
custom System.Management.Automation.PSRemotingJob
#>
[cmdletbinding(DefaultParameterSetName="Minutes",SupportsShouldProcess)]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the alert message text")]
[string]$Message,
[Parameter(ParameterSetName="Time")]
[ValidateNotNullorEmpty()]
[Alias("date","dt")]
[datetime]$Time,
[Parameter(ParameterSetName="Minutes")]
[ValidateNotNullorEmpty()]
[int]$Minutes=1,
[switch]$Wait,
[switch]$Passthru
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
Write-Verbose -Message "Using parameter set $($PSCmdlet.ParameterSetName)"
Switch ($PSCmdlet.ParameterSetName) {
"Time" { [int]$sleep = ($Time - (Get-Date)).TotalSeconds }
"Minutes" { [int]$sleep = $minutes*60}
}
#get last job ID
$lastjob = Get-Job -Name "Reminder*" | sort ID | select -last 1
if ($lastjob) {
#define a regular expression
[regex]$rx ="\d+$"
[string]$counter = ([int]$rx.Match($lastJob.name).Value +1)
}
else {
[string]$counter = 1
}
} #begin
Process {
Write-Verbose -message "Sleeping for $sleep seconds"
$sb = {
Param($sleep,$cmd)
Start-Sleep -seconds $sleep ; Invoke-Expression $cmd
}
[string]$cmd = "msg.exe $env:username"
if ($Wait) {
Write-Verbose "Reminder will wait for user"
$cmd+=" /W"
}
$cmd+=" $message"
$jobName = "Reminder$Counter"
Write-Verbose -Message "Creating job $jobname"
#WhatIf
$whatif = "'{0}' in {1} seconds" -f $message,$sleep
if ($PSCmdlet.ShouldProcess( $whatif )) {
$job = Start-Job -ScriptBlock $sb -ArgumentList $sleep,$cmd -Name $jobName
#add some custom properties to the job object
$job | Add-Member -MemberType NoteProperty -Name Message -Value $message
$job | Add-Member -MemberType NoteProperty -Name Time -Value (Get-Date).AddSeconds($sleep)
$job | Add-Member -MemberType NoteProperty -Name Wait -Value $Wait
if ($passthru) {
#if -Passthru write the job object to the pipeline
$job
}
}
} #process
End {
Write-Verbose -Message "Do not close this PowerShell session or you will lose the reminder job"
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
简而言之,该脚本创建一个后台作业,该作业将使用 MSG.EXE 命令行工具向我自己显示消息。作业操作会休眠指定的秒数,然后运行我的 MSG.EXE 命令。我本可以使用多种方式来显示消息,但 MSG.EXE 是内置的,我没有看到任何重新发明轮子的理由。
如果需要,您可以将脚本修改为函数,该脚本需要提醒文本以及何时“传递”提醒。您可以指定分钟数(默认值为 1)或日期和/或时间。如果您指定“9:00AM”之类的时间,脚本将假定您的意思是今天上午 9 点。该脚本将您的开始时间转换为必须等待的秒数,并将其构建到作业脚本块中。
PS C:\scripts> .\New-ReminderJob.ps1 "Budget meeting" -date "5/27/2014 3:00PM" -wait
当时间到来时,我会收到这样的弹出消息。
因为我可能有几个日常提醒,所以我想要一种简单的方法来识别它们。我对脚本所做的一件事是为所有提醒作业指定一个以“提醒”开头的自定义名称。我使用正则表达式查找最近提醒作业中的数字并将其加一。另一个有用的步骤是我向作业对象本身添加了一些自定义属性。这些属性将脚本中的值嵌入到作业对象中。现在我可以执行如下有趣的命令:
自定义属性对任何其他作业对象没有影响。如果我发现自己经常使用这些属性,我可能会创建一些附加函数来节省一些输入。
该系统旨在每天对自己进行临时提醒,这就是我不使用计划作业的原因。我不想清理一堆一次性工作。这些提醒作业仅在我的 PowerShell 会话打开时持续。但请注意,每个运行的提醒都会启动一个新的 PowerShell 进程,因此我不建议设置数十个提醒。事实上,如果你需要那么多提醒,你要么需要找一份新工作,要么需要一个助手!
我希望您能尝试一下,并让我知道您的想法或您认为可以改进的地方。享受!
- 上一篇:[玩转系统] 创建 DSC 配置模板
- 下一篇:[玩转系统] 周五的格式化乐趣
猜你还喜欢
- 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