我正在尝试使用PowerShell脚本在远程服务器上安装软件(MSI)。该脚本能够从Internet下载MSI文件,但无法安装MSI。
通过登录远程服务器直接执行远程脚本时,它可以正常工作并安装MSI。
要在远程服务器(remote.ps1
)上执行的脚本:
$base_url = "https://www.python.org/ftp/python/2.7.13/python-2.7.13.amd64.msi"
$python_filename = [System.IO.Path]::GetFileName($base_url)
$output_filepath = "D:\Temp\$python_filename"
Remove-Item -Path "C:\python27" -Force -Verbose
$installation_path = New-Item -ItemType directory -Path "C:\python27"
$get_pip_url = "https://bootstrap.pypa.io/get-pip.py"
$pip_name = [System.IO.Path]::GetFileName($get_pip_url)
$get_pip_path = "D:\Temp\$pip_name"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest $base_url -OutFile "D:\Temp\$python_filename"
Start-Process "msiexec.exe" -arg "/qn /i $output_filepath InstallAllUsers=1 TargetDir=$installation_path" -Wait
要在本地计算机(local.ps1
)上执行的脚本:
$username = "user1"
$password = "password" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
$script = {
Param(
[String]$Application,
[String]$Environment,
[String]$Region,
[String]$Service,
[String]$Node_Number,
[String]$proxy_sec_method,
[String]$proxy_url
)
$pyinstall = D:\Temp\remote.ps1
$pyinstall
}
Copy-Item -Path "\\localServerName\d$\Monitoring\*" -Destination "\\remoteServerName\d$\Temp" -Force -Recurse
$s = New-PSSession -ComputerName remoteServerName -Credential $cred
Invoke-Command -Session $s -ScriptBlock $script -ArgumentList $Application, $Environment, $Region, $Service, $Node_Number, $proxy_sec_method, $proxy_url -Verbose
在本地脚本中,我尝试将代码行$pyinstall = D:\Temp\remote.ps1
更改为$pyinstall = Invoke-Expression -Command 'D:\Temp\pythoninstallation.ps1'
,但是无法安装MSI。