Add confirmation to powershell script

Emil Mammadli 21 Reputation points

Hello everyone,

I have one powershell script.

$inet1=Get-DnsClient | Get-DnsClientServerAddress | where{$.ServerAddresses -contains "" -or $.ServerAddresses -contains ""}
foreach($nic in $inet1){
Set-DnsClientServerAddress -InterfaceIndex $nic.InterfaceIndex -ServerAddresses ("","")

I want to add automatically answer yes. How can I do? Please help me. Thanks.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,176 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Thameur-BOURBITA 17,341 Reputation points


    If I understand your question , you want add confirmation for each NIC modification.
    Below a Example :

    $inet1=Get-DnsClient | Get-DnsClientServerAddress | where{$_.ServerAddresses -contains "" -or $_.ServerAddresses -contains ""}
    foreach($nic in $inet1){
    $interfaceAlias = $nic.InterfaceAlias
    $yes = Read-Host -Prompt "type Yes to confirm the modification on $interfaceAlias :"
    if($yes -eq "Yes"){
    Set-DnsClientServerAddress -InterfaceIndex $nic.InterfaceIndex -ServerAddresses ("","")

    Please don't forget to mark helpful reply as answer

  2. Rich Matheisen 42,996 Reputation points

    Before you run your script, run this:

    Get-ExecutionPolicy -List  
            Scope ExecutionPolicy  
            ----- ---------------  
    MachinePolicy       Undefined  
       UserPolicy       Undefined  
          Process    RemoteSigned  
      CurrentUser       Undefined  
     LocalMachine    RemoteSigned  

    There is nothing in that script that changes the execution policy. Are you seeing that warning when you start a PowerShell session (before you run anything)? If you do, it's because there's something in the PowerShell profile that trying to change it.

    Also check the $Profile variable when you start the session with "run as administrator" and when you start a session as an unprivileged user.

    Have a look at this, too: about_profiles

  3. Emil Mammadli 21 Reputation points

    I've found this script

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

    It's a great script. But when I clicked appear this error:

    annot find an overload for "SetDNSServerSearchOrder" and the argument count: "2".
    At C:\Users\Administrator\Desktop\dns12.ps1:1 char:131

    • ... rEach-Object { $_.SetDNSServerSearchOrder('','')}
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [], MethodException
    • FullyQualifiedErrorId : MethodCountCouldNotFindBest

    But when I type one DNS ip, process is done. How can I solve?

    0 comments No comments

  4. Newbie Jones 1,276 Reputation points

    You can't have the execution policy in the script.

    You set this outside the script. If this is running from a server, its usually dictated by some sort of group policy for that server. As its not feasible to have to set the execution policy each and every time.

    I have policies that allow unrestricted access, but only from a certain number of dev machines and the integration server that runs scripts. (But your scripts really should be signed).

    If you can't set\force the execution policy, its probably down to rights and policy, which you will need to speak to your administrators about.

    Certain organisations will block unsigned scripts from running or only allow from certain workstations.