1 2 Previous Next 23 Replies Latest reply on Jul 1, 2020 8:09 AM by ganapa2000 Go to original post
      • 15. Re: Update DNS
        ganapa2000 Hot Shot

        not sure, why is happening. I am re-attaching

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

          That worked.

           

          Give it a try like this.

          I made some changes:

          - the $code here-string should be between single quotes

          - I avoided the $adapters variable by placing everything in a pipeline construct

          - the command value is placed in quotes

          - the display of $result shall be inside the ForEach loop

           

          $reportlocation1 = ".\Prod_Info.csv"

          $WPassword = "password@123"

          $pass = ConvertTo-SecureString -AsPlainText $WPassword -Force

          $Creds = New-Object System.Management.Automation.PSCredential ("admin", $pass)


          $code = @'

          Get-WmiObject Win32_NetworkAdapterConfiguration |

          where {$_.DNSServerSearchOrder -contains "192.168.15.5"} |

          Set-DnsClientServerAddress -ServerAddresses "192.168.15.100","192.168.15.101" -Confirm:$false

          '@


          $ScriptBlock = @"

          function Elevate-Process  {

          param ([string]`$exe = `$(Throw "Pleave provide the name and path of an executable"),[string]`$arguments)

          `$startinfo = new-object System.Diagnostics.ProcessStartInfo

          `$startinfo.FileName = `$exe

          `$startinfo.Arguments = `$arguments

          `$startinfo.verb = `"RunAs`"

          `$startinfo.UseShellExecute = `"False`"

          `$startinfo.CreateNoWindow = `"True`"

          `$process = [System.Diagnostics.Process]::Start(`$startinfo)

          }

          Elevate-Process -Exe powershell.exe -Arguments `"-noninteractive -command '$code'"

          "@


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

          ForEach-Object -Process {

              $sInvoke = @{

                  VM              = $_.Name

                  GuestCredential = $Creds

                  ScriptTYpe      = 'powershell'

                  ScriptText      = $ScriptBlock

                  ErrorAction = 'Stop'

              }

              try{

                  $result = Invoke-VMScript @sInvoke

                  "$($row.Name) DNS has been changed"

                 }

              catch{

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

              }

              $result

          }

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

            LucD,

             

            I dont see any error but still DNS IP is not getting changed.

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

              The following seems to be working for me.
              I made a few changes:

              - I'm not using the Get-WmiObject anymore

              - the $code needs to be inside the $scriptblock

              - some quoting and escaping was adjusted

               

              $reportlocation1 = ".\Prod_Info.csv"

              $WPassword = "password@123"

              $pass = ConvertTo-SecureString -AsPlainText $WPassword -Force

              $Creds = New-Object System.Management.Automation.PSCredential ("admin", $pass)


              $ScriptBlock = @'

              $code = @"

              Get-DnsClientServerAddress | where{`$_.ServerAddresses -contains '192.168.15.5'} |

              Set-DnsClientServerAddress -ServerAddresses '192.168.15.100','192.168.15.101' -Verbose -Confirm:`$false

              "@


              function Elevate-Process  {

              param ([string]$exe = $(Throw "Pleave provide the name and path of an executable"),[string]$arguments)

              $startinfo = New-Object System.Diagnostics.ProcessStartInfo

              $startinfo.FileName = $exe

              $startinfo.Arguments = $arguments

              $startinfo.verb = "RunAs"

              $startinfo.UseShellExecute = $false

              $startinfo.CreateNoWindow = $true

              $process = [System.Diagnostics.Process]::Start($startinfo)

              }

              Elevate-Process -Exe powershell.exe -Arguments "-noninteractive -command &{$code}"

              '@


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

              ForEach-Object -Process {

                  $sInvoke = @{

                      VM              = $_.Name

                      GuestCredential = $Creds

                      ScriptTYpe      = 'powershell'

                      ScriptText      = $ScriptBlock

                      ErrorAction = 'Stop'

                  }

                  try{

                      $result = Invoke-VMScript @sInvoke

                      "$($row.Name) DNS has been changed"

                     }

                  catch{

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

                  }

                  $result

              }

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

                Hi LucD,

                 

                This works excellent on the server where UAC is disabled but on the UAC enabled server still it doesnt work.

                 

                I tried this script to check the UAC status, still it is not working

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

                  I tested again with several combinations, and this works for me.

                   

                  Is the user member of the Administrators group on the target machine?

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

                    LucD,

                     

                    The user is a domain admin and its present in Administrators Group on the target machine.

                     

                    The issue is on all Windows Servers 2012 R2 and 2016 Servers. But when I disable the UAC using the registry setting and reboot the server. the script changes the DNS IP.

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

                      I don't have any W2K12R2 servers but on my W2K16, where UAC is enabled, it works perfectly.

                      Not a lot more I can do I'm afraid.

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

                        Thanks a lot for your help. I was able to get this script working by creating a local Administrator account. This helped me to change the DNS without disabling the UAC.

                         

                        As always, you are simply superb. Thanks you very much

                        1 2 Previous Next