ACC プラグインパッケージの作成
修復アクションを定義する PowerShell スクリプト、Ruby スクリプト、およびエンドポイントデバイスで修復アクションを実行できるようにする許可リストを含む エージェントクライアントコレクター (ACC) プラグインパッケージを作成します。
始める前に
必要なロール:admin
手順
-
ローカルコンピューターに ACC プラグインのルートフォルダーを作成し、名前を付けます。
acc-dex-custom などです。
- ルートフォルダーに、 allowlist と bin という名前の 2 つのサブフォルダーを作成します。
-
PowerShell またはコマンドプロンプトコマンドを使用して PowerShell スクリプトを作成し、エンドポイントデバイスで実行する修復アクションを定義します。
-
bin フォルダーに .ps1 ファイルを作成し、拡張子 .ps1 という名前を付けます。
例: CustomScript.ps1
-
.ps1 ファイルを開き、コマンドを追加します。
たとえば、次のサンプル .ps1 ファイルでは、デバイス上の W32Time プロセスを停止および開始し、一時停止して再起動するコマンドを使用して、カスタム修復アクションを定義します。
sc.exe stop w32time $process = Get-Process -Name w32time -ErrorAction SilentlyContinue if ($process) { taskkill /f /t /im w32tm.exe } Start-Sleep -Seconds 5 sc.exe start w32time Start-Process "C:\Windows\System32\w32tm.exe" Start-Sleep -Seconds 5 sc.exe stop spooler sc.exe start spooler注:実行時に ServiceNow インスタンスで定義された入力パラメーターを受け入れるように PowerShell スクリプトを構成できます。 - ファイルを保存します。
-
bin フォルダーに .ps1 ファイルを作成し、拡張子 .ps1 という名前を付けます。
-
ServiceNow インスタンスから .ps1 ファイルを呼び出す Ruby スクリプトを作成します。
- bin フォルダーで Ruby ファイルを作成し、拡張子 .rb という名前を付けます。
-
.rb ファイルを開き、プレーンな Ruby または任意の Ruby フレームワークを使用してコードを追加します。
Ruby スクリプトには、次のロジックが含まれている必要があります。
- 作成した .ps1 スクリプトを呼び出し、正しいファイルパスを入力していることを確認します。
- コマンドが正常に実行されたかどうかを検証します。
- 実行ステータス、出力、およびエラー (存在する場合) を含む出力 JSON を作成します。
-
コマンドを実行し、出力を確認して、 .ps1 ファイルが正常に呼び出されたかどうか、またはエラーがあるかどうかを確認します。
たとえば、次のサンプルの .rb ファイルは、acc-f-commons プラグインの run コマンドを使用して .ps1 ファイル (CustomScript.ps1) を実行し、コマンドが正常に実行されたかどうかを検証して、結果 JSONを返します。
require "logger" require_relative "../../../acc-f-commons/bin/command_runner.rb" require "json" class CustomScript def initialize() @logger = Logger.new(STDOUT) @cmd_runner = CommandRunner.new @logger.level = "FATAL" end def execute_custom_script() status = "failure" msg = nil data = {} parent_cache_directory = File.expand_path("../..", File.dirname(__FILE__)).gsub(" ", "` ") query = "powershell -ExecutionPolicy Bypass " + parent_cache_directory + "/acc-dex-custom/bin/ps/customScript.ps1" output, error = @cmd_runner.runCmd(query, @logger, "mode con:cols=32766 lines=1 & chcp 65001 > nul & ", true, false) if !error && !output.empty? status = "success" msg = "Successfully ran script" data = output else msg = "Failed - #{error}" end result = { "status" => status, "type" => "device", "metric_type" => "remedial_action", "keys" => "custom_script", "data" => data, "message" => msg } puts JSON.generate(result) end end custom_script = CustomScript.new custom_script.execute_custom_script()注:PowerShell スクリプトが ServiceNow インスタンスからの入力パラメーターを受け入れるように構成されている場合、Ruby スクリプトは実行中にこれらのパラメーターを PowerShell スクリプトに渡します。
-
許可リストファイルを作成し、エンドポイントデバイスでの実行を許可する Ruby スクリプトを指定します。
-
allowlist サブフォルダーに、allowlist JSONファイルを作成します。
たとえば、 check-allowlist.json などです。
- .rb スクリプトの一部として受け入れられる引数などの条件を定義するか、カスタムロジックを作成します。
- ファイルを保存します。
-
allowlist サブフォルダーに、allowlist JSONファイルを作成します。
-
任意のユーティリティを使用して、ACC プラグインフォルダー用の tar.gz ファイルを生成します。
注:tar.gzファイル名はプラグインのルートフォルダー名と一致する必要があります。たとえば、 acc-dex-custom.tar.gz