简体   繁体   English

使用Powershell使用Invoke-Command远程安装Python失败

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

I am trying to use this script to install Python on the remote computer. 我正在尝试使用此脚本在远程计算机上安装Python。 If I run this file directly on the server. 如果我直接在服务器上运行此文件。 This is the Python_Pip_Proxy_PyWinAuto.ps1 file. 这是Python_Pip_Proxy_PyWinAuto.ps1文件。 It works. 有用。

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

However if I run the Invoke-Command using the following script to run this remotely on the same server, It print's the Hi message so I know that the file is running but Python doesn't get installed. 但是,如果我使用以下脚本运行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"
}

What is going wrong. 怎么了 What should I change the code to? 我应该将代码更改为什么?

Try using the -scriptblock {Your command here} parameter to execute the command inside the scriptblock parenthesis on the remote computer. 尝试使用-scriptblock {Your command here}参数在远程计算机上的scriptblock括号内执行命令。

Perhaps you can do it like 也许你可以做到

$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

You might want to remove the Write-Host "Hi" part because that gives the script an interactive nature. 您可能要删除Write-Host "Hi"部分,因为这使脚本具有交互性。 If you want to check for execution on remote computer, you can use out-file cmdlet to create a file on the remote computer as an indication. 如果要检查在远程计算机上的执行,则可以使用out-file cmdlet在远程计算机上创建文件作为指示。

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

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