How-To (CM-Self-Hosted): Create and debug adaptable scripts

How-To (CM-Self-Hosted): Create and debug adaptable scripts

115
Created On 11/12/26 15:36 PM - Last Modified 11/20/26 20:06 PM


Objective


This article covers methods to quickly create and debug adaptable scripts. See reference documentation:

  1. Adaptable Log channel
  2. VenafiPS - PowerShell Module


Environment


Certificate Manager-Self-Hosted



Procedure


  1. Create a new PS1 file and name it "#000 - Adaptable Log Template.ps1". (You can customize the name).
  2.  Copy and paste the code from the "Additional Information" section into your PS1 file. (Scroll down to bottom of this article).
  3. Install PS1 script on your TPP server at this path: (C:\Program Files\Venafi\Scripts\AdaptableLog).
  4. Create Adaptable Log object with the following settings:
  5. See table for field descriptions:
    1. NameValueDescription
      Service AddressFQDNTPP server hostname/FQDN
      Import VenafiPSBooleanImport VenafiPS module
      Create VenafiPS SessionBooleanAttempt to create VenafiPS session
      Log All Variable DataBooleanLog all system variables in debug log
      Log Event DataBooleanLog Events hash table in debug log
      Log Fields DataBooleanLog Fields hash table in debug log
      Log General DataBooleanLog General hash table in debug log
      OAuth Token Application IDClient IDClient ID of your API integration
      OAuth Token CredentialCredential ObjectCredentials for API integration
  6. Create a notification rule to trigger the script.
    1. (eventid == 0x000C0033 || eventid == 0x00290032) && component == "\\VED\\Policy\\Certificates\\_Log Trigger\\log-trigger-tppshako.com"
  7. Use these commands to quickly invoke the script and make changes:
    1. Import-Module venafips
      
      # Update VenafiPS module
      Update-Module -Name VenafiPs
      
      # Create new VenafiPS session
      $server = 'ws2022-c1n3.tppshako.com'
      $accessCred = 'ZBcdbEE+60tE5Yg2Lphb2w=='
      New-TrustClient -Server $server -AccessToken $accessCred
      
      # Use this to trigger the Adaptable Log
      Invoke-VdcCertificateAction -Validate -Path "\VED\Policy\Certificates\_Log Trigger\log-trigger-tppshako.com"
      
      # Update the script hash when you make changes to the script
      Add-CmAdaptableHash -Path '\VED\Logging\Channels\Production\#000 - Adaptable Log Template' `
      -FilePath "C:\Program Files\Venafi\Scripts\AdaptableLog\#000 - Adaptable Log Template.ps1"
  8. A log file should be generated at this path once you invoke the adaptable log: (C:\Program Files\Venafi\Logs).
  9. You can view the live updates to the adaptable log file using this command (Update the path to match your environment):
    1. Get-Content -Path "C:\Program Files\Venafi\Logs\#000 - Adaptable Log Template_{8af2bce9-6181-4e19-a56c-eba981a7bdb6}_debug.log" -Wait
  10. You can add custom logic to the script to fit your goals.
  11. Process complete.


Additional Information


<#
//------------------------------------------------------------------------
// Adaptable Log Channel Driver PowerShell Script Template
//
// Copyright (c) 2017 CyberArk Software Ltd.  All rights reserved.
//
// This sample script and its contents are provided by CyberArk to customers
// and authorized technology partners for the purposes of integrating with
// services and platforms that are not owned or supported by CyberArk .  Any
// sharing of this script or its contents without consent from CyberArk is
// prohibited.
//
// Provided as-is without warranty or support.
//------------------------------------------------------------------------

<field name>|<label text>|<flags>

Bit 1 = Enabled
Bit 2 = Mandatory

-----BEGIN FIELD DEFINITIONS-----
Text1|Text Field #1|00
Text2|Text Field #2|00
Text3|Text Field #3|00
Text4|Text Field #4|00
Text5|Text Field #5|00
Text6|Text Field #6|00
Text7|Import VenafiPS|100
Text8|Create VenafiPS Session|100
Text9|Log All Variable Data|100
Text10|Log Event Data|100
Text11|Log Fields Data|100
Text12|Log General Data|100
-----END FIELD DEFINITIONS-----
#>

<######################################################################################################################
.NAME
    Perform-Action
.DESCRIPTION
    Performs an action in response to the logging of an event by Trust Protection Platform.
.PARAMETER General
    A hashtable containing the general set of variables needed by all or most functions
        ServiceAddress : a string identifying the service endpoint (URL, host, port, etc) of the calling Adaptable Log Channel object
        UserName : a string containing the username portion of the credential assigned to the Adaptable Log Channel object
        UserPass : a string containing the password portion of the credential assigned to the Adaptable Log Channel object
.PARAMETER Event
    A hashtable containing the standard set of values captured when events are logged by Trust Protection Platform 
        ID : integer that uniquely identifies the event
        Name : string that briefly describes the meaning of the event
        Description : string that translates raw event data into meaningful human readable text
        Severity : 1 (Emergency), 2 (Alert), 3 (Critical), 4 (Error), 5 (Warning), 6 (Notice), 7 (Info), 8 (Debug)
        ServerDate : the date/time that the event was recorded by the log server
        ClientDate : the date/time that the event occurred
        SourceIP : the IP address of the TPP server where the event occurred
        Component : distiguished name of the TPP object responsible for logging the event
        Grouping : a numeric identifier that can be used to group related events
        Text1 : string variable whose meaning varies by event definition (255 character maximum)
        Text2 : string variable whose meaning varies by event definition (255 character maximum)
        Value1 : integer variable whose meaning varies by event definition (32-bit)
        Value2 : integer variable whose meaning varies by event definition (32-bit)
        Data : byte array variable generally used to capture larger amounts of data (1536 byte maximum)
.PARAMETER Fields
    A hashtable containing all of the extra field values specified on the Adaptable Log Channel object.
        Text1 : a string value for the first text extra field defined by the header at the top of this script
        Text2 : a string value for the second text extra field defined by the header at the top of this script
        ...
        Text12 : a string value for the twelfth text extra field defined by the header at the top of this script
.NOTES
    Returns...
        Result : 'Success' to indicate the non-error completion state
        Updates : (optional) hashtable of attribute name-value pairs pertaining to the Event.Component that
            specify what the driver will update when the Result is 'Success'
######################################################################################################################>
function Perform-Action
{
    Param(
        [Parameter(Mandatory=$true,HelpMessage="General Parameters")]
        [System.Collections.Hashtable]$General,
        [Parameter(Mandatory=$true,HelpMessage="Event Detail Parameters")]
        [System.Collections.Hashtable]$Event,
        [Parameter(Mandatory=$true,HelpMessage="Extra Field Parameters")]
        [System.Collections.Hashtable]$Fields
    )
    DH-Log-ToDebugFile "==========Starting New Run=========="
    DH-Log-ToDebugFile "Adaptable log triggered by event: $($Event.Name)"
    DH-Log-ToDebugFile "Event occurred on engine: $($Event.SourceIP)"

    DH-CheckTokenExists

    DH-Show-GeneralData
    DH-Show-FieldsData
    DH-Show-EventData
    DH-Show-AllVariablelData

    DH-Import-VNPS
    DH-GetVenafiPS-Version
    DH-CreateVenafiPsSession

    <# ADD CUSTOM LOGIC HERE
    
    Find-VdcCertificate -Path '\VED\Policy\Certificates\ACME Certs\' -Recursive | Get-VdcAttribute -Attribute 'Status'
    
    #>

    DH-Log-ToDebugFile "==========Script Completed=========="

    return @{ Result="Success" }
}


<###################### THE FUNCTIONS AND CODE BELOW THIS LINE ARE NOT CALLED DIRECTLY BY VENAFI ######################>

function DH-Log-ToDebugFile{
    Param(
        [Parameter(Mandatory=$true,HelpMessage="Value to log")]
        [object]$data
    )
    try {
        (Get-Date).ToString() + " - " + $data >> $DEBUG_FILE
    }
    catch {
        (Get-Date).ToString() + "Error occurred during Log-ToDebugFile" + $_ >> $DEBUG_FILE
        Throw $_
    }
}

function DH-Show-GeneralData{
    if ($Fields['Text12'] -eq "True"){
        DH-Log-ToDebugFile "Log General Data enabled."
        $General_log = $General | ConvertTo-Json
        DH-Log-ToDebugFile $General_log
    } else {
        DH-Log-ToDebugFile "Log General Data disabled."
    }
}

function DH-Show-FieldsData{
    if ($Fields['Text11'] -eq "True"){
        DH-Log-ToDebugFile "Log Fields Data enabled."
        $Event_log = $Fields | ConvertTo-Json
        DH-Log-ToDebugFile $Event_log
    } else {
        DH-Log-ToDebugFile "Log Fields Data disabled."
    }
}

function DH-Show-EventData{
    if ($Fields['Text10'] -eq "True"){
        DH-Log-ToDebugFile "Log Event Data enabled."
        $Event_log = $Event | ConvertTo-Json
        DH-Log-ToDebugFile $Event_log
    } else {
        DH-Log-ToDebugFile "Log Event Data disabled."
    }
}

function DH-Import-VNPS{
     try {
        if ($Fields['Text7'] -eq "True") {
            DH-Log-ToDebugFile "Import VenafiPS enabled."
            Import-Module VenafiPS
            DH-Log-ToDebugFile "Successfully imported VenafiPS Module."
        } else {
            DH-Log-ToDebugFile "Import VenafiPS disabled."
        }
     } catch {
        DH-Log-ToDebugFile "Error occurred during DH-Import-VNPS function."
        DH-Log-ToDebugFile $_
        Throw $_
     }
}

function DH-GetVenafiPS-Version{
    try{
        if ($Fields['Text7'] -eq "True") {
            DH-Log-ToDebugFile "Import VenafiPS enabled; will check version info."
            $data = Get-Module -Name VenafiPs
            $version = $data.Version
            DH-Log-ToDebugFile "VenafiPS Version: $version"
        }
    } catch{
        DH-Log-ToDebugFile "Error occurred during DH-GetVenafiPS-Version function."
        DH-Log-ToDebugFile $_
        Throw $_
    }
}

function DH-CreateVenafiPsSession {
    try{
        if ($Fields['Text8'] -eq "True") {
            DH-Log-ToDebugFile "Create VenafiPS Session enabled, will try to create session."
            New-TrustClient -Server $general.ServiceAddress -AccessToken $general.OAuthAccessToken
        } else {
            DH-Log-ToDebugFile "Create VenafiPS Session disabled, will NOT try to create session."
        }

    } catch {
        DH-Log-ToDebugFile "Error occurred during DH-CreateVenafiPsSession function."
        DH-Log-ToDebugFile $_
        Throw $_
    }
}

function DH-Show-AllVariablelData{
    if ($Fields['Text9'] -eq "True"){
        DH-Log-ToDebugFile "Log All Variable Data enabled."
        $data = get-variable | ConvertTo-Json
        DH-Log-ToDebugFile $data
    } else {
        DH-Log-ToDebugFile "Log All Variable Data disabled."
    }
}

 
function DH-CheckTokenExists {
    if($general.OAuthAccessToken) {
        DH-Log-ToDebugFile "API auth token aquired successfully."
    } else {
        DH-Log-ToDebugFile "API auth token not available."
    }
}


Actions
  • Print
  • Copy Link

    https://knowledgebase.paloaltonetworks.com/KCSArticleDetail?id=kA1hj0000001l1hCAA&lang=en_US&refURL=http%3A%2F%2Fknowledgebase.paloaltonetworks.com%2FKCSArticleDetail