簡體   English   中英

使用Powershell使用Invoke-Command遠程安裝Python失敗

[英]Remote Installation of Python using Powershell fails using Invoke-Command

我正在嘗試使用此腳本在遠程計算機上安裝Python。 如果我直接在服務器上運行此文件。 這是Python_Pip_Proxy_PyWinAuto.ps1文件。 有用。

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Write-Host("Hi")
$installer="C:\temp\python-3.6.2.exe"
& $installer /quiet PrependPath=1 InstallAllUsers=1 TargetDir="C:\Python36"

但是,如果我使用以下腳本運行Invoke-Command以便在同一台服務器上遠程運行此Invoke-Command則會打印出Hi消息,因此我知道該文件正在運行, 但未安裝Python。

# Getting the list of servers from a .txt file to an array #

$SRVListFile = "C:\Scripts\ServerList.txt"
$SRVList = Get-Content $SRVListFile -ErrorAction SilentlyContinue

# Copying the .exe file from a shared location to each server in the array #
# Invoking the .ps1 file which runs natively on each server #

Foreach($computer in $SRVList) {

    Get-Service remoteregistry -ComputerName $computer | start-service
    Copy-item -Path "E:\Software\python-3.6.2.exe" -Destination \\$computer\c$\temp -Recurse
    Copy-item -Path "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_GUI.py" -Destination \\$computer\c$\temp -Recurse
    Invoke-Command -ComputerName $computer -FilePath "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_Pip_Proxy_PyWinAuto.ps1"
}

怎么了 我應該將代碼更改為什么?

嘗試使用-scriptblock {Your command here}參數在遠程計算機上的scriptblock括號內執行命令。

也許你可以做到

$Scriptblock = {
PowerShell -file "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_Pip_Proxy_PyWinAuto.ps1"

"This is Working" | out-file "C:\Hi.txt"
}
Invoke-Command -ComputerName $computer -Scriptblock $Scriptblock

您可能要刪除Write-Host "Hi"部分,因為這使腳本具有交互性。 如果要檢查在遠程計算機上的執行,則可以使用out-file cmdlet在遠程計算機上創建文件作為指示。

暫無
暫無

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

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