簡體   English   中英

如何使用 Powershell 在遠程機器上執行安裝程序可執行文件?

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

我正在嘗試在我們的 Windows 服務器上自動升級 Spotfire BI 系統。

說明顯示了一個可用於靜默安裝的命令,其中說明了可執行文件的位置,后跟所有必需的選項/參數,如本示例所示:

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"

這確實有效,只要它前面有調用運算符 (&),它就會在 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 

我可以直接在遠程服務器上運行 function 中包含的代碼,它工作正常。 但是,當我嘗試通過 function 中的WinRM/Invoke-Command從中央服務器運行它時,它沒有給我這個錯誤:

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

是否可以在遠程服務器上使用 PowerShell 運行可執行文件?

您的可執行路徑基於驅動器X:

但是,在遠程會話中,映射驅動器(連接到網絡共享的驅動器)在默認情況下不可用,因此您有兩個選擇:

  • 在調用可執行文件之前使用New-PSDrive建立(臨時)驅動器映射(例如$null = New-PSDrive X FileSystem \\foo\bar

  • 更簡單地說,使用目標可執行文件的完整UNC路徑(例如
    & \\foo\bar\downloads\spotfire\install.exe... )

如果目標可執行文件實際上無法從遠程 session 訪問,則必須先將其復制到那里,這需要明確建立遠程 session 並使用
Copy-Item -ToSession - 有關示例,請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM