4

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-640x360.jpg"

Before to reach the link, I have to login first. here is My login page html code:

<tr>
    <td colspan="2">Please enter your user name and password below:</td>
</tr>
<tr style="height: 10px;"><td></td></tr>
<tr>
    <td>User Name:</td>
    <td><input name="login_username" style="width: 295px;" type="text" size="40" value=""></td>
</tr>
<tr>
    <td>Password:</td>
    <td><input name="login_password" style="width: 295px;" type="password" size="40"></td>
</tr>
<tr>
    <td>Realm:</td>
    <td>
        <select name="realm" style="width: 295px;"><option value="local">Local</option><option value="ldap" selected="">LDAP</option>
        </select>
    </td>
</tr>
    <tr style="height: 10px;"><td></td></tr>
<tr>
    <td><input type="submit" value="Login"></td>
</tr>   

Here is my powershell code :

$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?

Tazugan
  • 129
  • 3
  • 3
  • 9

7 Answers7

27

You're overcomplicating it using COM. I couldn't test these atm., but they should work.

#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
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • Thanks, but actually I need to login the webpage before I download the img link. Can webclient achieved this? – Tazugan Mar 17 '14 at 09:02
  • 2
    Yes, but it depends on the login system. Check out http://stackoverflow.com/questions/508565/how-to-make-an-authenticated-web-request-in-powershell (windows authentication) and http://gallery.technet.microsoft.com/scriptcenter/WenLient-HTML-Login-POST-231f117e#content (form login). You could find more examples on google and here on SO depending on what you need. – Frode F. Mar 17 '14 at 10:10
  • 1
    Solution2 works like a charm. It is very clean and concise as well. Thanks Frode! – Sage Dec 01 '15 at 15:17
  • NB: In addition to `Invoke-WebRequest $url -OutFile C:\temp\test.jpg`, you could first run `if ((Invoke-WebRequest $url -Method Head -OutFile C:\temp\test.jpg).StatusCode -eq 200) {` to check if the file exists without having to download anything; then include an `else` block to handle where that URL's invalid. – JohnLBevan Aug 11 '17 at 19:18
3

this works for me ;)

$wc = New-Object System.Net.WebClient
$wc.DownloadFile("https://asdfasdfxxx.blob.core.windows.net/images/test.png", "C:\test.png")

Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value "C:\test.png"

rundll32.exe user32.dll, UpdatePerUserSystemParameters

kill -n explorer
Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44
dana akary
  • 39
  • 1
2

I would use iwr and bits transfer and make it simple. I went ahead and did it with multiple files for you bc that is more likely than you just going to grab one file. #automateallthethings.

If you want to download multiple files, you can try this:

$URL = "http://www.website.com"

$Site = iwr -Uri $URL

$Images = ($Site).Images.src

foreach ($Image in $Images) {

Start-BitsTransfer -Source $Image -Destination C:\Test\ -TransferType Download

}
Michael
  • 39
  • 1
0

This is for downloading all images(I know that there are 16)

$url="http://www.MySite.tn/images/symboles/"
for ($i=1; $i -lt 16;$i++) {
    Invoke-WebRequest $url/$i.png -OutFile C:\Users\MyUser\Documents\icons\$i.png
    }
hirondelle
  • 31
  • 5
0

Try this, not a completely perfect script but it works as long as you provide the URI with a '/' at the end. Some images are stored on slightly different paths than the URI you may be requesting, so for exmaple here 'google.com/' wasn't working right but making the URI variable 'www.google.com/' fixed the issue.

$path = $null
$filter = $null

$uri = 'www.google.com/'
Write-Host "URI: $uri" -ForegroundColor Green

$filter = $uri | Select-String -AllMatches -Pattern '(\w.+)\/'
$path = $filter.Matches.Groups[1].Value
Write-Host "Server: $path" -ForegroundColor Yellow

$site = Invoke-WebRequest -Uri $uri

$images = $site.Images.src

foreach ($image in $images) {

    Write-Host "Files that are being downloaded:" -BackgroundColor Black -ForegroundColor White
    Write-Host $image -ForegroundColor DarkYellow
    Start-BitsTransfer -Source "http://$path$image" -Destination '.\TEMP\' -TransferType Download

                            }
skrap3e
  • 143
  • 1
  • 1
  • 8
0
$user = (whoami). Split("\")[1]
$url = "https://thumbs.dreamstime.com/b/funny-face-baby-27701492.jpg"
$dest = "C:\Users\"+$user+"\Desktop\baby.jpg"
Invoke-WebRequest $url -OutFile $dest

this just puts an image onto your desktop you can change it to whatever

PDG100
  • 1
-1

`$path = $null $filter = $null

$uri = 'www.google.com/'
Write-Host "URI: $uri" -ForegroundColor Green

$filter = $uri | Select-String -AllMatches -Pattern '(\w.+)\/'
$path = $filter.Matches.Groups[1].Value
Write-Host "Server: $path" -ForegroundColor Yellow

$site = for ($i=1; $i -lt 2;$i++) {Invoke-WebRequest -Uri $uri -ContentType Images -UseBasicParsing -DisableKeepAlive }

$images = $site.Images.src
$images

#again, this ONLY gives you the url path, not the file...

Patrick Burwell
  • 129
  • 1
  • 12