简体   繁体   English

PowerShell-在远程服务器的C:驱动器上执行PowerShell脚本

[英]PowerShell - Execute PowerShell Script on the C: drive of a remote server

PowerShell Version 5 build 10586 PowerShell版本5内部版本10586

I am using the following code to remote connect to a server from my local PC 我正在使用以下代码从本地PC远程连接到服务器

$cimSession = New-CimSession -ComputerName "SERVERNAME.DOMAIN.COM"


     Invoke-Command -ComputerName SERVERNAME -ScriptBlock {Get-ChildItem “C:\Temp\ps”}

     Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1"


Remove-CimSession -CimSession $cimSession

The first command is able to run successfully and sees the PowerShell file on the remote server. 第一个命令能够成功运行,并且可以在远程服务器上看到PowerShell文件。

The second command fails with an error: 第二个命令失败并显示错误:

Invoke-Command : Cannot find path '\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1' because it does not exist.

Is there another way to call/run a PowerShell script on the C Drive of a remote server? 还有另一种方法可以在远程服务器的C驱动器上调用/运行PowerShell脚本吗?

  • I have granted my account full access to the specified file. 我已向我的帐户授予对指定文件的完全访问权限。
  • I have also tried to share the specified folder and given myself Read/Write access to the folder. 我还尝试共享指定的文件夹,并赋予自己对该文件夹的读/写访问权限。
  • I have changed the file path to the share path and get the same result. 我已将文件路径更改为共享路径,并得到相同的结果。
  • Invoke-Command -ComputerName SERVERNAME -FilePath "\\\\SERVERNAME\\ps\\PS_SCRIPT_FILE.ps1"

Try this: 尝试这个:

Invoke-Command -ComputerName SERVERNAME -ScriptBlock { Invoke-Command -FilePath "C:\\Temp\\ps\\PS_SCRIPT_FILE.ps1" }

In your existing code the -FilePath parameter is processed on the calling machine. 在现有代码中,-FilePath参数在调用计算机上处​​理。 By including that as a parameter within the ScriptBlock it should be processed on the target machine. 通过将其作为参数包含在ScriptBlock中,应在目标计算机上对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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