title: PowerShell v2.0 DNS Server Search Order Modification
body: <p>I'm extremely new to scripting, but i'll explain what I want to do.
  I want to dynamically add my dns server to my coworkers machines.  I need to figure out how to do this.  Our VPN resets the dns server list every time we vpn in.  Once inside I can change the settings but that's annoying.  I want to create a script that will </p>
  
  <ol>
  <li>Read current DNS servers, and store them into an array</li>
  <li>Determine if the DNS server, that I am attempting to add, is already in the array</li>
  <li>Modify the array into a correct order (ie. put mine first, the rest don't matter, and then add in 8.8.8.8)</li>
  <li>Set the DNS servers to the array</li>
  </ol>
  
  <p>I will be doing this for search order as well, but I think that once I'm able to get this answered I can move on.</p>
  
  <p>I was looking at this article to help set the DNS server search order, however it doesnt have any information on how to view them.
  <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx" rel="nofollow">http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx</a></p>
  
  <p>I'm confident that I can modify and set the DNS server IP addresses, once I can get it to read current status and populate a variable.  </p>
  
  <p>I tried to follow that and then use it to read the dns server search order and populate a variable but it didn't seem to like it. This library says that there is <code>DNSServerSearchOrder</code>, but I have been unable to get anything to save to a variable.
  <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394217(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/aa394217(v=vs.85).aspx</a></p>
  
  <p>I tried to do this in VBS and I had some issues with resizing the array once I set it.  I took this from someone else's script that just displayed the DNS servers.  I'm just trying to manipulate the variable to reorder in this, however it doesn't work correctly.  I was hoping PowerShell would make it easier to modify arrays.</p>
  
  <pre><code>On Error Resume Next
  
  strComputer = "."
  sithDomain = "sith.empire.com"
  
  Set objWMIService = GetObject("winmgmts:" _
   &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
  Set colNicConfigs = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
  
  For Each objNicConfig In colNicConfigs
    DIM strDNSSuffixSO(1) 
    strDNSServerSO = ""
    strDNSHostName = objNicConfig.DNSHostName
    strIndex = objNicConfig.Index
    strDescription = objNicConfig.Description
    strDNSDomain = objNicConfig.DNSDomain
    If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
      For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
        ReDim Preserve strDNSSuffixSO(UBound(strDNSSuffixSO) + 1) 
        strDNSSuffixSO(UBound(strDNSSuffixSO)) = strDNSSuffix
      Next
    End If
  
  i=0
  sithFound = False
  'Wscript.echo sithFound
  For Each strDNSSuffix In strDNSSuffixSO
      If strDNSSuffix = sithDomain Then
          strDNSSuffixSO(i) = strDNSSuffixSO(0)
          strDNSSuffixSO(0) = sithDomain
          sithFound = True
      End If
      i = i+1
  Next
  If sithFound = False Then
      ReDim Preserve strDNSSuffixSO(UBound(strDNSSuffixSO) + 1) 
      strDNSSuffixSO(UBound(strDNSSuffixSO)) = strDNSSuffixSO(0)
      strDNSSuffixSO(0) = sithDomain
      End If
  
  
    strDNSServerSO = ""
    If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
      For Each strDNSServer In objNicConfig.DNSServerSearchOrder
        strDNSServerSO = strDNSServerSO &amp; VbCrLf &amp; String(37, " ") &amp; _
   strDNSServer
      Next
    End If
    strDomainDNSRegistrationEnabled = _
   objNicConfig.DomainDNSRegistrationEnabled
    strFullDNSRegistrationEnabled = objNicConfig.FullDNSRegistrationEnabled
    strDNSSettings = strDNSSettings &amp; VbCrLf &amp; VbCrLf &amp; _
     "  Network Adapter " &amp; strIndex &amp; VbCrLf &amp; _
     "    DNS Domain Suffix Search Order:" &amp; strDNSSuffixSO(0) &amp; strDNSSuffixSO(1) '&amp; strDNSSuffixSO(2) &amp; VbCrLf '&amp; _
     REM "    DNS Server Search Order:" &amp; strDNSServerSO &amp; VbCrLf 
     REM "    Domain DNS Registration Enabled: " &amp; _
     REM strDomainDNSRegistrationEnabled &amp; VbCrLf &amp; _
     REM "    Full DNS Registration Enabled:   " &amp; _
     REM strFullDNSRegistrationEnabled
  Next
  
  WScript.Echo VbCrLf &amp; "DNS Settings" &amp; VbCrLf &amp; VbCrLf &amp; _
   "Host Name: " &amp; strDNSHostName &amp; strDNSSettings &amp; VbCrLf &amp; sithFound
  </code></pre>