簡體   English   中英

Powershell 向一些膩子發送命令 windows

[英]Powershell send commans to some putty windows

我正在尋找 powershell 代碼將文本發送到一些膩子 windows (與膩子命令發送者所做的相同)。 我在下面找到了一個類似的示例,它將文本發送到一些記事本 windows - 它有效。 但是,我從記事本到膩子做了 1 個字的更改,它不適用於膩子。

如何更新代碼以使其適用於膩子? 謝謝你。

下面是記事本的代碼(膩子的行被注釋):

#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, 0, $text)
        Write-Output "---------------------"
    }
  }
}

繼續我的評論。

有許多通過 PowerShell 的命令行 plink 自動執行 putty 命令的示例。 Plink 只是一個 putty 命令行可執行文件,因此您可以像任何其他可執行文件一樣通過 PowerShell 運行它。 如此處所述:

PowerShell:運行可執行文件

https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

使用您最喜歡的搜索引擎快速搜索 web 將為您列出它們。

PowerShell 使用 Plink

示例命中

Powershell + Plink.exe (Putty) – 在遠程 Linux 設備上執行命令

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

使用 Plink 通過 Powershell 調用 Putty - 如何自動回答安全密鑰問題

Write-Output "yes" | 
PLINK.EXE -ssh $remoteserver -P 22 -pw $password -m $command

Powershell 和普林克

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM