Invoke-WebRequestコマンドレットを使用することにより、wgetのようにウェブページ(HTML)を簡単に取得することができます。
以下に実行例等を記します。
PS C:\> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 1 16299 251
PS C:\> (Get-WmiObject Win32_OperatingSystem).Version 10.0.16299
Invoke-WebRequestコマンドレットをすることにより、簡単にウェブページ(HTML)を取得することができます。
下記コマンドを実際に実行したときの出力です。
Invoke-WebRequest URL
PS C:\> Invoke-WebRequest http://just4fun.biz
StatusCode : 200
StatusDescription : OK
Content : <html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>www.just4fun.biz</title>
<style type="text/css">
html, body {
width: 100%;
height: 100%;
margin:...
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
Vary: Accept-Encoding
Accept-Ranges: bytes
Content-Length: 2294
Content-Type: text/html
Date: Tue, 17 Apr 2018 03:31:31 GMT
Last-Modified: Fri, 14 Jul 201...
Forms : {}
Headers : {[Connection, keep-alive], [Vary, Accept-Encoding], [Accept-Ranges, bytes], [Content-Length, 2294].
..}
Images : {@{innerHTML=; innerText=; outerHTML=<IMG src="http://linux.just4fun.biz/image/logo.png">; outerTex
t=; tagName=IMG; src=http://linux.just4fun.biz/image/logo.png}, @{innerHTML=; innerText=; outerHTML
=<IMG src="http://win.just4fun.biz/image/logo.png">; outerText=; tagName=IMG; src=http://win.just4f
un.biz/image/logo.png}, @{innerHTML=; innerText=; outerHTML=<IMG src="http://db.just4fun.biz/image/
logo.png">; outerText=; tagName=IMG; src=http://db.just4fun.biz/image/logo.png}, @{innerHTML=; inne
rText=; outerHTML=<IMG src="http://cryptocurrency.just4fun.biz/image/logo.png">; outerText=; tagNam
e=IMG; src=http://cryptocurrency.just4fun.biz/image/logo.png}...}
InputFields : {}
Links : {@{innerHTML=<IMG src="http://linux.just4fun.biz/image/logo.png"> ; innerText= ; outerHTML=<A href=
"http://linux.just4fun.biz"><IMG src="http://linux.just4fun.biz/image/logo.png"> </A>; outerText= ;
tagName=A; href=http://linux.just4fun.biz}, @{innerHTML=Linuxã¨éãã; innerText=Linuxã¨é
ãã; outerHTML=<A href="http://linux.just4fun.biz">Linuxã¨éãã</A>; outerText=Linuxã¨é
ãã; tagName=A; href=http://linux.just4fun.biz}, @{innerHTML=<IMG src="http://win.just4fun.biz/
image/logo.png"> ; innerText= ; outerHTML=<A href="http://win.just4fun.biz"><IMG src="http://win.ju
st4fun.biz/image/logo.png"> </A>; outerText= ; tagName=A; href=http://win.just4fun.biz}, @{innerHTM
L=Windowsã¨æ®ãã; innerText=Windowsã¨æ®ãã; outerHTML=<A href="http://win.just4fun.biz"
>Windowsã¨æ®ãã</A>; outerText=Windowsã¨æ®ãã; tagName=A; href=http://win.just4fun.biz}
...}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 2294
以下の通り、HtmlWebResponseObjectが返却されているのが確認できます。
PS C:\> (Invoke-WebRequest http://just4fun.biz).GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False HtmlWebResponseObject Microsoft.PowerShell.Commands.WebResponseObject
以下のようにメンバーを指定すれば、HTTPステータスなども取得できます。
PS C:\> $html=(Invoke-WebRequest http://just4fun.biz) PS C:\> $html.StatusCode 200 PS C:\> $html.RawContentLength 2294
ファイルに出力したい場合は、以下のようになります。
Invoke-WebRequest URL -OutFile ファイル名
実際に実行したときの出力は以下の通りです。
PS C:\Users\sakura\Desktop> Invoke-WebRequest http://just4fun.biz -OutFile html.txt
PS C:\Users\sakura\Desktop> Get-ChildItem .\html.txt
ディレクトリ: C:\Users\sakura\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2018/04/17 12:37 2294 html.txt
ファイルに出力されているのが確認できます。
Invoke-WebRequestの-UseBasicParsingを使うとブラウザエンジンを使わず、一般的なパースのみを行うそうです。
参考記事
上記の参考記事を参考にさせて頂きますと、AllElements、Forms、Scriptsのプロパティ値が取得できないようです。
もし、HTML以外の要素で上行のプロパティ必要とする場合は(ブラウザエンジンによるパースを利用する場合)、-UseBasicParsingを
付与しないでください。