Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 软件正在本地计算机而不是远程计算机上安装_Powershell_Installation_Remote Access - Fatal编程技术网

Powershell 软件正在本地计算机而不是远程计算机上安装

Powershell 软件正在本地计算机而不是远程计算机上安装,powershell,installation,remote-access,Powershell,Installation,Remote Access,大家好,在我的代码中,我犯了一个错误,没有在远程安装,而是尝试在本地安装 ## 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.

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

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

你能帮我找到我代码中的错误吗?启动进程在本地机器上启动进程(参考)。 您可以尝试在远程计算机上使用会话(
New-psession
Enter-psession
)运行该进程,因此它看起来像这样(未测试,我更喜欢检查连接等):


您正在本地运行install命令。您需要在远程设备上运行它。签出invoke命令。此命令具有远程机器的参数。注意双跳,在invoke命令中使用本地文件路径,而不是unc路径。
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
}