1
votes

I am trying to do a foreach loop to pull down a file from AWS. When I run Invoke-WebRequest -Uri $S3InstallerLoc -OutFile $S3OutFileoutside of a foreach loop on the server itself it pulls my test file down. When I stick it inside a foreach loop it does not pull down the file. When I attempt to do this from another server I am getting the following error: Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.

Here is the entirety of the script:

$Servers = "AWS-GPOTEST"
$S3InstallerLoc = "https://s3-us-west-2.amazonaws.com/bucketname/test.txt"
$S3OutFile = "C:\Windows\Temp\Test.txt"

ForEach ($Server in $Servers)

{

Invoke-WebRequest -Uri $S3InstallerLoc -OutFile $S3OutFile

}
1

1 Answers

0
votes

In the case that you are running a version of Powershell before 6.0 and that you are running this within a WinRM session you should use the UseBasicParsing parameter.

As in:

Invoke-Command -ComputerName $Servers -ScriptBlock {Invoke-WebRequest -Uri $Using:S3InstallerLoc -OutFile $Using:S3OutFile -UseBasicParsing}

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6

Using local variables in scripts:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6#using-local-variables