0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows環境で PowerShell + curl を使って ServiceNow API に接続するサンプルスクリプト

Posted at

以下は、Windows環境で PowerShell + curl を使って ServiceNow API に接続するサンプルスクリプトです。
この例では、

  • プロキシサーバーの指定あり
  • プロキシ認証なし
  • ServiceNowのBasic認証を使用
  • curl.exe を明示的に呼び出す(PowerShellには curl のエイリアスがあるため)

という前提で記述しています。


✅ PowerShell スクリプト例

# ServiceNow 接続情報
$instance = "dev12345"  # 例: dev12345
$username = "admin"
$password = "your_password"

# プロキシサーバー情報(認証なし)
$proxyUrl = "http://your.proxy.server:8080"

# ServiceNow API エンドポイント
$url = "https://$instance.service-now.com/api/now/table/incident?sysparm_limit=1"

# curl.exe のパスを明示的に指定(必要に応じてパス修正)
$curlPath = "C:\Windows\System32\curl.exe"

# 認証文字列(Base64エンコード)
$authString = "$username:$password"
$authBase64 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($authString))

# curl コマンドの構築
$curlCommand = @(
    "`"$curlPath`"",
    "--request GET",
    "--url `"$url`"",
    "--header `"Authorization: Basic $authBase64`"",
    "--header `"Accept: application/json`"",
    "--proxy `"$proxyUrl`""
)

# 実行して結果を表示
$commandLine = $curlCommand -join " "
Write-Host "Executing: $commandLine"
Invoke-Expression $commandLine

💡補足ポイント

  • curl.exe のパスを明示的に指定することで、PowerShell内の curl のエイリアス(Invoke-WebRequest)を避けています。
  • --proxy-user オプションを指定しなければ、プロキシにユーザー認証は送られません
  • $authBase64 によって ServiceNow への Basic 認証を実現しています。
  • Invoke-Expression は少々危険なので、信頼できる環境でのみ使用してください。必要に応じて Start-Process 等でも代替可能です。

もし curl の実行結果を PowerShell オブジェクトとして使いたい場合や、レスポンスを整形したいなどありましたら、その点も対応できます!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?