powershell - 软件正在本地机器上而不是远程安装
问题描述
大家好,在我的代码的某个地方我犯了一个错误,而不是在远程安装它尝试在本地安装
## Get list of servers
#$servers = Get-Content C:\listOfServer.txt
$servers = ('test','test2')
$url = "https:/microsoft.com/dotnetcore/5.0/Runtime/5.0.2/win/$file"
$file = "dotnet-hosting-5.0.2-win.exe"
$args = @("/install", "/quiet", "/norestart")
$path = ("c:\tmp")
$sourcefile = "$path\$file"
$remotefile = "$destinationPath\$file"
#download file on a local machine
Invoke-WebRequest -Uri $url -OutFile $sourcefile
Write-Verbose "Downloading [$url]`nSaving at [$sourcefile]" -Verbose
#connecting to remote machine and checking for dir existed
foreach($server in $servers) {
# Destination UNC path changes based on server name
$destinationPath = "\\$server\D$\tmp\"
# Check that full folder structure exists and create if it doesn't
if(!(Test-Path $destinationPath)) {
# -Force will create any intermediate folders
New-Item -ItemType Directory -Force -Path $destinationPath
}
# Copy the file across
Copy-Item $sourcefile $destinationPath
}
#PART WHICH IS NOT WORKING: It installs on a local machine
foreach($server in $servers) {
$p = Start-Process -FilePath $remotefile -ArgumentList $args -Wait
if($p -eq 0)
{
Write-Host "installation finished sucessfuly"
}
Write-Host "installation failed"
}
Remove-Item $remotefile
你能帮我找出我在代码中犯的错误吗
解决方案
Start-Process 在本地机器上启动进程(参考https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1)。您可以尝试使用会话(New-PSSesssion
或Enter-PSSession
)在远程计算机上运行该过程,因此它看起来像这样(未经测试,我更喜欢检查连接等):
foreach($server in $servers) {
$session = (New-PSSession $server)
#two approaches - change remote path to local path
#for sake of invoking command or stay with the remote path
#as the machine should work with it
$sb = {
param (
[string] $path
)
$args = @("/install", "/quiet", "/norestart")
$p = (Start-Process -FilePath $path -ArgumentList $args -Wait)
if ($p -eq 0)
{
Write-Host "installation finished sucessfuly"
}
Write-Host "installation failed"
}
Invoke-Command -Session $session -ScriptBlock $sb -ArgumentList $remotefile
Remove-Item $remotefile -Verbose
}
推荐阅读
- python - 将单个组中的 pandas 数据帧迭代地附加到 h5 文件
- swift - Xcode 错误不会让我点击注册或登录
- javascript - 在js中禁用一些id
- r - 问:如何应用多重 LASSO 回归?
- windows - 获取内置 Windows 用户帐户的本地化名称的最短方法是什么?
- c - 用户定义的一维数组的加法和平均函数
- python - 每次方法调用都会自动调用 __init__ 吗?
- ruby-on-rails - 检查字符串是否包含 Ruby 中的表情符号
- pandas-groupby - 按国家/地区对所有属性应用 z 分数
- r - R找到向量x中大于向量y中的值的值的计数