Cannot find an overload for "SetDNSServerSearchOrder" and the argument count: "2".

Emil Mammadli 21 Reputation points
2022-03-31T12:40:19.47+00:00

Hello everyone,

I want to change DNS IP via powershell script. The script:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Where-Object {!$.DHCPEnabled} | ForEach-Object { $.SetDNSServerSearchOrder('8.8.8.8','8.8.8.4') }

After run the script it says:

Cannot find an overload for "SetDNSServerSearchOrder" and the argument count: "2".

I removed 8.8.4.4 script works. How can I solve this problem?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

9 answers

Sort by: Most helpful
  1. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2022-03-31T13:22:07.077+00:00

    Hi @Emil Mammadli ,

    not quite sure if this is the cause, but can you please try this:

    $DNSSearchOrder = "8.8.8.8","8.8.8.4"  
    Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Where-Object {!$_.DHCPEnabled} | ForEach-Object { $_.SetDNSServerSearchOrder($DNSSearchOrder) }  
    

    Does it work?

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Stoyan Chalakov

    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-03-31T14:37:59.65+00:00

    When dealing with system-type methods a comma is used to separate parameters. This is unlike PowerShell where commas are usually used to create lists (arrays) and spaces are used to separate unnamed parameters.

    The answer provided by @SChalakov creates a list and then passes that list as a single parameter. His answer does, however, contain an omission: it's missing the underbar ("_") character in two places.

    $.DHCPEnabled  should be $_.DHCPEnabled  
      
    and  
    
    $.SetDNSServerSearchOrder  should be $_.SetDNSServerSearchOrder  
    
    
      
    
    1 person found this answer helpful.

  3. Emil Mammadli 21 Reputation points
    2022-03-31T15:20:16.083+00:00

    Hello Stoyan,

    Thanks for help. Unfortunatelly the error occurs again.

    Get-WmiObject : Invalid query "select * from Win32_NetworkAdapterConfiguration where $._IPEnabled=TRUE"
    At C:\Users\Administrator\Desktop\dns12.ps1:1 char:1

    • Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter $._IPEnabled=TRUE ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
    • FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

  4. Emil Mammadli 21 Reputation points
    2022-03-31T15:21:46.93+00:00

    At C:\Users\Administrator\Desktop\dns1121.ps1:2 char:97

    • ... Where-Object {!$._DHCPEnabled} | ForEach-Object { $._SetDNSServerSearchOrder($DN ...
    • ~
      Missing expression after unary operator '!'.
      At C:\Users\Administrator\Desktop\dns1121.ps1:2 char:97
    • ... Where-Object {!$._DHCPEnabled} | ForEach-Object { $._SetDNSServerSearchOrder($DN ...
    • ~~~~~~~~~~~~~~
      Unexpected token '$._DHCPEnabled' in expression or statement.
    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : MissingExpressionAfterOperator

  5. SChalakov 10,576 Reputation points MVP Volunteer Moderator
    2022-03-31T15:32:19.793+00:00

    Hi @Emil Mammadli ,

    please try this one:

     $DNSSearchOrder = "8.8.8.8","8.8.8.4"  
     Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | Where-Object {$_.IPEnabled -eq "True"} | ForEach-Object { $_.SetDNSServerSearchOrder($DNSSearchOrder) }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Stoyan Chalakov


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.