powershell - 在远程计算机上运行Powershell脚本

标签 powershell

我正在尝试在远程计算机上执行Powershell脚本。该脚本将打开一个记事本应用程序,创建一个文件并保存。我在源计算机中使用以下命令运行脚本:

调用命令-ComputerName“na18actxdc” -ScriptBlock {D:\ NMAutomation \ trial_2.ps1}

该命令在源计算机上运行了很长时间并中断。但是没有创建文本文件。有人可以在这里帮忙吗?

但是,从远程计算机执行同一脚本时,执行速度更快,并且会保存文本文件。我已经使用autoit脚本和psexec选项尝试了相同的概念。它们都不起作用。

远程计算机上的代码:

Import-Module "C:\Program Files (x86)\AutoIt3\AutoItX\AutoItX.psd1"
Invoke-AU3Run -Program notepad.exe
$windowtitle ="Untitled - Notepad"
Wait-AU3WinActive($windowtitle)
$windowhandle = Get-AU3WinHandle -Title $windowtitle
Show-AU3WinActivate -WinHandle $windowhandle 
Send-AU3Key("I'm in notepad"); 
$windowhandle = Get-AU3WinHandle($windowtitle);
$controlHandle = Get-AU3ControlHandle -WinHandle $windowhandle -Control  "Edit1"
Send-AU3ControlKey -ControlHandle $controlHandle -Key "{ENTER}simulate key strokes - line 1" -WinHandle $windowhandle
Close-AU3Win($windowtitle)
$windowtitlesaveas = "Save As"
$windowhandlesaveas = Get-AU3WinHandle ($windowtitlesaveas)
$controlHandlesaveas = Get-AU3ControlHandle -WinHandle $windowhandlesaveas -Control "Edit1"
$windowhandlesaveas -NewText "sample"
$controlHandlesaveas -Key "sample"
Invoke-AU3ControlClick -Title "Notepad" -Control "Button1" -NumClicks 1 
Wait-AU3WinActive("Save As")
Set-AU3ControlText -Title "Save As" -Control "Edit1" -NewText "sample" 
Send-AU3ControlKey -Title "Save As" -Control "Button1" -Key "{ENTER}"

最佳答案

microsoft:

HOW TO RUN A SCRIPT ON REMOTE COMPUTERS

To run a local script on remote computers, use the FilePath parameter of Invoke-Command.

For example, the following command runs the Sample.ps1 script on the S1 and S2 computers:

PowerShell

Invoke-Command -ComputerName S1, S2 -FilePath C:\Test\Sample.ps1

The results of the script are returned to the local computer. You do not need to copy any files.


更改
  Invoke-Command -ComputerName "na18actxdc" -ScriptBlock {D:\NMAutomation\trial_2.ps1}
   Invoke-Command -ComputerName "na18actxdc" -FilePath D:\NMAutomation\trial_2.ps1
希望能有所帮助。

关于powershell - 在远程计算机上运行Powershell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56127297/

相关文章:

windows - powershell 中进程的状态

powershell - 替换文本文件中的竖线

powershell - 从命令行将 pfx 文件导入到特定的证书存储中

powershell - 使用PowerShell在cmd.exe中运行命令

powershell - 在进行 session 后,Get-VM 未被识别为命令

powershell - 从 powershell 向集群添加通用服务

PowerShell 2.0 和 "The term ' Param' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称”

具有多个值和一个键的 Powershell 哈希表

regex - 从变量中搜索字符串并仅打印其上方的 3 行

powershell - 如何从powershell中的集合中提取范围?