[玩转系统] 周五乐趣:拿起牛奶
作者:精品下载站 日期: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 并不是完成这项工作的最佳工具,但我认为它总是很有趣。让我知道你对我的小项目的看法。
周末愉快。
猜你还喜欢
- 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