PowerShell利用Curl发送请求

2020-01-16  本文已影响0人  wwmin_

前言

PowerShell能干什么呢?PowerShell首先是个Shell,定义好了一堆命令与操作系统,特别是与文件系统交互,能够启动应用程序,甚至操纵应用程序;第二,PowerShell允许将几个命令组合起来放到文件里执行,实现文件级的重用,也就是说有脚本的性质;第三,PowerShell能够能够充分利用.Net类型和COM对象,来简单地与各种系统交互,完成各种复杂的、自动化的操作。

当我们习惯了windows的界面模式就很难转去命令行,甚至以命令行发家的git也涌现出各种界面tool。然而命令行真的会比界面快的多,如果你是一个码农。

需求

接到需求分析bug,需要访问http。那台机器属于product,不允许装postman。我只能手动命令行来发请求。发现了内置的PowerShell中有curl命令。试了半天,总是命令不对,google发现这个curl是冒名顶替的,只是一个Invoke-WebRequest的alias。参考

PS C:\Users\wweim> get-alias -definition invoke-webrequest | format-table -autosize

CommandType Name                      Version Source
----------- ----                      ------- ------
Alias       curl -> Invoke-WebRequest
Alias       iwr -> Invoke-WebRequest
Alias       wget -> Invoke-WebRequest

powershell中使用curl

获取帮助:
get-help invoke-webrequest 或者 get-help invoke-webrequest -online
online参数会打开官方文档powershell

PS C:\Users\wweim> get-help invoke-webrequest

名称
    Invoke-WebRequest

语法
    Invoke-WebRequest [-Uri] <uri>  [<CommonParameters>]


别名
    iwr
    wget
    curl


备注
    Get-Help 在此计算机上找不到该 cmdlet 的帮助文件。它仅显示部分帮助。
        -- 若要下载并安装包含此 cmdlet 的模块的帮助文件,请使用 Update-Help。
        -- 若要联机查看此 cmdlet 的帮助主题,请键入: "Get-Help Invoke-WebRequest -Online" 或
           转到 https://go.microsoft.com/fwlink/?LinkID=217035。

可以看到curl也是别名,linux的wget也在其中,这样为习惯其他平台的用户提供了方便,但是用法上也稍有区别
基本用法介绍:

#请求地址
- Uri $uri
#添加header
-Headers @{"content-type"="application/json";"authorization"="bearer token"}
#指定Method
-Method Get
#将获取到的content输出到文件
-OutFile 'c:\Users\rmiao\temp\content.txt'
PS C:\Users\wweim> $R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile

查看返回结果

PS C:\Users\wweim> $R
StatusCode        : 200
StatusDescription : OK
Content           : <!DOCTYPE html><html lang="zh" xml:lang="zh" 
RawContent        : HTTP/1.1 200 OK
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 118264
...... # 省略n多字符

内容筛选

PS C:\Users\wweim> $R.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText

返回结果

innerText
---------
下一页
....#省略n多字符
$uri = 'https://www.pstips.net/restapi/v2'
 
$hash = @{ name = "$name";
           pwd = "$passwd";
        }
$headers = @{"accept"="application/json"}
$JSON = $hash | convertto-json
curl -uri $uri -Method POST -Body $JSON

示例:

PS C:\Users\wweim> $uri = "http://localhost:8001/api/Account/login"
PS C:\Users\wweim> $header = @{"content-type"="application/json"}
PS C:\Users\wweim> $hash = @{userName='bz';password='1';}
PS C:\Users\wweim> $json = $hash | convertto-json
PS C:\Users\wweim> curl -h $header -uri $uri -method post -body $json

得到如下内容

PS C:\Users\wweim> curl -h $header -uri $uri -method post -body $json

