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

[玩转系统] 周五乐趣:拿起牛奶

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

周五乐趣:拿起牛奶


[玩转系统] 周五乐趣:拿起牛奶

也就是说,在某些情况下您可能仍然想使用 PowerShell,即使体验不是最佳。那么今天让我们来玩点乐趣吧。对于许多版本的 Windows 客户端来说,都有一个名为“粘滞便笺”的小实用程序。

[玩转系统] 周五乐趣:拿起牛奶

这是便利贴的电子版。能够使用 PowerShell 来利用该实用程序不是很好吗?遗憾的是,该应用程序的编写方式没有暴露任何可以通过 COM 或 .NET 实现自动化的内容。因此,我们需要采取一种极端的措施,将密钥发送到应用程序。

与 VBScript 时代一样,发送密钥是一个冒险的提议,因为您无法始终保证相关应用程序具有焦点。但让我们尝试一下。首先,我们需要通过调用命令 stikynot 来启动程序。

为了发送密钥,我将使用 .NET Framework。因此,为了安全起见,我将添加一些必要的组件以防万一。如果它们已经加载也没关系。

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")

现在激活窗口。您可以通过窗口标题来执行此操作。

PS D:\temp> get-process stikynot | select mainwindowtitle

MainWindowTitle
---------------
Sticky Notes

或者进程ID。我将使用标题。

[Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")

当窗口具有焦点时,我可以向它发送键。

[System.Windows.Forms.SendKeys]::SendWait("Get milk")

这就是全部!便笺有许多键盘快捷键,您可以使用此技术发送。我在 http://www.door2windows.com/list-of-all-keyboard-shortcuts-for-sticky-notes-in-windows-7/ 上找到了一个页面,其中列出了其中的许多内容。

但你知道我不会就此止步。我构建了一个名为 StickyNotes 的模块。

#requires -version 3.0

#StickyNotes.psm1

<#
a set of functions for using Sticky Notes in Windows

Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones6/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell 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.             *
  ****************************************************************

#>


#load necessary assemblies just in case
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")

#sleep interval to allow stikynot process to start
$sleep = 150

#private function for formatting the note

Function _FormatNote {

[cmdletbinding(DefaultParameterSetName="__AllParameterSets")]

Param(
[string]$Text,
[switch]$Bold,
[switch]$Italic,
[switch]$Underline,
[Parameter(ParameterSetName="Center")]
[switch]$Center,
[Parameter(ParameterSetName="Right")]
[switch]$Right,
[switch]$Append
)

Write-verbose "Activating note"
#grab the program
[Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")

#add formatting
if ($Bold) {
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    [System.Windows.Forms.SendKeys]::SendWait("^b")
}
if ($Italic) {
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    [System.Windows.Forms.SendKeys]::SendWait("^i")
}
if ($Underline) {
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    [System.Windows.Forms.SendKeys]::SendWait("^u")
}
if ($Center) {
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    [System.Windows.Forms.SendKeys]::SendWait("^e")
}
if ($Right) {
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    [System.Windows.Forms.SendKeys]::SendWait("^r")
}

if ($append) {
    #jump to the end of existing text
    Write-verbose "Appending"
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    [System.Windows.Forms.SendKeys]::SendWait("{Down}")
} 

#send the text
if ($text) {
 #copy text to clipboard and paste it. Faster than trying to send keys
 $text | clip
  [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
  [System.Windows.Forms.SendKeys]::SendWait("^v")
  [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
  #give the paste a moment to complete
  start-sleep -Milliseconds 50
  [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
 
 
} #if $text

[Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
Start-Sleep -Milliseconds 50
[System.Windows.Forms.SendKeys]::SendWait("{Down}")

Write-Verbose "Finished formatting note"
} #end private function

Function New-StickyNote {

<#
.Synopsis
Create a new sticky note.
.Description
This command will create a new sticky note. You can specify additional formatting options.
.Parameter Center
Align text center. The default is left.
.Parameter Right
Align text right. The default is left.
.Example
PS C:\> new-stickynote "pickup milk on the way home" -bold
.Notes
Last Updated: Sept. 25, 2014
Version     : 0.9

#>

[cmdletbinding(DefaultParameterSetName="__AllParameterSets")]
Param(
[Parameter(position=0,Mandatory=$True,HelpMessage="Enter text for the sticky note")]
[string]$Text,
[switch]$Bold,
[switch]$Italic,
[switch]$Underline,
[Parameter(ParameterSetName="Center")]
[switch]$Center,
[Parameter(ParameterSetName="Right")]
[switch]$Right
)

Write-Verbose "Starting $($MyInvocation.MyCommand)"

#if Sticky Note is already running switch to Add-StickyNote

$proc = Get-Process -Name stikynot -ErrorAction SilentlyContinue
if ($proc) {
  Write-Verbose "Sticky Note already running. Calling Add-StickyNote"
  Add-StickyNote @PSBoundParameters
  #bail out of this function
  Return
}
else {
#start the program
Try {
    Write-Verbose "Starting new process"
    Start-Process stikynot
    #give it a moment to complete
    Write-Verbose "Sleeping $sleep ms"
    Start-Sleep -Milliseconds $sleep
}
catch {
    Throw
}

} #if $proc

#format the note
_FormatNote @PSBoundParameters

#return focus to originating app
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)

Write-Verbose "Ending $($MyInvocation.MyCommand)"
} #end function

Function Add-StickyNote {

<#
.Synopsis
Add a new sticky note.
.Description
This command will create a add sticky note. You can specify additional formatting options. If there are no existing sticky notes, this command will create one.
.Parameter Center
Align text center. The default is left.
.Parameter Right
Align text right. The default is left.
.Example
PS C:\> add-stickynote "pickup milk on the way home" -bold
.Notes
Last Updated: Sept. 25, 2014
Version     : 0.9

#>

[cmdletbinding(DefaultParameterSetName="__AllParameterSets")]
Param(
[Parameter(position=0,Mandatory=$True,HelpMessage="Enter text for the sticky note")]
[string]$Text,
[switch]$Bold,
[switch]$Italic,
[switch]$Underline,
[Parameter(ParameterSetName="Center")]
[switch]$Center,
[Parameter(ParameterSetName="Right")]
[switch]$Right
)

Write-Verbose "Starting $($MyInvocation.MyCommand)"

#start the program if not running
Try {
    if (-Not (Get-Process -Name stikynot -ErrorAction SilentlyContinue)) {
        Write-Verbose "Starting Sticky Note"
        Start-Process stikynot
        #give it a moment to complete
         Write-Verbose "Sleeping $sleep ms"
        Start-Sleep -Milliseconds $sleep
       $Existing = $False
    }
    else {
         #create a flag to know that I can use an existing process
         Write-Verbose "Re-using existing Sticky Note Process"
        $Existing = $True
    }
        #grab the program
        [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
   
}
catch {
    Throw
}

If ($Existing) {
    #Add a new note
    Write-Verbose "Add a new note"
    [System.Windows.Forms.SendKeys]::SendWait("^n")
    Start-sleep -Milliseconds $sleep
}

#format the note
_FormatNote @PSBoundParameters

#return focus to originating app
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)

Write-Verbose "Ending $($MyInvocation.MyCommand)"


} #end function

Function Set-StickyNote {

<#
.Synopsis
Set text and style for a sticky note.
.Description
This command will set the text and style for a sticky note. If there are multiple notes, this command will set the one that has focus, which you must do manually. The default is the last note created.

The style parameters like Bold behave more like toggles. If text is already in bold than using -Bold will turn it off and vice versa.
.Parameter Center
Align text center. The default is left.
.Parameter Right
Align text right. The default is left.
.Parameter Append
Append your text to the end of the note. Otherwise existing text will be replaced
.Example
PS C:\> set-stickynote "pickup milk on the way home" -bold -underline -append
.Notes
Last Updated: Sept. 25, 2014
Version     : 0.9

#>

[cmdletbinding(DefaultParameterSetName="__AllParameterSets")]
Param(
[Parameter(position=0)]
[string]$Text,
[switch]$Bold,
[switch]$Italic,
[switch]$Underline,
[Parameter(ParameterSetName="Center")]
[switch]$Center,
[Parameter(ParameterSetName="Right")]
[switch]$Right,
[switch]$Append
)

Write-Verbose "Starting $($MyInvocation.MyCommand)"

#start the program if not running
Try {
     #grab the program
     [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    #select existing text
    [System.Windows.Forms.SendKeys]::SendWait("^a")
}
catch {
    Throw
}

#format the note
_FormatNote @PSBoundParameters

#return focus to originating app
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)

Write-Verbose "Ending $($MyInvocation.MyCommand)"


} #end function

Function Remove-StickyNote {

<#
.Synopsis
Remove a new sticky note.
.Description
This command will remove the sticky note that has focus. If you have more than one note, the last one created has focus, unless you manually select a different one.

If you kill the stikynot process, the next time you create a note, any previously created notes will return.

.Example
PS C:\> remove-stickynote 

.Notes
Last Updated: Sept. 25, 2014
Version     : 0.9

#>

[cmdletbinding(SupportsShouldProcess)]
Param()

Write-Verbose "Starting $($MyInvocation.MyCommand)"

$proc= Get-Process stikynot -ErrorAction Stop
if ($proc) {
    #grab the program
    [Microsoft.VisualBasic.Interaction]::AppActivate("Sticky Notes")
    if ($PSCmdlet.ShouldProcess(($proc| Out-String).Trim())) {
        [System.Windows.Forms.SendKeys]::SendWait("^d")
    } #whatif
}
else {
    Write-Verbose "No sticky notes seem to be running"
}

#return focus to originating app
[Microsoft.VisualBasic.Interaction]::AppActivate($pid)

Write-Verbose "Ending $($MyInvocation.MyCommand)"


} #end function

#define aliases
Set-Alias -Name rn -Value Remove-StickyNote
Set-Alias -Name an -Value Add-StickyNote
Set-Alias -Name sn -Value Set-StickyNote
Set-Alias -Name nn -Value New-StickyNote

Export-ModuleMember -Alias * -Function "*-StickyNote"

该模块包括用于创建、添加、设置和删除便签的多种功能。棘手的部分是把握正确的时机。您可能需要调整一些睡眠值,或者准备重新运行命令。我不会围绕任何使用发送密钥技术的事物构建任何关键任务流程,因为它不可靠。但它确实很有趣!

你甚至可以像这样使用它:

start-job { 
$paramHash = @{
 LogName = "System"
 newest = 10
 ComputerName = "chi-dc01","chi-dc02","chi-dc04","chi-core01","chi-hvr2"
 EntryType = "Error","warning"
}

Get-EventLog @paramHash
New-StickyNote "The event log job has completed" -Bold

} 

命令结束时,会弹出一个便签,让您知道操作已完成。您可能会看到有关缺少进程的错误,但您可以忽略它。或者,也许您只是需要提醒在回家的路上拿起那条面包或您的孩子。

有时 PowerShell 并不是完成这项工作的最佳工具,但我认为它总是很有趣。让我知道你对我的小项目的看法。

周末愉快。

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

取消回复欢迎 发表评论:

关灯