A simple PS-script to configure syslog on NSX-V

Introduction

In this blog I describe the necessary step for configuring syslog on NSX for vSphere with the use of PowerShell.

Configuring syslog on the NSX Manager can be done by the GUI, but for the controllers you need to create a REST API call towards the NSX-Manager (which can be a daunting task).

So, when I already have to create REST API calls .. why not create a script that it can do automatically for the NSX-Manager and its connected Controllers ? yeah .. why not ..

The Powershell script below uses the native “invoke-webrequest” cmdlets to configure syslog on the NSX Managers and Controllers.


#variables
$NSXmanager = "NSXM01.domain.local"
$NSXadmin = "admin" 
$NSXadminpwd = "Password01!"
$syslogserver = "syslogsrv.domain.local"

#create header
$pair = "$($NSXadmin):$($NSXadminpwd)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
    Authorization = $basicAuthValue
}

$RequestBodyMGR = @"
<syslogserver>
 <syslogServer>$syslogserver</syslogServer>
 <port>514</port>
 <protocol>UDP</protocol>
</syslogserver>
"@

$RequestBodyCTRL = @"
<controllerSyslogServer>
 <syslogServer>$syslogserver</syslogServer>
 <port>514</port>
 <protocol>UDP</protocol>
 <level>INFO</level>
</controllerSyslogServer>
"@

write-host "configuring syslog on NSX-Manager"
#write syslog config to NSX Manager
$URI = "https://"+$NSXmanager+"/api/1.0/appliance-management/system/syslogserver"
$result = Invoke-WebRequest -Uri $URI -Method PUT -Body $RequestBodyMGR -Headers $Headers -ContentType "application/XML" 
$result = Invoke-WebRequest -Uri $URI -Method Get -Headers $Headers
write-host $result.Content

write-host "retrieving controllers from NSX-Manager"
#retrieving NSX Controllers from NSX Manager
$URI = "https://"+$NSXmanager+"/api/2.0/vdn/controller"
$result = Invoke-WebRequest -Uri $URI -Method Get -Headers $Headers -ContentType "application/xml"
$controllers = ([xml]$result.Content).Controllers.controller.id

write-host "configuring syslog on NSX-Controllers"
#write syslog config to NSX Controller
foreach ($ctrlnr in $controllers) {
$URI = "https://"+$NSXmanager+"/api/2.0/vdn/controller/"+$ctrlnr+"/syslog"
$result = Invoke-WebRequest -Uri $URI -Method DELETE -Headers $Headers 
$result = Invoke-WebRequest -Uri $URI -Method POST -Body $RequestBodyCTRL -Headers $Headers -ContentType "application/XML" 
$result = Invoke-WebRequest -Uri $URI -Method Get -Headers $Headers 
write-host $result.Content
} 
write-host "Finished configuring syslog on NSX"

Using this script is free to use, but using it is on your own risk!

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top