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:
Previous Articles on the Kemp API and Powershell (did not work for me)
Kemp’s API and Powershell documentation