从网上下载exe并安装在一组服务器上

时间:2021-05-13 09:20:06

标签: windows powershell windows-installer remote-server

我需要你的帮助来解决我的错误代码,我正在尝试创建我的第一个 ps 脚本,但我失败了: 我的代码应该做什么 - 从我的本地机器连接到远程电脑并启动包下载,然后安装它。

$servers = ('test1','test2')
$path = (New-Item -ItemType Directory -Force -Path "d:\tmp")
$file = "dotnet-4.8.3"
$url = "https://microsoft.com/dotnetcore/5.0/Runtime/5.0.5/win/$file"
$output = "$path\$file"
$destination = "$path.build.checkoutDir%\website\"
$args = @("/install", "/quiet", "/norestart")
$logpath = ("d:\tmp\install.log")

foreach ($server in $servers) {

    $session = New-PSSession -ComputerName $server
    $serverreport =  Invoke-Command -Session $session -Scriptblock {
    Invoke-WebRequest -Uri $url -OutFile $output
    if(!(Split-Path -parent $output) -or !(Test-Path -pathType Container (Split-Path -parent $output))){
      $foutputile = Join-Path $pwd (Split-Path -leaf $output) 
    }  
    
    Write-Verbose "Downloading [$url]`nSaving at [$output]" -Verbose
    $webclient = New-Object System.Net.WebClient
    Write-Verbose $output -Verbose
    $p = Start-Process  -FilePath $output -ArgumentList $args -Wait
    if($p -eq 0)
    {
        Write-Host "installation finished sucessfuly"
    }
        Write-Host "installation failed"
    }
    Remove-Item $output
            }
    
    Remove-PSSession -Session $session

收到此错误信息

New-Item : Cannot find drive. A drive with the name 'd' does not exist.
At line:2 char:10
+ $path = (New-Item -ItemType Directory -Force -Path "d:\tmp")
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (d:String) [New-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
 
Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    + CategoryInfo          : InvalidData: (:) [Invoke-WebRequest], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    + PSComputerName        : test
 
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Split-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
    + PSComputerName        : test
 
VERBOSE: Downloading []
Saving at []
Cannot bind argument to parameter 'Message' because it is null.
    + CategoryInfo          : InvalidData: (:) [Write-Verbose], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.WriteVerboseCommand
    + PSComputerName        : test
 
Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    + CategoryInfo          : InvalidData: (:) [Start-Process], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand
    + PSComputerName        : test 
installation failed
Remove-Item : Cannot find path 'C:\dotnet-4.8.3.exe' because it does not exist.
At line:29 char:5
+     Remove-Item $output
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\dotnet-4.8.3:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

请帮我找出我哪里出错了。哪种方法是改进我的坏代码。 谢谢

1 个答案:

答案 0 :(得分:0)

脚本已修复

## Get list of servers
#$servers = Get-Content C:\listOfServer.txt
$servers = ('test','test2')
# Source file path doesn't change
$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"
    
Invoke-WebRequest -Uri $url -OutFile $sourcefile
Write-Verbose "Downloading [$url]`nSaving at [$sourcefile]" -Verbose

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
}

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
相关问题