License activation with Powershell scripts

License activation with Powershell scripts

The LicenseManagerAutomation.exe can be used to activate licenses via a powershell script. This works online and offline. 
The online activation is described in the help of the license manager automation and very easy to implement.
For the offline activation two scripts are used.
  1. The first script creates the license request file
  2. The second script applies the license update file
In between these steps, the license request file has to be uploaded to the license web depot. Also, the license update file is gathered (downloaded) from the license web depot. These are manual steps. (Feel free to write your own script if you like)
For storing the data in between the first and second step the *.licsn file is needed. These files are sent to you if you are ordering 3 or more licenses at once. If you didn't get it: this is just a text file with the ending .licsn. the text file content looks like this:
C00VS-YMNUN-B95T2-00000-33559;zenon OP DEV 64 Tags
C00DU-7ITIL-CF3LY-00000-33578;zenon OP DEV 64 Tags
C00GT-5CRVX-PU6Q9-00005-33571;zenon OP DEV 64 Tags
C00LZ-L5KXW-TIKY5-00003-33572;zenon OP DEV 64 Tags
**

The scripts cannot be attached to this article. Please copy and paste them to an editor and save the file as .ps1 (powershell script).
Store the scripts and the *.licsn file at the same location.

The first script:
# goto script location
cd $PSScriptRoot

#get first file with *.licsn fileending
$LicenseSerialNumberFile = @(gci '*.licsn')[0]
if ($LicenseSerialNumberFile -le $null)
{
    Write-Output "[ERROR] License serial number file not found. Please put license serial number file in the same file location than this script."
    Write-Output "[INFO] Script end."
    pause
    exit
}
else
{
    Write-Output "[SUCCESS] License serial number file $LicenseSerialNumberFile found!"
}

Write-Output "[INFO] License serial number file: '$LicenseSerialNumberFile'"

#parse file for first line without license request file created (hostname empty)
$SerialNumbers = Import-Csv $LicenseSerialNumberFile -header SerialNumber,LicenseName,Host,UserName,ActivationStatus -Delimiter ';'
$SerialNumberForRequest = ''
$LicenseCounter = 0
foreach($SerialNumberLine in $SerialNumbers)
{
    $sn = $SerialNumberLine.SerialNumber
    Write-Output "[INFO] Serial number for consideration: '$sn'..."
    if($SerialNumberLine.Host -le '')
    {
        $SerialNumberForRequest = $SerialNumberLine.SerialNumber
        Write-Output "[SUCCESS] Serial number for request found! $SerialNumberForRequest"
        break
    }
    else
    {
        $h = $SerialNumberLine.Host
        Write-Output "[INFO] ...not suitable. Request or activation already performed for host $h"
    }
    $LicenseCounter = $LicenseCounter + 1
    if($LicenseCounter -eq $SerialNumbers.Count)
    {    
        Write-Output "[ERROR] All licenses in '$LicenseSerialNumberFile' have been activated or requests for activation are created. No available license found in file. Please check the activation status of the licenses if they match the activation status in the file and update file. If activation status matches, contact vendor to buy more licenses!"
        Write-Output "[INFO] Script end."
        pause
        exit
    }
}

#license manager automation create license request
$LicenseManagerAutomationProgramName = 'C:\Program Files (x86)\Common Files\COPA-DATA\STARTUP\LicenseManagerAutomation.exe'
$PathNameForRequestfile = "$PSScriptRoot\$SerialNumberForRequest.LicReq"

$LicenseRequestCreationArguments = @(
    "--action","CreateRequestFile",
    "--SerialNumber", $SerialNumberForRequest,
    "--DongleType","Soft"
    "--FileNameRequest",$PathNameForRequestfile
    )
Write-Output "[INFO] Start: $LicenseManagerAutomationProgramName $LicenseRequestCreationArguments"
& $LicenseManagerAutomationProgramName $LicenseRequestCreationArguments
Write-Output "[INFO] Finish: $LicenseManagerAutomationProgramName $LicenseRequestCreationArguments"

#check if request file was created
cd $PSScriptRoot
$LicenseRequestFile = @(gci "$SerialNumberForRequest.LicReq")[0]
if ($LicenseRequestFile -le $null)
{
    Write-Output "[ERROR] License request file could not be created. Please check script or create license request file with the License Administration Tool/License Manager with GUI."
    Write-Output "[INFO] Script end."
    pause
    exit
}
else
{
    Write-Output "[SUCCESS] License request file $SerialNumberForRequest.LicReq created."
    #update lic sn file
    Write-Output "[INFO] Start update LicSn file."
    $TempFile = "$PSScriptRoot\temp.licsn"
    Import-Csv $LicenseSerialNumberFile -header SerialNumber,LicenseName,Host,UserName,ActivationStatus -Delimiter ';' | ForEach-Object {
     if($_.SerialNumber -eq $SerialNumberForRequest)
     {
        $_.Host = "$((Get-WMIObject Win32_ComputerSystem).Name).$((Get-WMIObject Win32_ComputerSystem).Domain)"
     }
     $_
    } | ConvertTo-Csv -NoTypeInformation -Delimiter ';' | %{$_.Replace('"','')} | Out-File $TempFile 
    get-content $TempFile |
        select -Skip 1 |
        set-content "$TempFile-temp"
        move "$TempFile-temp" $TempFile -Force
    Remove-Item -Path $LicenseSerialNumberFile
    Rename-Item -Path $TempFile -NewName $LicenseSerialNumberFile

    Write-Output "[INFO] Stopp update LicSn file."
}
Write-Output "[INFO] Script end."
exit


