【问题标题】:software is being installing on a local machine instead of remote软件正在本地机器上而不是远程安装
【发布时间】:2021-05-14 08:21:48
【问题描述】:

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

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

您能帮我找出我在代码中造成的错误吗

【问题讨论】:

  • 您正在本地运行安装命令。您需要在远程设备上运行它。查看调用命令。这个命令有一个远程机器的参数。注意双跳,在调用命令中使用本地文件路径而不是 unc 路径。

标签: powershell installation remote-access


【解决方案1】:

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
}

【讨论】:

    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2019-07-11
    • 2015-01-26
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多