Last active
October 12, 2025 04:41
-
-
Save daniloalsilva/8171691e7c5b6df5dc6545f4b0ba941f to your computer and use it in GitHub Desktop.
Script used to create and retrieve a plesk license contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .Synopsis | |
| Plesk Licensing script | |
| .DESCRIPTION | |
| Plesk Licensing script | |
| This script intend to create a new license key in Plesk via API and | |
| retrieve the XML content of created key in a plesk node. | |
| Author: Danilo Silva | |
| #> | |
| $ErrorActionPreference = 'Stop' | |
| # Plesk API endpoint | |
| $url = 'https://api.central.plesk.com/30' | |
| # Plesk auth data | |
| $user = 'username' | |
| $pass = 'password' | |
| $note = "$(Get-Date) - Key generated for $env:COMPUTERNAME - $((gwmi win32_operatingsystem).Caption)" | |
| $keyPath = "C:\Program Files\Plesk\$([datetime]::Now.ToString("yyMMdd-HHmmss")).pleskkey.xml" | |
| # create directory path if it doesn't exists | |
| if (! (Test-Path ([System.IO.Path]::GetDirectoryName($keyPath)))){ | |
| mkdir ([System.IO.Path]::GetDirectoryName($keyPath)) | |
| } | |
| # create credentials | |
| $spass = $pass | ConvertTo-SecureString -AsPlainText -Force | |
| $cred = New-Object System.Management.Automation.PSCredential ($user, $spass) | |
| # Plesk template for API body | |
| $content = '{ | |
| "items" : [ { | |
| "externalId" : "", | |
| "item" : "PLESK-12-WEB-HOST-1M" | |
| }, | |
| { | |
| "externalId" : "", | |
| "item" : "FT-PLESK-5-LANGUAGE-PACKS-1M" | |
| } ] | |
| }' | |
| # creating key | |
| $var = irm "$url/keys?return-key-state=yes" -cred $cred -Body $content -Method POST -ContentType 'application/json;charset=UTF-8' | |
| # add key notes | |
| irm "$url/keys/$($var.keyIdentifiers.keyId)/notes" -cred $cred -Body "{ `"value`" : `"$note`" }" -Method POST -ContentType 'application/json;charset=UTF-8' | |
| # retrieving and saving xml key | |
| $xmlKey = irm "$url/keys/$($var.keyIdentifiers.keyId)/retrieve" -cred $cred | |
| $xmlKey.Save($keyPath) | |
| # installing the license | |
| .$env:plesk_bin\keymng.exe --install --source-file=$keyPath | |
| .$env:plesk_cli\license.exe -i $keyPath | |
| # if testing something, delete the key in the end to avoid chargings | |
| # irm "$url/keys/$($var.keyIdentifiers.keyId)?return-key-state=yes" -cred $cred -Method DELETE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment