如何从本地在远程系统上执行 [System.Windows.Forms.SendKeys]::SendWait("Clear{Enter}")

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

我写了一个 PowerShell 脚本来执行一些任务,例如:

  • 更新/降级驱动器固件。
  • 使用桌面快捷方式打开一个特殊的 PowerShell(它只是在系统中预加载其他 ps.1 脚本)窗口并在其中运行一些命令。
  • 再次更新/降级驱动器固件。

我能够成功地做到这一点,当直接从客户端机器运行时,它工作正常。

但在上述步骤之后,我需要重新启动并再次运行一些 PowerShell cmds 并再次重新启动服务器,只能使用来自另一台服务器(机架管理器)的另一个应用程序远程完成电源循环(可以对其进行腻子并发出命令) .

  • 所以我的问题: 要执行电源循环,我需要远程运行整个脚本,不能在本地运行。

当我远程运行脚本时,它不会使用以下 sendkeys 命令执行 PowerShell 命令部分:

[System.Windows.Forms.SendKeys]::SendWait("Clear-WcsError{Enter}")
  • 获取错误:

使用“1”参数调用“SendWait”的异常:“访问被拒绝” + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : Win32Exception

我在网上查了很多,很多地方都建议使用凭据,也尝试过但仍然是同样的问题,也尝试过其他方法,也使用了任务计划程序,但不能在这里工作,因为涉及电源循环。

我能做一个

Enter-PSSession vs-g,

并执行 fw up/down 任务但不执行 sendkeys 部分。 我试图从本地服务器在客户端中运行此脚本,但它抛出相同的错误“使用“1”参数调用“SendWait”的异常:“访问被拒绝”,仅当它直接从客户端运行时才有效.

尝试使用以下代码从本地计算机在客户端计算机上运行脚本。

Enable-PSRemoting -force
winrm s winrm/config/client '@{TrustedHosts="vs-g"}'
Set-NetFirewallRule -Name "WINRM-HTTP-In-TCP-PUBLIC" -RemoteAddress Any
Set-Item wsman:\localhost\Client\TrustedHosts -value vs-g
Enable-WSManCredSSP –Role Client –DelegateComputer spoke

$scrpitpath = "C:\Users\Administrator\Documents\tools" #path of script in client machine
$s = New-PSSession -ComputerName "vs-g" -Credential Administrator 
Invoke-Command -Session $s -Command {C:\Users\Administrator\Documents\tools\1_REMOTE_Step1-8_forSlot2_.ps1}

仍然失败,并显示“使用“1”参数调用“SendWait”的异常:“访问被拒绝”。

  • 也试过这个,但同样的错误:
Enter-PSSession vs-g -Credential Administrator
Invoke-Command -FilePath "C:\Users\Administrator\Documents\tools\1_REMOTE_Step1-8_forSlot2.ps1" -ComputerName vs-g
Exit-PSSession

我用来直接在远程系统上运行 power shell 代码的代码,这个有效:

shortcut = "C:\Users\Administrator\Desktop\CSI Toolkit.lnk"
$programTitle = "CSI Toolkit"
$wshell = New-Object -ComObject Wscript.Shell
$shortcutt = $wshell.CreateShortcut($shortcut)

$process = Start-Process $shortcut -PassThru
Start-Sleep -Seconds 120

Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class CsiToolkitUtils {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

$windowHandle = $process.MainWindowHandle
[CsiToolkitUtils]::SetForegroundWindow($windowHandle)
Start-Sleep -Seconds 5

[System.Windows.Forms.SendKeys]::SendWait("Clear-WcsError{Enter}")
Start-Sleep -Seconds 15

我需要从另一台本地机器运行上面的代码,或者脚本可以在远程系统中,但应该能够从本地机器调用,而发送键功能不会因上述错误而失败。 我已经尝试了几个星期了,但没有在线解决方案,我看到过类似的错误,但没有在线解决方案。 工作中也没有人对此有答案,请帮忙!! 希望我的问题很清楚。

windows powershell sendkeys
© www.soinside.com 2019 - 2024. All rights reserved.