Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Running Remote Comman on a different path

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

enter image description here

Edit 2

Yes thanks for the comment (which has disappeared now) - putting a semi colon after the path works. Thanks for the help.

like image 791
Varun Sharma Avatar asked Dec 13 '25 05:12

Varun Sharma


1 Answers

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
like image 198
Frode F. Avatar answered Dec 15 '25 19:12

Frode F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!