Skip to content

Set-DnsIPAddressRemote

Windows: Configures static DNS server addresses for an adapter

#Requires -Version 5.1
#Requires -Modules NetTCPIP

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    [string]$InterfaceAlias,

    [Parameter(Mandatory = $true)]
    [string[]]$DnsServers,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process {
    try {
        $session = $null
        if ($ComputerName -ne $env:COMPUTERNAME) {
            $sessionParams = @{
                'ComputerName' = $ComputerName
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential) {
                $sessionParams.Add('Credential', $Credential)
            }
            $session = New-CimSession @sessionParams
            Set-DnsClientServerAddress -CimSession $session -InterfaceAlias $InterfaceAlias -ServerAddresses $DnsServers -ErrorAction Stop
        }
        else {
            Set-DnsClientServerAddress -InterfaceAlias $InterfaceAlias -ServerAddresses $DnsServers -ErrorAction Stop
        }

        $result = [PSCustomObject]@{
            InterfaceAlias = $InterfaceAlias
            DnsServers     = $DnsServers -join ", "
            ComputerName   = $ComputerName
            Action         = "DnsServersConfigured"
            Status         = "Success"
            Timestamp      = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
    finally {
        if ($null -ne $session) {
            Remove-CimSession $session
        }
    }
}

Specifies the friendly name of the network interface (e.g., "Ethernet").

Specifies an array of DNS server IP addresses to configure.

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.