Question:
limited knowledge with powershell.
I try to download a image from an image url. for example like this :
“http://hdwallpaperia.com/wp-content/uploads/2014/01/Mc-Laren-P1-Wallpaper-Image-Picture-640×360.jpg”
Before to reach the link, I have to login first. here is My login page html code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Please enter your user name and password below: | User Name: | Password: | Realm: | |
Here is my powershell code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$url = "http://local_machine/example_640x360.jpg" $ie = New-Object -com InternetExplorer.Application $ie.visible = $true $ie.navigate($url) $ie.Document.getElementByid("login_username").value = "$Account" $ie.Document.getElementByid("login_password").value = "$Password" $ie.Document.getElementByid("realm").value = "LDAP" $Log_In=$ie.Document.getElementsByTagName("input") | where-object {$_.type -eq "submit"} $Log_In.click(); while($ie.Busy) {Start-Sleep -s 1} #$ie.Document.Body | Out-File -FilePath "c:\temp\test.jpg" $ie.quit() |
I can successfully login and reach the img link, but don’t know how to download the image. What command can helps me to download?
Answer:
You’re overcomplicating it using COM. I couldn’t test these atm., but they should work.
1 2 3 4 5 6 7 8 9 10 |
#Solution 1 - WebClient $url = "http://www.united.no/wp-content/uploads/2014/03/moyesliver.jpg" $wc = New-Object System.Net.WebClient $wc.DownloadFile($url, "C:\temp\test.jpg") #Solution 2 - never tried this before Invoke-WebRequest $url -OutFile C:\temp\test.jpg |