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

[玩转系统] 星期五乐趣:改进的 PowerShell 午睡

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

星期五乐趣:改进的 PowerShell 午睡


因此,上周我对使用 PowerShell 打个盹的帖子感到很有趣。我在 Twitter 上得到了一些很好的反馈,在博客上也收到了新的评论。我最初的努力是一个相对简单的 PowerShell 脚本,它确实完成了工作。但我有很多可以扩展和改进脚本的领域,它们将是很棒的教学工具。所以我做了。

该函数是在一个脚本中定义的,您可以在 Github 中找到该脚本。

:

让我们看看我所做的一些更改。首先,我将其变成了一个带有基于注释的帮助的函数。您需要将脚本文件点源到 PowerShell 会话或配置文件脚本中,以使命令可用。

我将几乎每个选项都设置为参数,并添加了一些参数别名。因此,即使我将 Minutes 参数设置为位置参数,以便您不需要使用参数名称,您也可以使用 -Nap 或 -Time。您会注意到我还将唤醒消息作为参数。请随意设置您自己的默认值。否则,您可以在不同时间设置不同的消息。

我还意识到,如果你正在打瞌睡,仍然有人可能会来到你的办公桌前。因此,我添加了一个使用 Write-Progress 显示进度条的选项。这个 cmdlet 没有得到应有的喜爱。

我定义了一组消息:

#an array of status messages if using a progress bar
$ops = "I'm solving a PowerShell problem","I'm chasing cmdlets",
"Brilliance at work","Re-initializing my pipeline",
"Go away","I'm checking eyelid integrity","It can wait...",
"Don't you dare!","Spawning a new runspace","I'm multitasking",
"Nothing is that important","Unless you have beer for me, go away",
"I'm testing the new PSNap provider","I need this",
"I'm downloading my new matrix","Resource recyling in progress",
"Synaptic synch in progress","Neural network rebooting",
"If you can read this you shouldn't be here",
"$($env:username) has left the building"

这些消息将用作 Write-Progress 的 Status 属性。我喜欢在使用 Write-Progress 时使用参数哈希表来进行 splat。

$progHash = @{
 Activity = "Ssssshhh..."
 Status = $ops[0]
 SecondsRemaining = $remainingSeconds
}

如果我使用进度栏,则会使用剩余秒数显示。

if ($ProgressBar ) {
    Write-Progress @proghash
    #tick down $remainingseconds
    $proghash.SecondsRemaining = $remainingSeconds--
    #pick a new random status if remaining seconds is divisible by 10
    if ($remainingSeconds/10 -is [int]) {
        $proghash.status = $ops | Get-Random
    }
} #if

每隔 10 秒,我将状态设置为另一条随机选择的消息。结果是这样的:

[玩转系统] 星期五乐趣:改进的 PowerShell 午睡

我根据建议所做的最后一个重大更改是使用文本转语音功能让 Windows 语音“说出”唤醒消息。我添加了一个参数,让您指定一个语音名称,在美国最有可能是 David 或 Zira。如果您不知道名称,可以指定一个虚假值,例如“foo”,该函数将显示可用的名称。这是有效的,因为我向 Voice 参数添加了验证脚本。

[ValidateScript({
 #get voices
 Add-Type -AssemblyName System.speech
 $installed = [System.Speech.Synthesis.SpeechSynthesizer]::new().GetInstalledVoices().voiceinfo.Name
 if ($installed -match $_ ) {
    $True
 }
 else {
    [regex]$rx= "Microsoft\s+(?\w+)\s+"
    #build a list of voices assuming the voice name is something like Microsoft David Desktop
    $choices = (($rx.Matches($installed)).foreach({$_.groups["name"].value})) -join ","
    Throw "Can't find an installed voice for $_. Possible values are: $choices"
 }

})]
[Parameter(ParameterSetName = "voice")]
[string]$Voice,

这可能比大多数验证脚本更复杂一些。主要的要点是,如果您使用验证脚本,它必须返回 True 或 False,或者像我在这里所做的那样抛出异常。但它有效。

[玩转系统] 星期五乐趣:改进的 PowerShell 午睡

通过添加语音选项,我决定该功能可以使用 Write-Host 显示消息或说出消息。无论哪种情况都会发出铃声。

If ($Voice) {
    Add-Type -AssemblyName System.speech
    $speech = New-Object System.Speech.Synthesis.SpeechSynthesizer
    #find the matching voice object
    $selected = [System.Speech.Synthesis.SpeechSynthesizer]::new().GetInstalledVoices().voiceinfo.Name | where {$_ -match $voice}
    $speech.SelectVoice($selected)
    $speech.Rate = $Rate
    $speech.SpeakAsync($message) | Out-Null
    #write a blank line to get a new prompt
    Write-Host "`n"
}
else {
    Write-Host "`n$Message" -ForegroundColor Yellow
}

这意味着我必须区分我对参数集所做的参数。我在 cmdletbinding 属性中指定了默认值。

[cmdletbinding(DefaultParameterSetName = "host")]

然后我需要为每个参数指定一个参数集名称。如果不指定参数集名称,则该参数将属于所有集合。或者你也可以像我一样,并且明确地表达出来。如果你做得正确,它应该反映在帮助中。

[玩转系统] 星期五乐趣:改进的 PowerShell 午睡

可以看到有2种方法可以使用这个命令。我会让您获取一份副本并尝试新添加的内容。

当然,这不是面向生产的脚本,但我希望它提供一些不同脚本技术和 cmdlet 的有趣示例。

一如既往,真诚欢迎评论。

享受!

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

取消回复欢迎 发表评论:

关灯