VMware Cloud Community
virtualtech_wor
Enthusiast
Enthusiast

VCSA and PSC DNS Server settings

Hi,

Looking for a PowerCli way:

To run through a list of VCSA servers and generate report on the current DNS Servers configured on VCSA and PSC appliances.

Also, if possible (can be a second script) to update DNS Servers on VCSA and PSC.

Thanks in Advance.

Regards,

Uman

0 Kudos
7 Replies
AdrianTT
Enthusiast
Enthusiast

Hi Uman,

This can be achieved with the vCSA REST API. If you had a list of vCenter Servers you could do something like the following;

$Credential = Get-Credential # vSphere SSO Credentials

$colvCenterServer = ("labvc1.pigeonnuggets.com","labvc2.pigeonnuggets.com") # Your vCenter server collection

$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName+':'+$Credential.GetNetworkCredential().Password))

$head = @{

  'Authorization' = "Basic $auth"

}

foreach($vCenterServer in $colvCenterServer){

  # Setup the API Session

  $SessionURI = "https://" + $vCenterServer + "/rest/com/vmware/cis/session"

  $request = Invoke-WebRequest -Uri $SessionURI -Method Post -Headers $head

  $token = (ConvertFrom-Json $request.Content).value

  $session = @{'vmware-api-session-id' = $token}

  # Get a list of the DNS Configuration

  $DNSURI = "https://" + $vCenterServer + "/rest/appliance/networking/dns/servers"

  $dnsRequest = Invoke-WebRequest -Uri $DNSURI -Method Get -Headers $session

  $DNSServers = (ConvertFrom-Json $dnsRequest.Content).value

  # Do something with the $DNSServers value returned ?

  $DNSServers

}

# To Amend the values use a POST Method replacing the IP as required

$strPOSTValue = @'

{

  "config": {

    "mode": "is_static",

    "servers": [

      "192.168.88.10"

    ]

  }

}

'@

foreach($vCenterServer in $colvCenterServer){

  # Setup the API Session

  $SessionURI = "https://" + $vCenterServer + "/rest/com/vmware/cis/session"

  $request = Invoke-WebRequest -Uri $SessionURI -Method Post -Headers $head

  $token = (ConvertFrom-Json $request.Content).value

  $session = @{'vmware-api-session-id' = $token}

  $DNSURI = "https://" + $vCenterServer + "/rest/appliance/networking/dns/servers"

  Invoke-WebRequest -Uri $DNSURI -Method Put -Headers $session -Body $strPOSTValue -ContentType "application/json"

}

Hopefully this helps; I would recommend that you check out the API Explorer for vCenter (https://vCenter.tld/apiexplorer/) for more methods. Let me know if you need any further info and please mark this as helpful if it has been. Cheers,

Adrian

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Cheers,
0 Kudos
virtualtech_wor
Enthusiast
Enthusiast

Thank you Adrian.

However, we are using ldaps for authentication. Can you please advise how to modify the script.

Also, if I want to change the DNS Servers (2 IPs) do I just replace this part of the script with my new DNS servers? Please advise. Is the separator ; ok.

  "servers": [

      "NewDNSIP1";"NewDNSIP2"

    ]

Thanks,

Uman

0 Kudos
virtualtech_wor
Enthusiast
Enthusiast

Hi Adrian,

I did it this way and it worked. Please let me know if any suggestions on this.

1) For fetching current DNS configuration:

     Create a batch file (example: dnssettings.bat) with the below contents:

     cd PathtoPlink.exe

     FOR /F %%a IN (servers.txt) DO (echo y | plink -ssh -l root -pw Passwordxxxxx -m commands.txt %%a) >> output-psc.txt 2>&1

server.txt should contain the list of PSC/VCSA appliance names

Commands.txt Contents:

cd /opt/vmware/share/vami

./vami_dns

Output.txt: Will output the DNS Servers configured for each of the appliance listed in server.txt

Note: Its best to keep all these files (plink.exe, server.txt, commands.txt, dnssettings.bat) under same folder.

2) To update DNS Servers: Contents in commands.txt should be replaced with the below commands

     cd /opt/vmware/share/vami/

     ./vami_set_dns NewDNS1 NewDNS2

Regards,

Uman

0 Kudos
AdrianTT
Enthusiast
Enthusiast

Hi Urman,

Sorry been a crazy week so sorry I did not get a chance to respond. I am all about using the REST API's to perform this type of configuration wherever possible so whilst the above looks like a workable solution there is still a bit of manual work there. I have reworked the original code provide into a PowerShell module (https://raw.githubusercontent.com/AdrianBegg/vSphere/master/Module-vCSA-Administration.psm1) which will hopefully a lot more useful.

Basically for your scenario you should be able to do something along the lines of;

Import-Module Module-vCSA-Administration.psm1

$servers = ("server1.fqdn", "server2.fqdn", 'Server3.fqdn")

$credentials = Get-Credential

foreach($server in $servers){

     Connect-VIServerREST $server -Credetnials $credentials

     Set-VCSANetworkConfigDNS -Server "XXX.XXX.XXX.XXX","YYY.YYY.YYY.YYY" -Domains "fqdn.com"

}

I have written a blog post outlining how to use the module (PowerCLI : Get/Set cmdlets for DNS Configuration of the vCenter Server Appliance | Pigeon Nuggets )

Have a great weekend,

Adrian

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Cheers,
0 Kudos
AdrianTT
Enthusiast
Enthusiast

Also should have mentioned to get the current configuration:

Import-Module Module-vCSA-Administration.psm1

$servers = ("server1.fqdn", "server2.fqdn", 'Server3.fqdn")

$credentials = Get-Credential

foreach($server in $servers){

     Connect-VIServerREST $server -Credetnials $credentials

     Get-VCSANetworkConfigDNS

}

Please consider marking this answer "correct" or "helpful" if you think your query have been answered correctly. Cheers,
0 Kudos
virtualtech_wor
Enthusiast
Enthusiast

Thank you for the updates. I haven't yet tested this. got occupied with other priority projects. will test it and share my findings.

0 Kudos
virtualtech_wor
Enthusiast
Enthusiast

Hi Adrian - Hope you are doing good.

(Had to keep this project on hold for long, just got go ahead to work on this).

I'm tyring to use Get-VCSANetworkConfigDNS, but getting error when it's trying to connect to the VCSA to establish session. Getting the same error (page not found) when using browser to acccess the URL.

An error occured connecting to https://$serverxxxx:443/rest/com/vmware/cis/session with the provided credentials. Please check the Server Name, Port and Credentials.

Credentials, server name are correct ones. We are using VCSA 6.5 if that helps.

0 Kudos