我正在尝试在远程计算机上执行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}"
最佳答案
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/