サーチ…


前書き

RESTは、Representational State Transfer(時には「ReST」と書かれています)の略です。これは、ステートレスのクライアントサーバー、キャッシュ可能な通信プロトコルに依存し、ほとんどがHTTPプロトコルが使用されます。主に、軽量で、保守性があり、スケーラビリティの高いWebサービスを構築するために使用されます。 RESTに基づくサービスはRESTfulなサービスと呼ばれ、そのために使用されているAPIはRESTfulなAPIです。 PowerShellでは、 Invoke-RestMethodがそれらを処理するために使用されます。

Slack.comの着信Webhookを使用する

より複雑なデータを送信するためのペイロードを定義する

$Payload = @{ text="test string"; username="testuser" }

ConvertTo-JsonコマンドレットとInvoke-RestMethodを使用して呼び出しを実行する

Invoke-RestMethod -Uri "https://hooks.slack.com/services/yourwebhookstring" -Method Post -Body (ConvertTo-Json $Payload) 

hipChatへの投稿メッセージ

$params = @{
    Uri = "https://your.hipchat.com/v2/room/934419/notification?auth_token=???"
    Method = "POST"
    Body = @{
        color = 'yellow'
        message = "This is a test message!"
        notify = $false 
        message_format = "text"
    } | ConvertTo-Json
    ContentType = 'application/json'
}

Invoke-RestMethod @params 

PowerShellオブジェクトでRESTを使用して個々のデータを取得および配置する

RESTデータを取得し、PowerShellオブジェクトに格納します。

$Post = Invoke-RestMethod -Uri "http://jsonplaceholder.typicode.com/posts/1"

データを変更する:

$Post.title = "New Title"

RESTデータを戻します。

$Json = $Post | ConvertTo-Json
Invoke-RestMethod -Method Put -Uri "http://jsonplaceholder.typicode.com/posts/1" -Body $Json -ContentType 'application/json'

PowerShellオブジェクトでRESTを使用して多くのアイテムを取得およびPOSTする

RESTデータを取得し、PowerShellオブジェクトに格納します。

$Users = Invoke-RestMethod -Uri "http://jsonplaceholder.typicode.com/users"

データ内の多くの項目を変更する:

$Users[0].name = "John Smith"
$Users[0].email = "[email protected]"
$Users[1].name = "Jane Smith"
$Users[1].email = "[email protected]"

すべてのRESTデータをPOSTします。

$Json = $Users | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri "http://jsonplaceholder.typicode.com/users" -Body $Json -ContentType 'application/json'

PowerShellでのRESTを使用したアイテムの削除

削除するアイテムを特定して削除します。

Invoke-RestMethod -Method Delete -Uri "http://jsonplaceholder.typicode.com/posts/1"


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow