getting “Exception calling ”GetResponse" while getting cache size in kemp using powershell script

I am trying to get cache size from Kemp server using power shell script. Following is thew script I am using to do so. But keep getting "Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a send"

$uri = "https://10.0.0.1/access/get?param=cachesize"

$username = "bal" $password = "XXXXXXX" strong text

$secPw = ConvertTo-SecureString $password -AsPlainText -Force

$Credential = New-Object PSCredential -ArgumentList $username,$secPw

[System.Net.ServicePointManager]::Expect100Continue = $true [System.Net.ServicePointManager]::MaxServicePointIdleTime = 10000 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $request = [System.Net.HttpWebRequest]::Create($uri) $request.Credentials = $Credential

$response = $request.GetResponse() $response $stream = $response.GetResponseStream() if($response.contenttype -eq "text/xml") { $Encoding = [System.Text.Encoding]::GetEncoding("utf-8") $reader = New-Object system.io.StreamReader($stream, $Encoding) $result = $reader.ReadToEnd() $result if ($result.response.success) { if ([string]::IsNullOrEmpty($result.response.success.data)) { Write-Output $result.response.code } else { Write-output $result.response.success.data } } else { Write-Output $result.response } }

please guide what is causing this error. Not good in powershell.

  • What you have submitted does is not really complete enough to see what you are really doing.

    What version of PowerShell are you using?
    Are you using the Kemp PowerShell module?

    'support.kemptechnologies.com/hc/en-us/articles/203863385-PowerShell'

    Be aware that it has its own impacts.
    'support.kemptechnologies.com/hc/en-us/community/posts/115009479906-KEMP-PowerShell-Module-Breaks-Native-Windows-RemoteAccess-Commands'

    Note, that when calling after web information, you'd use either the Invoke-WebRequest and / or Invoke-RestMethod if you are on v3 or higher. If you are below that, then you'd have to leverage the .Net framework directly. Specifically [System.Net.WebRequest] and / or [System.Net.HttpWebRequest]. The Kemp PoSH modules leverage RestApi to do it's work. In according to it's own documentation.
  • In reply to postanote:

    Thank you for the feedback. I am running Windows Powershell version 4.0.

    I have imported kemp module and now I have managed to do it by gettign credentials using the below command:-
    $mycreds = New-Object System.Management.Automation.PSCredential ("xxxxxx", $secpasswd) as somehow it was not supporting New-Object PSCredential -ArgumentList $username,$secPw.

    Thanks for your help.
  • In reply to gigabyte:

    No worries.
    Glad you got to what is a working state for you.

    However, you are using the long way to handle the user credentials. Virtually in all cases it is simpler to use the:
    Get-Credential cmdlet

    # Store the defined users credential in the variable
    $mycreds = Get-Credential -Credential 'domain\username'

    # Show that is in the variable
    $mycreds

    UserName Password
    -------- --------
    domain\username System.Security.SecureString

    Then use $mycreds whenever credentials are needed.

    Or just look at the parts

    $mycreds.UserName
    domain\username

    $mycreds.Password
    System.Security.SecureString