Delphix Web API – PowerShell Run Masking Job

| | | |

Take note of the Global Variables $BaseURL and $LogFilePath, make sure you set them to your values.

The idea for this script is to run this as part of a database restore/migration. Drop all access to the database to do the restore/migration, then run the script for Delphix, and then restore all access.

<#
.SYNOPSIS
Delphix - Run Masking Job

.DESCRIPTION
This script will kick off a masking job within Delphix

.EXAMPLE
.\Delphix-RunJob.ps1 -DelphixApplication Core -DelphixJob 31 -DelphixUser dkittell

#>

Param(
    [Parameter(
        Position=0,
        Mandatory = $True,
        HelpMessage="What Delphix Application?",
        valueFromPipeline=$true)]
    [String] $DelphixApplication,

    [Parameter(
        Position=1,
        Mandatory = $True,
        HelpMessage="What Delphix Job?",
        valueFromPipeline=$true)]
    [String] $DelphixJob,
    
    [Parameter(
        Position=2,
        Mandatory = $True,
        HelpMessage="What Delphix User Name?",
        valueFromPipeline=$true)]    
    [String] $DelphixUser,
    
    [Parameter(
        Position=3,
        Mandatory = $True,
        HelpMessage="What Delphix User Password?",
        valueFromPipeline=$true)]    
    [Security.SecureString] $DelphixPassword
 
)

# Global Variables - Start
$BaseURL = "http://192.168.1.2:123/dmsuite/apiV4/"
$LogFilePath = "c:\"
# Global Variables - Done

#Get Token - Start
$tokenReq = try {
    # Token Variables - Start
    Write-Output $DelphixUser
    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($DelphixPassword)
    $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    $url = $BaseURL + "login?user=$($DelphixUser)&password=$($UnsecurePassword)"
    #Write-Output $url
    #exit
    # Token Variables - Done
    Invoke-WebRequest -Uri $url -Method GET
}
catch {
    Write-Output "ERROR"
    write-Output "ERROR" | Out-File "${$LogFilePath}DelphixStatus_${DelphixApplication}_${DelphixJob}.txt"
    exit
}

$token = $tokenReq.Headers.auth_token
Write-Output $token
#Get Token - Done

# Run Masking Job - Start
$RunReq = try {
    # RunReq Variables - Start
    $url = $BaseURL + "applications/$DelphixApplication/maskingjobs/$DelphixJob/run"
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers = @{}
    $headers.Add("Content-Type","application/xml" )
    $headers.Add("auth_token",$token )
    # RunReq Variables - Done

    Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body "<MaskingsRequest/>"
}
catch {
   write-Output "ERROR" | Out-File "${$LogFilePath}DelphixStatus_${DelphixApplication}_${DelphixJob}.txt"    
}

Write-Output $RunReq
# Run Masking Job - Done

# Get Masking Job Status - Start
function Get-JobStatus{
    $RunReq = try {
        # RunReq Variables - Start
        $url = $BaseURL + "applications/$DelphixApplication/maskingjobs/$DelphixJob/results"
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers = @{}
        $headers.Add("Content-Type","application/xml" )
        $headers.Add("auth_token",$token )
    
 
    # RunReq Variables - Done
    Invoke-WebRequest -Uri $url -Method GET -Headers $headers 
}
catch {
   write-Output "ERROR" | Out-File "${$LogFilePath}DelphixStatus_${DelphixApplication}_${DelphixJob}.txt"
}
 
$xml = [xml]($(write-output $RunReq.Content))
return $xml.MaskingsResponse.Maskings.Masking.Status
}
# Get Masking Job Status - Done

$a = 1
DO
    {
        $status = Get-JobStatus

        switch ($status)
            {
                "RUNNING" {
                    #Write-Output $status
                    write-Output "Running..."
                }
                "SUCCESS" {
                    Write-Output $status
                    write-Output "Success" | Out-File "${$LogFilePath}DelphixStatus_${DelphixApplication}_${DelphixJob}.txt"
                    $a = 2
                }
                "ERROR" {
                    Write-Output $status
                    write-Output "Error" | Out-File "${$LogFilePath}DelphixStatus_${DelphixApplication}_${DelphixJob}.txt"
                    $a = 2
                }
                default {
                    Write-Output $status
                    Write-Output "Not running"
                    write-Output $status | Out-File "${$LogFilePath}DelphixStatus_${DelphixApplication}_${DelphixJob}.txt"
                    $a = 2
                }
            }
    }
WHILE ($a -ne 2)

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.