VMware Cloud Community
dekoshal
Hot Shot
Hot Shot
Jump to solution

PowerCLI script for updating/adding three DNS IP ( 1 primary, 2 alt)

Hi,

Need help with script to update/add DNS ip of all esxi host in vcenter. Need to configure three DNS IP's.

Best Regards,

Deepak Koshal

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, got it, there was a typo, it said $esxci and it should say $esxcli.

I corrected the script above.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$dnsServers = '192.168.1.2','192.168.1.3','192.168.1.4'

Get-VMHost | %{

    $esxcli = Get-EsxCli -VMHost $_ -V2

    $esxcli.network.ip.dns.server.list.Invoke() | select -ExpandProperty DNSServers | %{

        $sOldDns = @{

            server = $_

        }

        $esxcli.network.ip.dns.server.remove.Invoke($sOldDns)

    }

    $dnsServers | %{

        $sDns = @{

            server = $_

        }

        $esxcli.network.ip.dns.server.add.Invoke($sDns)

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dekoshal
Hot Shot
Hot Shot
Jump to solution

getting error message :

You cannot call a method on a null-valued expression.

At C:\denverdns.ps1:9 char:5

+     $esxcli.network.ip.dns.server.list.Invoke() | select -ExpandProperty

DNSServ ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

At C:\denverdns.ps1:29 char:9

+         $esxcli.network.ip.dns.server.add.Invoke($sDns)

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you running, do a Get-PowerCLIVersion?

And can you show the script your are using, the .ps1 file?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dekoshal
Hot Shot
Hot Shot
Jump to solution

yes it is .ps1 script.

powercli_version.JPG

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, that's an older version, but supports the V2 option.

Can you show your script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dekoshal
Hot Shot
Hot Shot
Jump to solution

just replaced the ip with x here.

$dnsServers = 'x.x.x.x','1x.x.x.x','x.x.x.x '

 

Get-VMHost | %{

    $esxci = Get-EsxCli -VMHost $_ -V2

    $esxcli.network.ip.dns.server.list.Invoke() | select -ExpandProperty DNSServers | %{

        $sOldDns = @{

            server = $_

        }

        $esxcli.network.ip.dns.server.remove.Invoke($sOldDns)

    }

    $dnsServers | %{

        $sDns = @{

            server = $_

        }

        $esxcli.network.ip.dns.server.add.Invoke($sDns)

    }

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does Get-VMHost actually return any objects?

Are you connected to a vCenter? Check $global:defaultviserver.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dekoshal
Hot Shot
Hot Shot
Jump to solution

Both the command works first list the host and second list the vcenter server.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, got it, there was a typo, it said $esxci and it should say $esxcli.

I corrected the script above.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dekoshal
Hot Shot
Hot Shot
Jump to solution

Thank you. It worked. Smiley Happy

0 Kudos