程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Powershell 将命令发送到一些腻子窗口大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决PowersHell 将命令发送到一些腻子窗口?

开发过程中遇到PowersHell 将命令发送到一些腻子窗口的问题如何解决?下面主要结合日常开发的经验,给出你关于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 等

大佬总结

以上是大佬教程为你收集整理的Powershell 将命令发送到一些腻子窗口全部内容,希望文章能够帮你解决Powershell 将命令发送到一些腻子窗口所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: