软件正在本地机器上而不是远程安装

software is being installing on a local machine instead of remote

大家好,我在代码中的某处犯了一个错误,我没有在远程安装,而是尝试在本地安装

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