2
votes

I am trying to execute a powershell script on a remote computer. The script opens a notepad application, creates a file and saves it. I run the script with below command in source machine:

Invoke-Command -ComputerName "na18actxdc" -ScriptBlock {D:\NMAutomation\trial_2.ps1}

The command runs for a long time on source machine and breaks. But the text file is not created. Could anyone help here please ?

However the same script when executed from the remote machine, executes faster and the text file gets saved. I have tried the same concept with autoit scripts and psexec options. None of them works.

Code on remote machine:

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}"
3
Can you try this please: Enter-PSSession -ComputerName na18actxdc and then D:\NMAutomation\trial_2.ps1. I have a feeling that Autoit cmdlets that are meant for an interactive window will fail to work in a commandline session. - Sid
Did anything happen with this? - Sid

3 Answers

0
votes

From 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.

Change

  Invoke-Command -ComputerName "na18actxdc" -ScriptBlock {D:\NMAutomation\trial_2.ps1}

to

   Invoke-Command -ComputerName "na18actxdc" -FilePath D:\NMAutomation\trial_2.ps1

Hope that helps.

0
votes

I think what is happening here is that by running this: Invoke-Command -ComputerName "na18actxdc" -ScriptBlock {D:\NMAutomation\trial_2.ps1} the ps1 does not get executed but simply opened in a notepad process.

I believe what you have to do is to try and tell it to run it in powershell.

Invoke-Command -ComputerName "na18actxdc" -ScriptBlock {PowerShell.exe -File D:\NMAutomation\trial_2.ps1}

0
votes

To verify that what you are trying to do is even possible with powershell remoting, you should verify that it can run in a live PSSession.

That can be done like this:

PS C:\Users\XXX> Enter-PSSession -ComputerName "na18actxdc"
[na18actxdc]: PS C:\Users\XXX\Documents> D:\NMAutomation\trial_2.ps1

If script runs successfully, then I don't see why Invoke-Command shouldn't work. If you still experience problems, try writing out some debug messages.