Powershell启动过程可以';找不到文件
我正在尝试通过PowerShell remote从客户端升级具有特定应用程序的服务器:Powershell启动过程可以';找不到文件,powershell,Powershell,我正在尝试通过PowerShell remote从客户端升级具有特定应用程序的服务器: Invoke-Command -ComputerName $server -Credential $mycreds {Start-Process -FilePath "C:\temp\xxx.exe" -ArgumentList "-default", "-acceptEULA" -wait } 无论我尝试什么,我都会收到诸如“找不到指定的文件…”之类的消息。我做错了什么? 文件路径在本地(客户端)计算
Invoke-Command -ComputerName $server -Credential $mycreds {Start-Process -FilePath "C:\temp\xxx.exe" -ArgumentList "-default", "-acceptEULA" -wait }
无论我尝试什么,我都会收到诸如“找不到指定的文件…”之类的消息。我做错了什么?
文件路径在本地(客户端)计算机上。您的
C:\temp\xxx.exe
可执行文件必须在服务器(远程计算机)上。您的命令才能工作,因为这是执行脚本块({…}
)的地方
注意:相比之下,如果您使用带有-FilePath
参数的Invoke命令
远程运行本地脚本文件(.ps1
),PowerShell会自动将其复制到远程计算机;from:“使用此参数时,PowerShell将指定脚本文件的内容转换为脚本块,将脚本块传输到远程计算机,并在远程计算机上运行。”
要从本地(客户端)计算机复制那里的可执行文件,您需要一种四步方法(PSv5+,因为使用了复制项-ToSession
[1]):
- 使用显式创建到
的远程处理会话$server
- 使用及其
参数将本地(客户端)可执行文件复制到该会话(远程计算机)-ToSession
- 使用
参数(而不是-Session
)运行命令,以便在显式创建的会话中运行(这不是严格必需的,但无需创建另一个(临时)会话)-ComputerName
- 运行以关闭远程会话
- 虽然您可以启动GUI应用程序,但它们总是以不可见的方式运行
- 类似地,不支持交互式控制台应用程序(尽管客户端接收控制台应用程序的输出)
# Specify the target server(s)
$server = 'w764' # '.'
# Establish a remoting session with the target server(s).
$session = New-PSSession -ComputerName $server
# Copy the local executable to the remote machine.
# Note: Make sure that the target directory exists on the remote machine.
Copy-Item C:\temp\xxx.exe -ToSession $session -Destination C:\temp
# Now invoke the excutable on the remote machine.
Invoke-Command -Session $session {
# Invoke *synchronously*, with -Wait.
# Note: If the program is a *console* application,
# you can just invoke it *directly* - no need for Start-Process.
Start-Process -Wait -FilePath C:\temp\xxx.exe -ArgumentList "-default", "-acceptEULA"
}
# Close the remote session.
# Note: This will terminate any programs that still
# run in the remote session, if any.
Remove-PSSession $session
(1)如果运行<强> PosivsV4或低于< /强>,请考虑下载<强> < /强> ./p>,以确认,“EXE在<代码> $Server < /C>”或“运行<代码>调用命令< /代码>的计算机上?EXE必须在$Server上。您必须将其复制到客户端。您可以通过复制项目和会话来完成此操作。如果exe有图形界面,您将看不到它。谢谢您的快速回复。就我的理解而言,为什么我可以在本地机器上运行PS脚本,但不能运行.exe?即使scrpt只在lokal机器上,这一点也很有用:$inst=Invoke命令-ComputerName$server-Credential$mycreds-FilePath“C:\scripts\InstalledBssVersions.ps1”@ChristerLöwing:如果您使用
Invoke命令和-FilePath
参数来运行本地显示的脚本文件(.ps1
)远程,PowerShell会自动将其复制到远程机器;from:“当您使用此参数时,PowerShell将指定脚本文件的内容转换为脚本块,将脚本块传输到远程计算机,并在远程计算机上运行。”-我还用此信息更新了答案。@ChristerLöwing:很遗憾,-ToSession
仅在PowerShell v5中引入。您可以研究其他替代方案,例如。我还更新了答案以包含此信息。