Powershell - 从图像网址下载图像

Taz*_*gan 3 powershell webclient

与PowerShell有限的知识.
我尝试从图片网址下载图片.例如:" http://hdwallpaperia.com/wp-content/uploads/2014/01/Mc-Laren-P1-Wallpaper-Image-Picture-640x360.jpg "

在到达链接之前,我必须先登录.这是我的登录页面html代码:

<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>   
Run Code Online (Sandbox Code Playgroud)

这是我的powershell代码:

$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()
Run Code Online (Sandbox Code Playgroud)

我可以成功登录并访问img链接,但不知道如何下载图像.什么命令可以帮助我下载?

Fro*_* F. 11

你使用COM过度复杂了.我无法测试这些atm.但它们应该可以工作.

#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
Run Code Online (Sandbox Code Playgroud)

  • 是的,但这取决于登录系统。查看http://stackoverflow.com/questions/508565/how-to-make-an-authenticated-web-request-in-powershell(Windows身份验证)和http://gallery.technet.microsoft.com/scriptcenter/ WenLient-HTML-Login-POST-231f117e#content(表单登录)。您可以根据您的需要在谷歌和此处找到更多示例。 (2认同)
  • Solution2 很有魅力。它也非常干净简洁。谢谢弗罗德! (2认同)