Okay . . . understand that I'm working in the dark, so to speak. I don't have access to the web site in question. So, take what I post on this subject as "educated guesses".
With that said, I don't really have a very elegant way to get the "root" part of the URL except for copying the URL of one of the images. That value (with a bit of judicious manual editing) will replace the $baseUrl value in the code below. You'll basically just be trimming everything after the "/attachments/" to the end of the URL.
Next, I'm not sure that getting the file name from the end of the URL is going to work very well. The HTML encoded attachment name looks like this:
%5B5%5D_%5BImage%5D%5B28%5D_%5BScreenshot%202024-04-06%20074303%5D%5B1%5D_%5B1%5D.png
Which decodes to this: [5]_[Image][28]_[Screenshot 2024-04-06 074303]_[1]_[1].png
And I'm assuming the real name is Screenshot 2024-04-06 074303.png
-- but I'm not sure what the [1] [1]
represent.
The invalid access is solved by your creating a credential with your name/password and using that credential when you use the Invoke-WebRequest.
Take another shot at getting the images with this code. But you may yet need some help/guidance from a SME for the product you're working with. PowerShell can help, but it requires knowing what needs to done!
# URL copied from web site
#https://lists.live.com/personal/2dd00bbfdecb79f6/_api/v2.1/sites('9847079b-02c6-40fe-af8d-034b7cbf1b25,af0163b3-9812-47d9-9485-a8a853f37c4b')/lists('519f099d-d358-4665-9fb1-393b07ce7675')/items('1')/attachments('Reserved_ImageAttachment_%5B5%5D_%5BImage%5D%5B28%5D_%5BScreenshot%202024-04-06%20074303%5D%5B1%5D_%5B1%5D.png')/thumbnails/0/c3000x2000/content?prefer=noredirect%2Cclosestavailablesize
# the part of the URL I _think_ is relevant
$baseUrl = "https://lists.live.com/personal/2dd00bbfdecb79f6/_api/v2.1/sites('9847079b-02c6-40fe-af8d-034b7cbf1b25,af0163b3-9812-47d9-9485-a8a853f37c4b')/lists('519f099d-d358-4665-9fb1-393b07ce7675')/items('1')/attachments/"
$UserName = "your_username"
$Password = Read-Host -AsSecureString "Enter Password"
# Create a PSCredential object
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
# Import the ImportExcel module
Import-Module ImportExcel
# Path to the Excel file
$excelFilePath = "path_to_your_excel_file.xlsx"
# Read the Excel file
$data = Import-Excel -Path $excelFilePath
# Create a directory to save images
$outputDir = "downloaded_images"
if (-Not (Test-Path -Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir
}
# Function to extract the actual URL from the "Reserved_ImageAttachment" format
function Get-ActualImageUrl {
param (
[string]$reservedImageAttachment
)
$imageName = $reservedImageAttachment -replace "Reserved_ImageAttachment_", ""
"{0}{1}" -f $baseUrl, $imageName
}
# Loop through each row and download images
foreach ($row in $data) {
$reservedImageAttachment = $row."Image_URL_Column_Name"
$imageUrl = Get-ActualImageUrl -reservedImageAttachment $reservedImageAttachment
$imageName = [System.IO.Path]::GetFileName($imageUrl)
$imageName = Join-Path -Path $outputDir -ChildPath $imageName
Invoke-WebRequest -Uri $imageUrl -OutFile $imageName -Credential $Credential
}