简体   繁体   English

如何使用 Powershell 在远程机器上执行安装程序可执行文件?

[英]How to execute an installer executable on a remote machine with Powershell?

I'm trying to automate the upgrading of Spotfire BI system on our Windows servers.我正在尝试在我们的 Windows 服务器上自动升级 Spotfire BI 系统。

The instructions show a command that can be used to silently install which states the location of the executable file followed by all the required options/parameters as shown with this example:说明显示了一个可用于静默安装的命令,其中说明了可执行文件的位置,后跟所有必需的选项/参数,如本示例所示:

install.exe INSTALLDIR="<node manager installation dir>" NODEMANAGER_REGISTRATION_PORT=9080 NODEMANAGER_COMMUNICATION_PORT=9443 SERVER_NAME=SpotfireServerName SERVER_BACKEND_REGISTRATION_PORT=9080 SERVER_BACKEND_COMMUNICATION_PORT=9443 NODEMANAGER_HOST_NAMES=NodeManagerHostNames NODEMANAGER_HOST=NodeManagerHost -silent -log "C:\Users\user\Log file.log"

This does work, and as long as it preceded by the call operator (&), it runs well in PowerShell. However, I can't get it to work when running this on a remote server:这确实有效,只要它前面有调用运算符 (&),它就会在 PowerShell 中运行良好。但是,在远程服务器上运行它时我无法让它工作:

 function nodeManagerUpgrade {
    Param([String]$ServiceName1,
    [String]$InstallDirectory,
    [String]$HostNames1
    )

    Stop-Service $ServiceName1

    # this is the line that fails
    & "X:/downloads/spotfire/install.exe" INSTALLDIR="$InstallDirectory" NODEMANAGER_REGISTRATION_PORT=17080 NODEMANAGER_COMMUNICATION_PORT=17443 SERVER_NAME=localhost SERVER_BACKEND_REGISTRATION_PORT=19080 SERVER_BACKEND_COMMUNICATION_PORT1=9443 NODEMANAGER_HOST_NAMES=$HostNames1 -silent -log "install.log"
} 

 Invoke-Command -ComputerName $NodeIP -ScriptBlock ${function:nodeManagerUpgrade} -argumentlist ($ServiceName,$InstallDirectory,$HostNames) -credential $credentials 

I can run the code contained in the function directly on the remote server and it works correctly.我可以直接在远程服务器上运行 function 中包含的代码,它工作正常。 However, when I try and run it from the central server through WinRM/Invoke-Command within a function, it fails giving me this error:但是,当我尝试通过 function 中的WinRM/Invoke-Command从中央服务器运行它时,它没有给我这个错误:

The term 'X:/downloads/spotfire/install.exe' is not recognized as the name of a cmdlet

Is it possible to run an executable using PowerShell on a remote server?是否可以在远程服务器上使用 PowerShell 运行可执行文件?

Your executable path is based on a drive letter , X: .您的可执行路径基于驱动器X:

However, in remote sessions mapped drives (drives connected to.network shares) aren't available by default, so you have two options:但是,在远程会话中,映射驱动器(连接到网络共享的驱动器)在默认情况下不可用,因此您有两个选择:

  • Establish a (temporary) drive mapping with New-PSDrive before calling the executable (eg, $null = New-PSDrive X FileSystem \\foo\bar )在调用可执行文件之前使用New-PSDrive建立(临时)驱动器映射(例如$null = New-PSDrive X FileSystem \\foo\bar

  • More simply, use the full UNC path of the target executable (eg更简单地说,使用目标可执行文件的完整UNC路径(例如
    & \\foo\bar\downloads\spotfire\install.exe... ) & \\foo\bar\downloads\spotfire\install.exe... )

If the target executable isn't actually accessible from the remote session, you'd have to copy it there first, which requires establishing a remote session explicitly and using如果目标可执行文件实际上无法从远程 session 访问,则必须先将其复制到那里,这需要明确建立远程 session 并使用
Copy-Item -ToSession - see the docs for an example. Copy-Item -ToSession - 有关示例,请参阅文档

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

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