Last active
June 12, 2016 10:35
-
-
Save MichaelRyom/74ca354f60d9dda311ec4777f74816b1 to your computer and use it in GitHub Desktop.
This script six inputs which are used to setup NSX Controller syslog option, which has to be done via API calls
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
Param( | |
[string]$username, | |
[string]$NSXFQDN, | |
[string]$SyslogServer, | |
[string]$SyslogPort, | |
[string]$SyslogProtocol, | |
[string]$SyslogLevel | |
) | |
$Secpw = Read-Host -AsSecureString -Prompt "Password" | |
#^End of variables | |
#SSL certificate trust | |
add-type @" | |
using System.Net; | |
using System.Security.Cryptography.X509Certificates; | |
public class TrustAllCertsPolicy : ICertificatePolicy { | |
public bool CheckValidationResult( | |
ServicePoint srvPoint, X509Certificate certificate, | |
WebRequest request, int certificateProblem) { | |
return true; | |
} | |
} | |
"@ | |
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
#^End of SSL certificate trust | |
#Convert username and password to basic auth | |
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username + ":" + ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secpw))))) | |
#^End of Convert username and password to basix auth | |
#Get a list of NSX Controller(s) id(s) | |
$Controllers = (Invoke-RestMethod -Method Get -Uri "https://$NSXFQDN/api/2.0/vdn/controller" -Headers @{'Content-Type'='application/xml';'Authorization' = "Basic $auth"}).controllers.controller.id | |
#^End of Get a list of NSX Controller(s) id(s) | |
#API Call Payload | |
$body = @" | |
<controllerSyslogServer> | |
<syslogServer>$SyslogServer</syslogServer> | |
<port>$SyslogPort</port> | |
<protocol>$SyslogProtocol</protocol> | |
<level>$SyslogLevel</level> | |
</controllerSyslogServer> | |
"@ | |
#^End of API Call Payload | |
#Run through all controller(s) and test if already configured or else configure it. | |
Foreach($Controller in $Controllers){ | |
$Resp = "" | |
#Test if a configuration exists | |
try { $NSXResp = Invoke-RestMethod -Method Get -Uri "https://$NSXFQDN/api/2.0/vdn/controller/$Controller/syslog" -Headers @{'Content-Type'='application/xml';'Authorization' = "Basic $auth"}} catch { $Resp = $_.Exception.Response } | |
#If configuration is NullOrEmpty, setup the NSX Controller with the correct configuration and output the new configuaration | |
#or else just output the configuration | |
if(!([string]::IsNullOrEmpty($Resp))){ | |
$NSXResp = Invoke-RestMethod -Method Post -Uri "https://$NSXFQDN/api/2.0/vdn/controller/$Controller/syslog" -Headers @{'Content-Type'='application/xml';'Authorization' = "Basic $auth"} -Body $body | |
if(($NSXResp = Invoke-RestMethod -Method Get -Uri "https://$NSXFQDN/api/2.0/vdn/controller/$Controller/syslog" -Headers @{'Content-Type'='application/xml';'Authorization' = "Basic $auth"})){ | |
Write-Host "Syslog has been configured on"$Controller | |
Write-Host "Syslog is set to"$NSXResp.controllerSyslogServer.syslogServer"on port"$NSXResp.controllerSyslogServer.Port"using"$NSXResp.controllerSyslogServer.Protocol"and logging level set to"$NSXResp.controllerSyslogServer.level"for"$Controller"" | |
}else{ | |
Write-Host "ERROR! Controller not configured"$Controller | |
} | |
}else{ | |
Write-Host "Syslog is set to"$NSXResp.controllerSyslogServer.syslogServer"on port"$NSXResp.controllerSyslogServer.Port"using"$NSXResp.controllerSyslogServer.Protocol"and logging level set to"$NSXResp.controllerSyslogServer.level"for"$Controller"" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment