1
$dd = Get-Date -Format dd
$mn = Get-Date -Format MMM
$yr = Get-Date -Format yyyy
$umn = $mn.ToUpper()
$dt = $dd + $umn + $yr
$Zfile = 'cm'+$dt+'data.csv.zip'
$Tfile='DATA'+$dt+'.CSV'
$URL = 'https://archives.XYZ.com/content/historical/XYZ/'+$yr+'/'+$umn+'/'+$Zfile
Invoke-WebRequest -URI $URL -OutFile D:\MYDATA\$Zfile
#$URL

I have this script and if I print $URL then I can see https but when run script in error message I can only http and that's why facing "Access Denied"

Invoke-WebRequest : Access Denied
You don't have permission to access "http://archives.XYZ.com/content/historical/XYZ/2021/JULY/MYFILE.csv.zip" on this server.
Reference #18.b5fdd417.1627307177.3a23ec35
At D:\mywork\XYZ.ps1:12 char:1
Invoke-WebRequest -URI $URL -OutFile D:\MYDATA\$Zfi ...
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Check out this thread: https://stackoverflow.com/questions/48603203/powershell-invoke-webrequest-throws-webcmdletresponseexception#48603437 BTW: MalwareBytes reports a Trojan when I try this? – RetiredGeek Jul 26 '21 at 14:31
  • 1
    `$mn = Get-Date -Format MMM` returns the monthname as three-letter abbreviation. Your error message shows `JULY`, which is cleary not abbreviated. Also `MYFILE.csv.zip` is not consistent with the code, where you define the file name as `$Zfile = 'cm'+$dt+'data.csv.zip'`. This makes me feel the error message is **not** from the code you show.. – Theo Jul 26 '21 at 15:11

0 Answers0