WinRPCを使用したバッチスクリプト例

バッチスクリプトを使用してWindowsサービスアカウントの認証情報を更新するPAMスクリプト例

概要

このコード例では、標準のバッチスクリプトを使用してWindowsサービスアカウントの認証情報を更新し、サービスを再起動します。ただし、この例はローカルサーバーで実行されているKeeperゲートウェイでのみ動作します。WinRMスクリプトの使用を推奨します。

以下にご留意ください。

  • レコードのローテーション設定では、バッチスクリプトと特定の記号のエスケープの問題により、「記号」を使用しないようにする必要があります。

  • serverパラメータはサーバー名かIPで更新される必要があります。

サーバーのホスト名は二重バックスラッシュで始まる必要があります。

バッチスクリプト

@echo off

:: Set the server name and service name as variables
set server=\\your-server
set service=SERVICENAME

for /f "tokens=*" %%a in ('more') do set input=%%a

set base64tmp=%temp%\base64.tmp
set json=%temp%\json.tmp

echo %input% > %base64tmp%

certutil -decode %base64tmp% %json%

for /f "usebackq delims=" %%a in (`jq -r .user %json%`) do set "user=%%a"
for /f "usebackq delims=" %%a in (`jq -r .newPassword %json%`) do set "newPassword=%%a"

del %base64tmp%
del %json%

echo "Stopping..."

sc "%server%" query "%service%" | find "STOPPED"
if errorlevel 1 (
    echo "Stopping"
    sc "%server%" stop "%service%"
)

echo "Waiting for service to stop"

set count=1
set limit=120
:loop
sc "%server%" query "%service%" | find "STOPPED"
if errorlevel 1 (
    ping 127.0.0.1 -n 1 > NUL 
    set /a count += 1
    if %count% lss %limit% goto loop
)

sc "%server%" query "%service%" | find "STOPPED"
if errorlevel 1 (
    echo "Timed out"
    exit /b 1
)

echo "Service stopped, waiting 5 seconds"
timeout /t 5 /nobreak >nul

echo "Setting new password"
sc "%server%" config "%service%" obj=%user% password="%newPassword%"
if %errorlevel% neq 0 exit /b %errorlevel%

echo "Updated, waiting 2 seconds"
timeout /t 5 /nobreak >nul

echo "Starting service"
sc "%server%" start "%service%"
if %errorlevel% neq 0 exit /b %errorlevel%

最終更新