1
votes

I have searched several sites to try to figure out how to run a PowerShell script on a remote machine. I have been using the following command:Invoke-Command -Session $s -FilePath "C:\Temp\test.ps1" however I get an error back saying the file doesn't exist. However I can search the same machine trough powershell and see that in fact it is there. Any suggestions on what I'm missing?

View of powershell winodw

Both of the answers below fixed the problem of it not finding the script, however now it looks like it runs the script, but nothing happens on the local machine:

enter image description here

The test.ps1 script has the following code: cscript C:\Temp\queryTest.vbs.
The queryTest.vbs file has this code: `

sysdate = "1/1/2015"
Dim e
Dim wb
Dim sheet
Set e = CreateObject("EXCEL.APPLICATION")
e.Workbooks.Open("C:\temp\testbook.xlsx")
Set wb = e.ActiveWorkbook
Set Sheet = wb.Sheets("Sheet1")
sheet.Cells(1,1).Value = sysdate
set sheet = nothing
wb.Save
wb.close
Set wb=nothing
e.Quit
Set e = Nothing

`

Nothing is writing in the excel file when i run test.ps1 remotely, however if i kick if off on the local machine it runs fine. I've tried calling both the ps1 script and the vbs script from powershell.

3

3 Answers

0
votes

The -FilePath parameter of Invoke-Command is looking at the LOCAL filesystem, not the filesystem on the remote server. It is intended as a way to run complex commands remotely, by reading a local script file, serializing its contents, and then deserializing and executing those commands on the remote host. You could just change your -FilePath parameter to -Scriptblock {& "C:\Temp\Test.ps1"}, or, if you copy the script to your own local C:\Temp directory, it will run as you intend.

0
votes

The script file "C:\Temp\test.ps1" are you supposing it to be on remote machine? If yes then try to give "\\machineName\C$\temp\test.ps1"

0
votes

I figured out that the problem I'm having was a problem in one of my scripts. Opening a new question for the issue I'm now observing.