简体   繁体   中英

Using PowerShell to run a remote script

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?

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:

在此处输入图片说明

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.

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.

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM