Windows

Powershell – Zdalna zmiana ustawień adresów DNS

Czasami potrzebujemy szybko zmienić ustawienia adresów serwerów DNS na wielu maszynach jednocześnie. By nie robić tego ręcznie można napisać prosty skrypt w PowerShellu, który zrobi to za nas.

Solution

Poniżej znajduje się prosty skrypt, który znajduje karte sieciową po jej nazwie i ustawia nowe serwery DNS za pomocą funkcji PowerShell.

function Set-DnsServerIpAddress {
    param(
        [string] $ComputerName,
        [string] $NicName,
        [string] $IpAddresses
    )
    if (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
        Invoke-Command -ComputerName $ComputerName -ScriptBlock { param ($ComputerName, $NicName, $IpAddresses)
            write-host "Setting on $ComputerName on interface $NicName a new set of DNS Servers $IpAddresses"
            Set-DnsClientServerAddress -InterfaceAlias $NicName -ServerAddresses $IpAddresses

        } -ArgumentList $ComputerName, $NicName, $IpAddresses

    } else {
        write-host "Can't access $ComputerName. Computer is not online."
    }
}

Użycie skryptu jest stosunkowo proste:

# Where $ServerName can be set as needed
# Where Service is the name of the Network Card (takes wildcard)
# Where IpAddresses are given in brackets
Set-DnsServerIpAddress -ComputerName $ServerName -NicName "Service*" -IpAddresses '8.8.8.8','8.8.4.4','8.8.8.1'

This post was last modified on 11 listopada, 2018 01:17

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

3 tygodnie ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 miesięcy ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 miesięcy ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 miesięcy ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 miesięcy ago