PowerShell在不同路径上运行远程通信

PowerShell在不同路径上运行远程通信,powershell,path,remoting,Powershell,Path,Remoting,通过执行以下操作,我可以将远程powershell命令从本地计算机运行到其他域中的服务器: Invoke-Command -ComputerName serverName -ScriptBlock {Get-Process} -Credential $credential 但是,由于某些原因,它会在默认目录中运行-c:\users\username\documents 我的实际情况有点不同。我这里有一个powershell脚本(Test.ps1): C:\Data\Shared\Install

通过执行以下操作,我可以将远程powershell命令从本地计算机运行到其他域中的服务器:

Invoke-Command -ComputerName serverName -ScriptBlock {Get-Process} -Credential $credential
但是,由于某些原因,它会在默认目录中运行-
c:\users\username\documents

我的实际情况有点不同。我这里有一个powershell脚本(
Test.ps1
):

C:\Data\Shared\Installation
我想远程运行这个脚本。我在ScriptBlock中尝试了不同的方法,但总是出现错误。看起来像是语法问题。例如,我尝试了以下方法:

Invoke命令-ComputerName服务器名-ScriptBlock{cd“C:\Data\Shared\Installation”。/Test.ps1}-Credential$Credential

但这会产生以下错误

A positional parameter cannot be found that accepts argument './Test.ps1'.
只是需要一些帮助。谢谢

编辑

编辑2


是的,谢谢你的评论(现在已经消失了)——在路径工作后加上分号。感谢您的帮助。

如果您需要在脚本块中运行多个命令,则需要使用换行符或分号将它们分隔开来
。尝试:

Invoke-Command -ComputerName serverName -ScriptBlock {cd "C:\Data\Shared\Installation"; ./Test.ps1} -Credential $credential
或者尝试以下方法(如果脚本使用相对路径,则这不是一个好的解决方案):


谢谢你,伙计。第一个有效,但第二个出现以下错误-术语“C:\Data\Shared\Installation\Test.ps1”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。奇怪:S您确定路径正确吗?如果使用
{.c:\data\…}
{&“c:\data\…”}
运行它是否有帮助?
Invoke-Command -ComputerName serverName -ScriptBlock {C:\Data\Shared\Installation\Test.ps1} -Credential $credential