[英]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脚本吗?
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.