I have a script which logs into a site and navigates to a page with a bunch of PDF files, which I wish to download. A handful of the PDF URLs are from a different domain than the rest. The error I am receiving when trying to download with Start-BitsTransfer (or Invoke-WebRequest) is the following:
Start-BitsTransfer : The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist. (Exception from HRESULT: 0x800704DD)
This message makes me think the browser session is not remaining logged in by the end of the script. Any clues would be much appreciated. Thanks
# Create an instance of internet explorer
$ie = New-Object -com InternetExplorer.Application
# Open IE and make it visible
$ie.visible = $true
# Navigate to home page
$ie.navigate("https://www.franklinamerican.com/ext/general?npage=home")
# Wait for page to finish loading
while ($ie.Busy -eq $true){Start-Sleep -Seconds 3}
# Populate username and password fields, and then click submit button
$ie.Document.getElementById("userName").value = "######" #$WebUID
$ie.Document.getElementById("brokerPassword").value = "######" #$WebPWD
$ie.Document.getElementsByName("Submit").item(0).Click()
# Wait 3 seconds
Start-Sleep -Seconds 3
# Go to download page
$ie.navigate("https://www.franklinamerican.com/ext/correspondent?npage=resourceCenter")
# Wait 3 seconds
Start-Sleep -Seconds 3
# Create an object of iFrame so we can search the page for the element
$frame = $ie.Document.parentWindow.frames[0].document.getElementById("myframe")
# Creates array, $hrefValues
$hrefValues = @()
# Finds HREF, InnerText and inserts into array, $hrefValues
$hrefValues = $frame.contentDocument.getElementsByTagName("a") | ? {$_.href -match '.pdf'} | foreach{
$_ | Select HREF, InnerText
}
# Loops through array, cleans href & text, builds file name, and downloads file to path
ForEach ($i in $hrefValues){
# Remove whitespace from text
$cleanText = $i.innerText.Trim()
# Remove all characters after '('
if ($i.innerText.Contains('(')){
$cleanText = $cleanText.Substring(0,$cleanText.IndexOf('('))
}
# Removes illegal characters from file name
$cleanText = $cleanText.Replace("/", "-")
$cleanText = $cleanText.Replace("\", "-")
$cleanText = $cleanText.Replace(":", "")
$cleanText = $cleanText.Replace("*", "")
$cleanText = $cleanText.Replace("?", "")
$cleanText = $cleanText.Replace("<", "")
$cleanText = $cleanText.Replace(">", "")
$cleanText = $cleanText.Replace("|", "")
# Builds file name
$fileName = $baseFileName + '_' + $cleanText
# Sets URL to href value
$currentURL = $i.Href
Invoke-WebRequest -Uri $currentURL -OutFile $historicalPath\$fileName.pdf
User contributions licensed under CC BY-SA 3.0