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

[玩转系统] PowerShell 提醒作业

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

PowerShell 提醒作业


[玩转系统] 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 提醒作业

自定义属性对任何其他作业对象没有影响。如果我发现自己经常使用这些属性,我可能会创建一些附加函数来节省一些输入。

该系统旨在每天对自己进行临时提醒,这就是我不使用计划作业的原因。我不想清理一堆一次性工作。这些提醒作业仅在我的 PowerShell 会话打开时持续。但请注意,每个运行的提醒都会启动一个新的 PowerShell 进程,因此我不建议设置数十个提醒。事实上,如果你需要那么多提醒,你要么需要找一份新工作,要么需要一个助手!

我希望您能尝试一下,并让我知道您的想法或您认为可以改进的地方。享受!

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

取消回复欢迎 发表评论:

关灯