StatusCode        : 200
StatusDescription : OK
Content           : {"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi6YOo6ZW_IiwiaHR0cDovL3NjaGVtYXMueG1s
                    c29hcC5vcmcvd3MvMjAwNS8wNS9pZGVud...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    Content-Type: application/json; charset=utf-8
                    Date: Thu, 16 Jan 2020 02:15:32 GMT
                    Server: nginx/1.14.0 (Ubuntu)

                    {"token":"eyJh...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [Content-Type, application/json; charset=utf-8], [Date, Thu, 16 Jan 2020 02:15:32 GMT]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 1818

会发现content内容被截断了,想要获取完整的content,可加参数| Select -ExpandProperty Content

PS C:\Users\wweim> curl -h $header -uri $uri -method post -body $json | select -expandproperty content
{"token":"XJ5Z3JvdXBz","userInfo":{"id":"08ef7b95-c4aa-4006-a074-66eaf0ff1974","companyId":"77a95763-3c6e-4296-8fd6-14d62316d535","roleId":"28b0acf3-76b0-4965-95cb-da2f5bc63bc2","creationTime":"0001-01-01 00:00:00","creatorId":"00000000-0000-0000-0000-000000000000","lastModificationTime":"0001-01-01 00:00:00","lastModifierId":"00000000-0000-0000-0000-000000000000","isDeleted":false,"userName":"部 长","gender":1,"dateOfBirth":"0001-01-01 00:00:00","email":"","phoneNumber":"132231121","avatarUrl":"","state":1,"publicOpenId":null,"miniOpenId":null},"companyInfo":{"id":"77a95763-3c6e-4296-8fd6-14d62316d535","name":"1jcsgs","shortName":"分局","introduction":"分局","level":1,"parentId":"00000000-0000-0000-0000-000000000000","state":1,"baiduX":"","baiduY":""},"roleInfo":{"id":"28b0acf3-76b0-4965-95cb-da2f5bc63bc2","name":"部长","normalizedName":"部长"},"expiration":"2020-01-17 10:22:23"}

此时content内容是完整的

PS C:\Users\wweim> curl -h $header -uri $uri -method post -body $json -outfile "d:\content.txt"

d:\content.txt可看到输出的内容

其他用法:

# fotmat-list查看$hash
PS C:\Users\wweim> $hash | format-list
Name  : password
Value : 1

Name  : userName
Value : bz
#查看属性
PS C:\Users\wweim> $hash.userName
bz
#属性赋值
PS C:\Users\wweim> $hash.userName = "wwmin"
PS C:\Users\wweim> $hash

Name                           Value
----                           -----
userName                       wwmin
password                       1
#将请求结果保存到变量
PS C:\Users\wweim> $res=curl -h $header -uri $uri -method post -body $json | select content
PS C:\Users\wweim> $res

Content
-------
{"token":"eyJhbGc..."}
PS C:\Users\wweim> $res.Content
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5..."}

将content内容转为对象ConvertFrom-json

PS C:\Users\wweim> $data = ConvertFrom-json -inputobject $res.content
PS C:\Users\wweim> $data.token
eyJhbGciOiJIUzI1NiIsInR5cCI....

你可以使用ConvertTo-Json转回JSON格式

PS C:\Users\wweim> $data | convertto-json
{
    "token":  "eyJhbGciOiJIUzI1NiI...",
}

请求带有token的uri, 其实就是在header中添加authorization对象

PS C:\Users\wweim> $data.token ="Bearer "+ $data.token
PS C:\Users\wweim> $heads = @{"authorization"=$data.token}
PS C:\Users\wweim> $uri="http://localhost:8001/api/ProjectInfo/preview?startYear=2019&endYear=2020"
PS C:\Users\wweim> curl -h $heads -uri $uri | select -ExpandProperty content

#返回的结果
[{"children":[],"name":"全部"]

到此,接口请求基本够用

使用脚本文件

PS C:\Users\wweim> "hello world" >d:\hello.ps1

执行

PS D:\> hello.ps1
hello.ps1 : 无法将“hello.ps1”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确
保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ hello.ps1
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (hello.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

没有权限,也是安全机制,可参考官网

PS D:\> Get-ExecutionPolicy -list

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine    RemoteSigned

用管理员权限打开powershell,并输入命令,然后选择y

PS C:\WINDOWS\system32> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): y

说明:
Restricted 执行策略不允许任何脚本运行。
AllSigned 和 RemoteSigned 执行策略可防止 Windows PowerShell 运行没有数字签名的脚本

使用Windows PowerShell ISE编辑脚本

Windows PowerShell ISE
  1. 将文本文件保存为.ps1后缀文件之后,右键编辑即可打开
  2. 在win10搜索框中输入ise
    ISE
$login_uri = "http://localhost:8001/api/Account/login"
$header = @{"content-type"="application/json"}
$hash = @{userName="bz";password="1"}
$json = $hash | ConvertTo-Json
$res = Curl -uri $login_uri -h $header -Method post -Body $json | select -ExpandProperty Content
$content = ConvertFrom-Json $res

$authHeader= @{"authorization"="Bearer "+$content.token}
$year = Get-Date | select year
$info_uri="http://localhost:8001/api/ProjectInfo/preview?startYear="+($year.Year-1)+"&endYear="+$year.Year
$data = curl -h $authHeader -uri $info_uri | select -ExpandProperty content
$data # 此处仅为了在控制台中显示数据

正常执行.

像命令一样执行脚本

怎样像执行一个命令一样执行一个脚本,不用输入脚本的相对路径或者绝对路径,甚至*.ps1扩展名。
那就将脚本的执行语句保存为别名吧:

PS D:\> Set-Alias testUri D:\hello.ps1

PS D:\> testUri
[{"children":[],"name":"全部"]

完美执行.
到此powershell的使用curl发送请求的需求基本满足.

上一篇 下一篇

猜你喜欢

热点阅读