Skip to content

Script Capture Agents with PowerShell

The Capture PowerShell cmdlets do from a script what you otherwise do in the SQDR Control Center: list Capture Agents, create, start, stop and delete them, and read or change their configuration. Use them, for example, to set up the same agent for many databases.

For the Replicator Service (sources, destinations, subscriptions, groups), use the SQDR module instead (see PowerShell). To publish tables with these cmdlets, see Publishing tables: PowerShell.

Set up

You need PowerShell 5.1 or later, on any computer that reaches the SQDR Control Center's port. The cmdlets work through the SQDR Manager API web application, which Stelo Capture installs in the SQDR Control Center's Jetty server.

  1. Download CapturePowerShell.zip, and unblock it (its Properties, Unblock, Apply) before you extract it.
  2. Extract it, and move its Tools folder to a folder of your choice, for example C:\sqdr.
  3. In PowerShell, load the module and list its cmdlets:

    Import-Module 'C:\sqdr\Tools\CapturePowerShell\CapturePowerShell.dll'
    Get-Command -Module CapturePowerShell
    
  4. Sign in with a user and password that can sign in to the SQDR Control Center (see Sign in to the SQDR Control Center), then try a command:

    $baseUrl = 'http://localhost:8080'
    Request-Login -BaseUrl $baseUrl -UserId '<user>' -Password '<password>'
    Show-Agents -BaseUrl $baseUrl
    

The examples folder has a sample script (test-api-<database>.ps1) and a JSON file (data\add-agent-<database>.json) for each source database type. Agent names are case-sensitive.

Create an agent

Create the first agent for a source type in the SQDR Control Center with the Add Capture Agent wizard (see Add a Capture Agent): you learn which settings that source needs, and check that the source is prepared. Then script the others:

  1. Copy the sample script and JSON file for your database type, and rename them.
  2. In the script, set the path of Import-Module, $baseUrl (for another server), the output and data folders, the agent name (by default database%schema@host), and the sign-in user and password; comment out the steps you don't want.
  3. In the JSON file, set the source in dbConfig, the staging database in controlDbConfig, and notification settings and other agent properties in properties, including name to give the agent another name. For example, for SQL Server:

    {
        "dbType": "Microsoft SQL Server",
        "dbConfig": {
            "database": "MYDB",
            "host": "mysqlhost",
            "port": "1433",
            "schema": "SQDR",
            "user": "<source user>",
            "password": "<source password>",
            "changeDataCapture": false
        },
        "controlDbConfig": {
            "user": "sqdr",
            "password": "<password>",
            "port": "50000"
        },
        "registerRemoteAgent": false,
        "properties": {
            "logLevel": "INFO",
            "notificationLevel": "WARNING"
        }
    }
    

    To put the agent in an existing staging database (for example SQDRP3), start controlDbConfig with "controlDb": "SQDRP3"; the user, password and port entries are then ignored but must be there.

  4. Run the script. Set-Agent creates the agent; the sample then shows, starts and stops it, as you chose.

The script and JSON file now hold passwords: see Keep the files safe.

Change an agent's configuration

Edit-AgentProperties sets the properties listed in a JSON file, as pairs of name and value, and restarts the agent if it's running. For example, to turn on the log reader stored procedure of a Db2 for LUW agent, change the notification level and rename the agent:

[
    ["udbReadLogUsingSP", "true"],
    ["notificationLevel", "INFO"],
    ["name", "MyAgent"]
]
Edit-AgentProperties -BaseUrl $baseUrl -Name 'MYDB@myhost' -JsonFile 'C:\sqdr\Tools\examples\data\edit-agent-properties.json'

Cmdlets

Every cmdlet takes -BaseUrl (default http://127.0.0.1:8080) and -TrustServerCertificate (see Another server, and HTTPS). The cmdlets that return information write JSON, to the screen or, with -JsonFileOut <file>, to a file.

Cmdlet What it does
Request-Login -UserId user -Password password Signs in. Run it first.
Show-Agents Lists the Launch Agent and every Capture Agent.
Show-Agent -Name agent Shows one agent.
Show-AgentProperties -Name agent Shows all of an agent's configuration properties.
Set-Agent -JsonFile file Creates an agent, as the Add Capture Agent wizard does (see Create an agent).
Edit-AgentProperties -Name agent -JsonFile file Changes or adds properties (see Change an agent's configuration).
Set-AgentProperties -Name agent -JsonFile file Replaces the agent's whole configuration. Use it only when Stelo Support asks you to.
Start-Agent -Name agent, Stop-Agent -Name agent Starts or stops the agent (see Start, stop and delete a Capture Agent).
Remove-Agent -Name agent Deletes the agent (see Delete).
Show-Publications, Add-Publication, Remove-Publication Publications; see Publishing tables: PowerShell.

In the output of Show-Agent and Show-Agents, State is 1 (running), 2 (stopped), 3 (disabled) or 4 (standby), and Type is the source database type: 0 for the Launch Agent, 1 Db2 for i, 3 Db2 for LUW, 5 SQL Server, 6 Oracle, 7 Informix, 8 MySQL, 9 PostgreSQL.

Another server, and HTTPS

To manage the agents of another Stelo VM, use its address in -BaseUrl, for example http://mysqdrplus:8080. The SQDR Control Center's port must be open in that server's firewall (see Ports). The Stelo Replicator container works the same way, at the host's address.

For HTTPS, use https:// and the HTTPS port, for example https://mysqdrplus:8443. The cmdlets check the server's certificate:

  • With your own certificate on the SQDR Control Center (see Your own certificate for the SQDR Control Center), the computer running the cmdlets must trust its certificate authority (for example in the Windows certificate store).
  • With the self-signed certificate, which changes each time the SQDR Plus Jetty service starts, add -TrustServerCertificate 1 to every cmdlet, which skips the check:

    Request-Login -BaseUrl 'https://localhost:8443' -UserId '<user>' -Password '<password>' -TrustServerCertificate 1
    Show-Agents -BaseUrl 'https://localhost:8443' -TrustServerCertificate 1
    

Keep the files safe

The scripts and JSON files hold passwords in plain text: of the source database, the SQDR Control Center and the staging database. Restrict who can read them, and delete them when you no longer need them.

Troubleshooting

Import-Module : Could not load file or assembly '...CapturePowerShell.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515). The extracted files are blocked because the zip file wasn't unblocked. Delete them, unblock the zip file, and extract it again.