Powershell 将命令发送到一些腻子窗口

如何解决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 using Plink

点击示例

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

Powershell and Plink

$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 等

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?