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



我的错误代码需要你的帮助,我试图创建我的第一个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:tmpinstall.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

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

脚本修复

## 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 = "\$serverD$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

相关内容

  • 没有找到相关文章

最新更新