LoginSignup
3
1

More than 1 year has passed since last update.

プリザンターでAPIを利用してデータを更新するpowershell

Posted at

公式マニュアル
https://pleasanter.org/manual/api

#今回は新規作成と更新をサポートすることにする
function Get-ULI(){
    Param($id)
    #idが渡されてくれば、更新URLを返す。URLは自分の環境に合わせて変更してください。
    if($id -eq ''){
        return 'https://example.qiita.com/api/items/1/create'
    } else {
        return "https://example.qiita.com/api/items/$id/update"
    }
}

#前回登録した情報があるかどうかチェック
$idfile = 'c:\users\public\documents\checkerid.txt'
$recordid = ''
if(test-path $idfile){
    #ファイルがある場合は中身を読み込む
    Get-Content -Path $idfile -Encoding Default
    $recordid = Get-Content -Path $idfile -Encoding Default
}

#この辺は決まり事
$header = 'application/json'
$apikey = '利用したいユーザーでプリザンターにログインしてapiキーを発行してください'
$uri = Get-ULI($recordid)
$body = ''

#今回はPC名とログイン名を取ってくることにしました。
$pc = $env:COMPUTERNAME
$user = $env:USERNAME

$body =@{
    apiversion = '1.1'
    apikey = $apikey
    title = $pc    #タイトル
    ClassHash = @{
        ClassA = $pc   #分類A項目
        ClassB = $user #分類B項目
    }
}

$json = $body | ConvertTo-Json

$ret = Invoke-RestMethod -Uri $uri -ContentType $header -Method Post -Body $json
$ret | Select -ExpandProperty Id | Out-File -FilePath $idfile -Force -Encoding default

必要なコードはコピペしたつもりですが、いかんせん、動かしているわけではないので、
全部コピーしても動きません。必要なところだけ参考にしてください。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1