首页 > 解决方案 > 软件正在本地机器上而不是远程安装

问题描述

大家好,在我的代码的某个地方我犯了一个错误,而不是在远程安装它尝试在本地安装

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

你能帮我找出我在代码中犯的错误吗

标签: powershellinstallationremote-access

解决方案


Start-Process 在本地机器上启动进程(参考https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1)。您可以尝试使用会话(New-PSSesssionEnter-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
}

推荐阅读