Powershell - Download Image from an image url

天大地大妈咪最大 提交于 2019-12-07 13:17:08

问题


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?


回答1:


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



回答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

}



回答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


来源:https://stackoverflow.com/questions/22447326/powershell-download-image-from-an-image-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!