获取分钟并使用SendKeys输出它们

问题描述 投票:0回答:2

这是我第一次编写脚本,我想从小开始。我的目标很简单:获取当前时间(Get-Date -Format mm)并输出2位数字作为SendKeys的击键。

问题是,我不知道如何将该2位数输出转换为对象以便“SendKeys”输出。

powershell sendkeys getdate
2个回答
4
投票

如果你有一些问题要发送它,请先使用ToString()方法将Datetime结果首先转换为String,这是将其发送到记事本的完整示例:

## Find all Active Windows Titles
$windows=Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle
## Find Specific name 
$WindowTitle=($windows | ? {$_ -match "Notepad"} ).MainWindowTitle
## Add Type and Focus Activate the Window
$wshell = New-Object -ComObject wscript.shell
$wshell.AppActivate($WindowTitle)
## Send Keys
$wshell.SendKeys((Get-Date -Format mm).ToString())

1
投票

不确定这里的全部要求是什么,但您可以将分钟发送到记事本,如下所示

Add-Type -AssemblyName microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
$secs = Get-Date -Format mm
notepad

start-sleep -Milliseconds 500

[Microsoft.VisualBasic.Interaction]::AppActivate("notepad")

[System.Windows.Forms.SendKeys]::SendWait($secs)
© www.soinside.com 2019 - 2024. All rights reserved.