一朋友苦于Symantec的更新包链接总是动态生成,没有规律可循,例如:“http://definitions.symantec.com/defs/2014xxxx-xxx-xxxxx.exe“此朋友因困于无法得到准确的链接,而无法使用脚本自动下载该更新包。
我们发现在Powershell 3.0以上的版本中可以使用Invoke-WebRequest获取其网页的HTML源码,获取的值其类型为一段约60000多长度的字符串。于是想到利用正则表达式中Matches 提取其中相关值,尝试以下代码:
$webreq = Invoke-WebRequest http://www.symantec.com/security_response/definitions/download/detail.jsp?gid=savce $HTMLstring = $webreq.Content [regex]::Matches("$HTMLstring",'http://definitions.symantec.com/defs/.{0,20}\.exe')|%{$_.value}
得到输出结果:
http://definitions.symantec.com/defs/20140124-016-v5i32.exe http://definitions.symantec.com/defs/20140124-016-v5i64.exe http://definitions.symantec.com/defs/20140124-004-i32.exe http://definitions.symantec.com/defs/20140124-004-i64.exe
到这一步,我们已经把问题的解决核心思路阐述完毕了,后面的代码就非常好处理,这里就不在阐述了。
本文链接: https://www.pstips.net/invoke-webrequest-down.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
我就是你说的那位“朋友”,没想到正则表达式也可以完成这项工作,学习了:)
ps:话说您也上winos?
呵呵,是的。
ps基础课程(http://www.pstips.net/powershell-v3-basic),提到好多次后续会有几节高级课程并已经制作完成,能否给我视频链接?
#url=http://www.symantec.com/security_response/definitions/download/detail.jsp?gid=sep$w=Invoke-WebRequest $url -UseBasicParsing$w.Links.href -like “http://*v5i32.exe”结果:http://definitions.symantec.com/defs/20140514-034-v5i32.exe网页上还提供了MD5,再下载完成后,甚至可以用Get-FileHash来校验下
谢谢分享