如何解决Powershell 将命令发送到一些腻子窗口
我正在寻找 powershell 代码来将文本发送到一些腻子窗口(与腻子命令发送者所做的相同)。 我在下面找到了一个类似的示例,该示例将文本发送到一些记事本窗口 - 它有效。 但是,我把记事本改成putty 1个字,putty不行。
如何更新代码以使其适用于腻子?谢谢。
下面是记事本的代码(腻子的那一行被注释了):
#requires -Version 2
function Out-Notepad
{
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[String]
[AllowEmptyString()]
$Text
)
begin
{
$sb = New-Object System.Text.StringBuilder
}
process
{
$null = $sb.AppendLine($Text)
}
end
{
$text = $sb.ToString()
$processes = Get-Process notepad
# $processes = Get-Process putty
$sig = '
[DllImport("user32.dll",EntryPoint = "FindWindowEx")]public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);
[DllImport("User32.dll")]public static extern int SendMessage(IntPtr hWnd,int uMsg,int wParam,string lParam);
'
foreach ($process in $processes) {
$null = $process.WaitForInputIdle()
$type = Add-Type -MemberDefinition $sig -Name APISendMessage -PassThru
Write-Output type: $type
$hwnd = $process.MainWindowHandle
Write-Output hwnd: $hwnd
[IntPtr]$child = $type::FindWindowEx($hwnd,[IntPtr]::Zero,"Edit",$null)
Write-Output child: $child
$null = $type::SendMessage($child,0x000C,$text)
Write-Output "---------------------"
}
}
}
解决方法
继续我的评论。
有许多通过命令行 plink 通过 PowerShell 自动执行 putty 命令的示例。 Plink 只是一个 putty 命令行可执行文件,因此您可以像任何其他可执行文件一样通过 PowerShell 运行它。如此处所述:
• PowerShell:运行可执行文件
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
使用您最喜欢的搜索引擎进行快速网络搜索会为您列出它们。
点击示例
Powershell + Plink.exe (Putty) – Execute a command on a remote Linux Device
$plinkPath = "c:\Users\public\Downloads\plink.exe"
$OSun = "root" # User name required by PLINK (Putty) to SSH to Linux Primary Server
$OSpw = "rOoTpAsSwOrD" # Password required by PLINK (Putty) to SSH to Linux Primary Server
$LinuxServer = "myLinuxServer" # Server name,must resolve on your desktop machine
$remoteCommand = """ls"""
$localCommand = "$plinkPath $LinuxServer -l $OSun -pw $OSpw $remoteCommand"
$result = Invoke-Expression($localCommand)
Calling Putty via Powershell using Plink - how to auto answer the security key question
Write-Output "yes" |
PLINK.EXE -ssh $remoteserver -P 22 -pw $password -m $command
$Params = @(
"-l $SwitchLogon"
"-pw $SwitchPsswd"
"-m . $SwitchCliCommand"
"$_"
)
& C:\Scripts\Brocadeswitch\plink.exe $Params
即使是您所说的找到的脚本对于您的用例来说也太过分了。 SendKeys 也有它的问题,但你可以这样做。延迟是允许应用加载的必要条件。这是问题。不同的机器需要不同的时间。
Add-Type -AssemblyName System.Windows.Forms
$SendKeys = [System.Windows.Forms.SendKeys]
# Sending ot notepad
Start-Process -FilePath 'notepad.exe'
Start-Sleep -Seconds 3
$SendKeys::SendWait("~{TAB}$env:USERNAME~")
# Sending ot wordpad
Start-Process -FilePath 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Wordpad.lnk'
Start-Sleep -Seconds 3
$SendKeys::SendWait("~{TAB}$env:USERNAME~")
# Sending powershell instance
Start-Process -FilePath 'powershell.exe' '-NoProfile','-NoLogo'
Start-Sleep -Seconds 3
$SendKeys::SendWait("~{TAB}Get-Date~")
您也可以将此作为延迟策略。
# Using Do/Unitl
$MainWindowTitle = $null
Start-Process -FilePath 'notepad.exe'
do
{
$MainWindowTitle = (Get-Process -Name notepad).MainWindowTitle
Start-Sleep -Milliseconds 250
}
until ($MainWindowTitle -ne '')
$SendKeys::SendWait("~{TAB}$env:USERNAME~")
所以,我希望你明白使用 Powershell 进行 UI 自动化并不是一件真正的事情,因为虽然它可以,但并不是它的强项。因此,坚持使用真正的自动化,或使用专门构建的 GUI 自动化工具,例如 AutoIT,
`Selenium 等