1 2 Previous Next 23 Replies Latest reply on Jul 1, 2020 8:09 AM by ganapa2000

    Update DNS

    ganapa2000 Hot Shot

      Hi,

       

      I am trying to update the DNS on all the VMs from the list. Now I am stuck, how to update the DNS IP with 192.168.15.100 and 192.168.15.101.

       

      Please help.

       

      Get-VM MyApp1 | Get-NetworkAdapter |

      Select-Object @{N="VM";E={$_.Parent.Name}},

         @{N="NIC";E={$_.Name}},

         @{N="Network";E={$_.NetworkName}},

        MacAddress,

         @{N='IP';E={

         $vNIc = $_

         ($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress -join '|'

         }},

         @{N='DNS';E={($_.Parent.ExtensionData.Guest.Net.DNSConfig).IPAddress -join ', '}}

       

      Current Output

      VM    : MyApp1
      NIC   : Network adapter 1
      Network: 12_dvs

      MacAddress : 00:50:56:97:79:da

      IP    : 192.168.3.17
      DNS   : 192.168.1.5, 192.168.1.6
        • 1. Re: Update DNS
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          That is something that is done inside the Guest OS and depends on which type of Guest OS you have running on the VM.
          There used to be a Set-VMGuestNetwork cmdlet, but that has been removed.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 3. Re: Update DNS
            ganapa2000 Hot Shot

            LucD,

             

            I tried as below, the script runs without any error but DNS is not getting updated.

             

            If the execute the commands directly, DNS is getting changed but when I use with invoke-vmscript, it is not working.

             

            Please help

             

            $reportlocation1 = ".\Prod_Info.csv"

             

            $code = @'

            $Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.DNSServerSearchOrder -eq "192.168.15.5"}

            $Adapters | Set-DnsClientServerAddress -ServerAddresses "192.168.15.100","192.168.15.101"

            '@

             

            Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

            ForEach-Object -Process {

                $sInvoke = @{

                    VM              = $_.Name

                    GuestCredential = $Creds

                    ScriptTYpe      = 'powershell'

                    ScriptText      = $code

                    ErrorAction = 'Stop'

                }

                try{

                    $result = Invoke-VMScript @sInvoke

                   }

                catch{

                    Throw "Failed for $($row.Name)"

                }

            }

            • 4. Re: Update DNS
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              Are you sure the Where-clause is picking the correct adapter?

               

              Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.DNSServerSearchOrder -eq "192.168.15.5"}

               

              Can you check with only running

               

              Get-WmiObject Win32_NetworkAdapterConfiguration | Select -ExpandProperty DNSServerSearchOrder
              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 5. Re: Update DNS
                ganapa2000 Hot Shot

                LucD,

                 

                Get-WmiObject Win32_NetworkAdapterConfiguration | Select -ExpandProperty DNSServerSearchOrder

                 

                I am getting the DNS Server`s IP as below

                 

                192.168.15.5

                192.168.15.6

                • 6. Re: Update DNS
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  That is returning an array, and the -eq will not work

                  Try with

                   

                  Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.DNSServerSearchOrder -contains "192.168.15.5"}
                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 7. Re: Update DNS
                    ganapa2000 Hot Shot

                    LucD,

                     

                    After changing, I am now getting error as below. Also attached the complete script for your reference

                     

                    ScriptOutput

                    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                    |  At line:2 char:2

                    |  +  | Set-DnsClientServerAddress -ServerAddresses "192.168.15.100","192.168.15.101"}

                    |  +  ~

                    |  An empty pipe element is not allowed.

                    |      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx

                    |     ception

                    |      + FullyQualifiedErrorId : EmptyPipeElement

                    |

                    |

                    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                    • 8. Re: Update DNS
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      Did you assign the line with the Where-clause to the variable $adapters?

                      Looks like that variable is empty

                       

                      PS: I can't read the ZIP file you attached

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 9. Re: Update DNS
                        ganapa2000 Hot Shot

                        yes, I assigned the $Adapter variable to the line that you mentioned

                         

                        $Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.DNSServerSearchOrder -contains "192.168.15.5"}

                        $Adapters | Set-DnsClientServerAddress -ServerAddresses "192.168.15.100","192.168.15.101"

                         

                        again attaching the script for your reference

                         

                        the above command works, if the execute directly on the any server but using the script, it is not working.

                        • 10. Re: Update DNS
                          LucD Guru
                          Community WarriorsvExpertUser Moderators

                          I see what happened, you change the delimiters of the here-string to double quotes, which means variable substitution will take place.

                          Define the $code with single quotes as follows

                           

                          $code = @'

                          $Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.DNSServerSearchOrder -contains "192.168.15.5"}

                          $Adapters | Set-DnsClientServerAddress -ServerAddresses "192.168.15.100","192.168.15.101"

                          '@

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 11. Re: Update DNS
                            ganapa2000 Hot Shot

                            LucD,

                             

                            I see Invoke-VMScript needs UAC to be disabled on the Servers. After I disabled the UAC on the Servers and rebooting. I am able to make the change using the script

                             

                            Is there a way to change without disabling the UAC on the Servers ?

                            • 12. Re: Update DNS
                              LucD Guru
                              User ModeratorsvExpertCommunity Warriors

                              Invoke-VMScript does not need UAC to be disabled.

                              Commands you run in the Guest OS via Invoke-VMScript might require UAC to be disabled to avoid prompting.

                               

                              And no, if one could bypass UAC that easily, it wouldn't be much of a security measure.

                               

                              You could try the option with RunAs, but I'm not sure that this will work in this case.

                              See for example How-to: Run with elevated permissions

                              You would need to start an elevated PowerShell session from within the non-elevated script you launch via Invoke-VMScript.

                               

                              For an example with Invoke-VMScript see Invoke-VMScript install App as an administrator

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 13. Re: Update DNS
                                ganapa2000 Hot Shot

                                LucD,

                                 

                                I tried as attached but I see script executes without any error but DNS is not getting updated.

                                 

                                Please help, if I have missed anything.

                                • 14. Re: Update DNS
                                  LucD Guru
                                  User ModeratorsvExpertCommunity Warriors

                                  I'm afraid I can't open the attached file

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  1 2 Previous Next