Disabling/Enabling real server through PS

1

Hi all

I'm trying to come up with a PS script that will allow for enabling/disabling real servers in the specific services on Kemp LB

however i've run into few issues and would greatly appreciate some feedback.

Here's what i run:

$uri = "https://10.252.20.5/access/modrs?vs=10.252.20.12&port=80&prot=tcp&rs=10.252.20.188&rsport=80&enable=y"
$secpasswd = ConvertTo-SecureString “xxxxxxx” -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("bal", $secpasswd)

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

$response = $request.GetResponse()

$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()
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
}
}

 

We are going to use it as a step in Octopus Deploy when releasing websites to take the server out of LB before deploying the code to it.

The problem is two fold, when running is locally through PS command prompt the script disables/enables the node however it does not spew any result output. When run through Octopus Deploy we running into 401 issue:

**********************
Windows PowerShell transcript start
Start time: 20160509104028
Username: WINTECH\sa_OctopusDeploy
RunAs User: WINTECH\sa_OctopusDeploy
Machine: UK-DEPLOY02 (Microsoft Windows NT 6.3.9600.0)
Host Application: C:\Windows\system32\WindowsPowershell\v1.0\PowerShell.exe -NoLogo -ExecutionPolicy Unrestricted -Command . {. 'C:\Octopus\Work\20160509094024-4\Bootstrap.Script.ps1'; if ((test-path variable:global:lastexitcode)) { exit $LastExitCode }}
Process ID: 11972
**********************
Transcript started, output file is c:\kemp\log01.txt

PS>TerminatingError(): "Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized.""
>> TerminatingError(): "Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized.""
>> TerminatingError(): "Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized.""
>> TerminatingError(): "Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized.""
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."
At C:\Octopus\Work\20160509094024-4\Script.ps1:12 char:1
+ $response = $request.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : WebException


PS>$global:?
False
**********************
Windows PowerShell transcript end
End time: 20160509104029
**********************

 

The sa_OctopusDeploy account is a local administrator on the server that the script runs on and when remoted in to the server executes the scripts fine (no response output but modifies the node). When script is lunched through the application is fails at 401.

I'm in a bit of a pickle as to what might be causing this.

 

0 comments