Add confirmation to powershell script

Emil Mammadli 21 Reputation points
2022-03-28T19:36:14.347+00:00

Hello everyone,

I have one powershell script. https://itdungeon.blogspot.com/2021/11/update-dns-static-servers-in-your-local.html

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

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
    2022-03-28T22:35:54.077+00:00

    Hi,

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

    $inet1=Get-DnsClient | Get-DnsClientServerAddress | where{$_.ServerAddresses -contains "192.168.0.245" -or $_.ServerAddresses -contains "192.168.0.207"}
    
    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 ("192.168.0.245","192.168.0.114")
    }
    }
    

    Please don't forget to mark helpful reply as answer


  2. Rich Matheisen 42,996 Reputation points
    2022-03-29T15:08:20.603+00:00

    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
    2022-03-30T10:42:04.137+00:00

    I've found this script

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

    https://learn.microsoft.com/en-us/answers/questions/174234/how-to-add-dns-record-only-to-computers-with-stati.html

    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('8.8.8.8','8.8.8.4')}
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • 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
    2022-03-30T13:29:14.01+00:00

    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.