1

I am trying to use this script to install Python on the remote computer. If I run this file directly on the server. This is the Python_Pip_Proxy_PyWinAuto.ps1 file. 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.

# 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?

Addzy K
  • 715
  • 1
  • 7
  • 11

1 Answers1

1

Try using the -scriptblock {Your command here} parameter to execute the command inside the scriptblock parenthesis on the remote computer.

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. 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.

Sid
  • 2,586
  • 1
  • 11
  • 22
  • Thanks! Your solution worked out. Why do you think my way was failing? My way worked for installing another software called Alteryx. – Addzy K Sep 27 '17 at 05:07
  • For starters, the -Filepath parameter is used to specify a script file on the local machine (The one you are running your script from) and not the remote computer where you want it to run. Since you are trying to execute a script file sitting on the remote computer, you have to use the -scriptblock parameter. You could alternately, put the script on your local machine and try. It should work as long as you have the setup file on a network share accessible from the remote computer. – Sid Sep 27 '17 at 11:09