I can run remote powershell commands from my local machine to a server in a different domain by doing something like this:
Invoke-Command -ComputerName serverName -ScriptBlock {Get-Process} -Credential $credential
However this runs in this default directory for some reason - c:\users\username\documents
My actual situation is little different. I have got a powershell script (Test.ps1
) sitting here:
C:\Data\Shared\Installation
I want to run this script remotely. I tried different things inside the ScriptBlock but it always gives an error. Looks like a syntax issue. For example I tried this:
Invoke-Command -ComputerName serverName -ScriptBlock {cd "C:\Data\Shared\Installation" ./Test.ps1} -Credential $credential
but this gives the following error
A positional parameter cannot be found that accepts argument './Test.ps1'.
Just need some help regarding this. Thanks.
Edit
Edit 2
Yes thanks for the comment (which has disappeared now) - putting a semi colon after the path works. Thanks for the help.
If you need to run multiple commands in a scriptblock, you need to seperate them using a linebreak OR a semicolon ;
. Try:
Invoke-Command -ComputerName serverName -ScriptBlock {cd "C:\Data\Shared\Installation"; ./Test.ps1} -Credential $credential
Or try this (this isn't a good solution if your script works with relative paths):
Invoke-Command -ComputerName serverName -ScriptBlock {C:\Data\Shared\Installation\Test.ps1} -Credential $credential