PowerShell中使用curl(Invoke-WebRequest)的方法教程--轉載
最近需要寫登入的指令碼,查了一下,可以用開發者工具,複製請求,直接生成指令碼。
同時也利用到了下貼的內容
此貼為備查
------------------------------------------------------------------------------------------------
--------------------
前言
PowerShell能幹什麼呢?PowerShell首先是個Shell,定義好了一堆命令與作業系統,特別是與檔案系統互動,能夠啟動應用程式,甚至操縱應用程式;第二,PowerShell允許將幾個命令組合起來放到檔案裡執行,實現檔案級的重用,也就是說有指令碼的性質;第三,PowerShell能夠能夠充分利用.Net型別和COM物件,來簡單地與各種系統互動,完成各種複雜的、自動化的操作。
當我們習慣了windows的介面模式就很難轉去命令列,甚至以命令列發家的git也湧現出各種介面tool。然而命令列真的會比介面快的多,如果你是一個碼農。
situation:接到需求分析bug,需要訪問http。那臺機器屬於product,不允許裝postman。我只能手動命令列來發請求。發現了內建的PowerShell中有curl命令。歡喜試了半天,總是命令不對,google發現這個curl是冒名頂替的,只是一個Invoke-WebRequest的alias。參考。
1 PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize2 3 CommandType Name Version Source 4 ----------- ---- ------- ------ 5 Alias curl -> Invoke-WebRequest 6 Alias iwr -> Invoke-WebRequest 7 Alias wget -> Invoke-WebRequest
Invoke-WebRequest簡單用法
1.用途
獲取http web請求訪問內容
2.語法Syntax
1 Parameter Set: Default 2 Invoke-WebRequest [-Uri] <Uri> [-Body <Object> ] [-Certificate <X509Certificate> ] [-CertificateThumbprint <String> ] [-ContentType <String> ] [-Credential <PSCredential> ] [-DisableKeepAlive] [-Headers <IDictionary> ] [-InFile <String> ] [-MaximumRedirection <Int32> ] [-Method <WebRequestMethod> {Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch} ] [-OutFile <String> ] [-PassThru] [-Proxy <Uri> ] [-ProxyCredential <PSCredential> ] [-ProxyUseDefaultCredentials] [-SessionVariable <String> ] [-TimeoutSec <Int32> ] [-TransferEncoding <String> {chunked | compress | deflate | gzip | identity} ] [-UseBasicParsing] [-UseDefaultCredentials] [-UserAgent <String> ] [-WebSession <WebRequestSession> ] [ <CommonParameters>]
3.簡單的幾個用法
3.1 Get請求
1 PS C:\Users\rmiao> curl -URi https://www.google.com 2 3 StatusCode : 200 4 StatusDescription : OK 5 Content : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many speci..." 6 RawContent : HTTP/1.1 200 OK 7 X-XSS-Protection: 1; mode=block 8 X-Frame-Options: SAMEORIGIN 9 Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32" 10 Vary: Accept-Encoding 11 Transfer-Encoding: chunked
會發現content內容被截斷了。想要獲取完整的content:
1 ps> curl https://www.google.com | Select -ExpandProperty Content3.2新增header
1 -Headers @{"accept"="application/json"}
3.3指定Method
1 -Method Get
3.4將獲取到的content輸出到檔案
1 -OutFile 'c:\Users\rmiao\temp\content.txt'
3.5表單提交1 For example: 2 $R = Invoke-WebRequest http://website.com/login.aspx 3 $R.Forms[0].Name = "MyName" 4 $R.Forms[0].Password = "MyPassword" 5 Invoke-RestMethod http://website.com/service.aspx -Body $R 6 OR 7 Invoke-RestMethod http://website.com/service.aspx -Body $R.Forms[0]
3.6內容篩選
1 PS C:\Users\rmiao> $R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile 2 PS C:\Users\rmiao> $R.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText - 3 First 5 4 5 innerText 6 --------- 7 = 8 9 1 10 Next 11 =
3.7一個登陸示例
1 #傳送一個登陸請求,宣告一個sessionVariable 引數為fb, 將結果儲存在$R 2 #這個變數FB就是header.cookie等集合 3 PS C:\Users\rmiao> $R=curl http://www.facebook.com/login.php -SessionVariable fb 4 PS C:\Users\rmiao> $FB 5 6 7 Headers : {} 8 Cookies : System.Net.CookieContainer 9 UseDefaultCredentials : False 10 Credentials : 11 Certificates : 12 UserAgent : Mozilla/5.0 (Windows NT; Windows NT 6.3; en-US) WindowsPowerShell/4.0 13 Proxy : 14 MaximumRedirection : -1 15 16 17 #將response響應結果中的第一個form屬性賦值給變數Form 18 PS C:\Users\rmiao> $Form=$R.Forms[0] 19 PS C:\Users\rmiao> $Form.fields 20 21 Key Value 22 --- ----- 23 lsd AVqQqrLW 24 display 25 enable_profile_selector 26 isprivate 27 legacy_return 0 28 profile_selector_ids 29 return_session 30 skip_api_login 31 signed_next 32 trynum 1 33 u_0_0 34 u_0_1 35 lgnrnd 214945_qGeg 36 lgnjs n 37 email 38 pass 39 persistent 40 default_persistent 1 41 42 43 44 # 檢視form 45 PS C:\Users\rmiao> $Form | Format-List 46 47 48 Id : login_form 49 Method : post 50 Action : /login.php?login_attempt=1&lwv=100 51 Fields : {[lsd, AVqQqrLW], [display, ], [enable_profile_selector, ], [isprivate, ]...} 52 53 54 #檢視屬性 55 $Form.fields 56 57 #設定賬號密碼 58 $Form.Fields["email"] = "[email protected]" 59 $Form.Fields["pass"] = "P@ssw0rd" 60 61 #傳送請求並儲存結果為$R 62 $R=Invoke-WebRequest -Uri ("https://www.facebook.com" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields 63 64 #檢視結果 65 PS C:\Users\rmiao> $R.StatusDescription 66 OK
雖然沒有curl那麼主流,但一樣可以成為http訪問的一個選擇。
總結
參考
https://technet.microsoft.com/en-us/library/hh849901.aspx