The second script:

# goto script location
cd $PSScriptRoot

#get first file with *.licsn fileending
$LicenseSerialNumberFile = @(gci '*.licsn')[0]
if ($LicenseSerialNumberFile -le $null)
{
    Write-Output "[SUCCESS] License serial number file not found. Please put license serial number file in the same file location than this script."
    Write-Output "[INFO] Script end."
    pause
    exit
}

Write-Output "[INFO] License serial number file: '$LicenseSerialNumberFile'"

#parse file for first line where hostname is equal to current machines hostname and activation is empty 
$SerialNumbers = Import-Csv $LicenseSerialNumberFile -header SerialNumber,LicenseName,Host,UserName,ActivationStatus -Delimiter ';'
$SerialNumberForActivation = ''
$LicenseCounter = 0
$HostAndDomain = "$((Get-WMIObject Win32_ComputerSystem).Name).$((Get-WMIObject Win32_ComputerSystem).Domain)"
foreach($SerialNumberLine in $SerialNumbers)
{
    $sn = $SerialNumberLine.SerialNumber
    Write-Output "[INFO] Serial number for consideration: '$sn'..."
    if( ($SerialNumberLine.Host -eq $HostAndDomain) -and ($SerialNumberLine.ActivationStatus -le ''))
    {
        $SerialNumberForActivation = $SerialNumberLine.SerialNumber
        Write-Output "[SUCCESS] Serial number for activation found! $SerialNumberForActivation"
        break
    }
    else
    {
        $h = $SerialNumberLine.Host
        Write-Output "[INFO] ...not suitable. Activation already performed for host $h"
    }
    $LicenseCounter = $LicenseCounter + 1
    if($LicenseCounter -eq $SerialNumbers.Count)
    {    
        Write-Output "[ERROR] All licenses in '$LicenseSerialNumberFile' have been activated. No available license found in file. Please check the activation status of the licenses if they match the activation status in the file and update file. If activation status matches, contact vendor to buy more licenses!"
        Write-Output "[INFO] Script end."
        pause
        exit
    }
}

#get first file with serial number for activation and .LicUpd fileending
$PathNameForActivationfile = "$PSScriptRoot\$SerialNumberForActivation" + '.LicUpd'
if (-not (Test-Path $PathNameForActivationfile))
{
    Write-Output "[ERROR] License activation/update file $PathNameForActivationfile for serial number $SerialNumberForActivation not found. Please upload license request, download license update file and put license update file in the same file location than this script."
    Write-Output "[INFO] Script end."
    pause
    exit
}
else
{
    Write-Output "[SUCCESS] License activation/update file for serial number $SerialNumberForActivation found!"
}

#license manager automation license activation
$LicenseManagerAutomationProgramName = 'C:\Program Files (x86)\Common Files\COPA-DATA\STARTUP\LicenseManagerAutomation.exe'
$PathNameForConfirmationfile = "$PSScriptRoot\$SerialNumberForActivation.LicConf"

$LicenseRequestCreationArguments = @(
    "--action","ActivateOffline",
    "--FileNameUpdate",$PathNameForActivationfile,
    "--FileNameConfirmation",$PathNameForConfirmationfile,
    "--WriteInIni"
    )
Write-Output "[INFO] Start: $LicenseManagerAutomationProgramName $LicenseRequestCreationArguments"
$output = cmd /c $LicenseManagerAutomationProgramName $LicenseRequestCreationArguments
Write-Output $output
Write-Output "[INFO] Finish: $LicenseManagerAutomationProgramName $LicenseRequestCreationArguments"
if($output.StartsWith("Error"))
{
    Write-Output "[ERROR] An error occured while activation license $SerialNumberForActivation. Please note exact error number and log the output of the script and contact support@copadata.com."
    Write-Output "[INFO] Script end."
    pause
    exit
}

#check if confirmation file was created
cd $PSScriptRoot
if (-not (Test-Path -Path $PathNameForConfirmationfile))
{
    Write-Output "[ERROR] License confirmation file $PathNameForConfirmationfile could not be found. Please check script or create license confirmation file with the License Administration Tool/License Manager with GUI. please also check, if license has been activated and update *.licSn file accordingly."
    Write-Output "[INFO] Script end."
    pause
    exit
}
else
{
    Write-Output "[SUCCESS] License $SerialNumberForActivation activated and confirmation file $SerialNumberForActivation.LicConf created."
    #update lic sn file
    Write-Output "[INFO] Start update LicSn file."
    $TempFile = "$PSScriptRoot\temp.licsn"
    Import-Csv $LicenseSerialNumberFile -header SerialNumber,LicenseName,Host,UserName,ActivationStatus -Delimiter ';' | ForEach-Object {
     if(($_.SerialNumber -eq $SerialNumberForActivation) -and ($_.Host -eq $HostAndDomain))
     {
        $_.UserName = $env:username
        $_.ActivationStatus = "True"
     }
     $_
    } | ConvertTo-Csv -NoTypeInformation -Delimiter ';' | %{$_.Replace('"','')} | Out-File $TempFile 
    get-content $TempFile |
        select -Skip 1 |
        set-content "$TempFile-temp"
        move "$TempFile-temp" $TempFile -Force
    Remove-Item -Path $LicenseSerialNumberFile
    Rename-Item -Path $TempFile -NewName $LicenseSerialNumberFile

    Write-Output "[INFO] Stopp update LicSn file."
}
Write-Output "[INFO] Script end."
exit



_______
** The serial numbers are for demonstration purposes only. They do not work.