There are several articles explaining how to access and manage the Kemp LoadMaster with the RESTful API. However, there are not very many articles that show you how to connect via Powershell. I attempted to follow several articles, but I kept running into problems. Primarily the following:
“Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.”
Everything that I did could not solve this problem. I found several articles stating that I would need to tell powershell to ignore certificate problems or force powershell to use TLS 1.2 before calling the invoke-restmethod cmdlet.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
However, this did not work for me at all! The problem is related to the invoke-restmethod and invoke-webrequest cmdlets run in their own runspace.
Follow this procedure!
- Download and install the latest version of Powershell Core
- Run your invoke-restmethod and invoke-webrequest cmdlets under the newly installed Powershell Core
Powershell code to connect to the Kemp Loadmaster and list the virtual services
$pass = Get-Content "c:\scripts\KempPassword.txt" | ConvertTo-SecureString $User = "YourKempUserAccount" $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $pass> $kempurl = "https://kempLB.constoso.com" $uri = $kempurl+"/access/listvs" [string]$response = Invoke-RestMethod $uri -Credential $MyCredential
In the example above, I am using a encrypted password that was saved in a KempPassword.txt file. You can generate this password by executing:
"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
The Kemp LoadMaster will return an xml response that will need to be parsed as required. Here are two websites that I used to parse the xml in powershell:
https://www.petri.com/search-xml-files-powershell-using-select-xml
and
http://www.itprotoday.com/management-mobility/getting-started-rest-and-powershell
Previous Articles on the Kemp API and Powershell (did not work for me)
- http://www.powershellmagazine.com/2014/01/16/managing-kemp-loadmaster-using-rest-api-and-powershell/
- http://patchcharron.com/blog/2013/4/2/setting-up-your-kemp-load-balancer-for-powershell.html
Kemp’s API and Powershell documentation
https://support.kemptechnologies.com/hc/en-us/articles/203863435-RESTful-API
https://support.kemptechnologies.com/hc/en-us/articles/203756799-RESTful-API-Programmer-Guide
https://support.kemptechnologies.com/hc/en-us/articles/203863385-PowerShell