Skip to content

Instantly share code, notes, and snippets.

@rsyuzyov
Created November 5, 2017 18:33
Show Gist options
  • Select an option

  • Save rsyuzyov/61e39c50e300bbf4dbd5391484868ccb to your computer and use it in GitHub Desktop.

Select an option

Save rsyuzyov/61e39c50e300bbf4dbd5391484868ccb to your computer and use it in GitHub Desktop.
MS DNS - add or update CNAME
function Update-CNAME {
[CmdletBinding()]
param(
[Parameter(Position=0,HelpMessage="Alias (short name)",Mandatory=$true,ValueFromPipeline=$true)]
[string] $Alias,
[Parameter(Position=1,HelpMessage="CName (FQDN)",Mandatory=$true,ValueFromPipeline=$true)]
[string] $Name,
[Parameter(Position=2,HelpMessage="Zone of the alupdatsdfias",Mandatory=$true,ValueFromPipeline=$true)]
[string] $ZoneName,
[Parameter(Position=3,HelpMessage="Domain controller",Mandatory=$true,ValueFromPipeline=$true)]
[string] $ComputerName
)
Try
{
Remove-DnsServerResourceRecord -Name $Alias -ZoneName $ZoneName -RRType CName -Force -ComputerName $ComputerName
}
Catch{}
Try
{
Add-DnsServerResourceRecordCName -Name $Alias -HostNameAlias $Name -ZoneName $ZoneName -ComputerName $ComputerName
}
Catch
{
$Exception = $_.exception.Message
Write-Warning "Alias update failed: $Exception"
}
}
@rsyuzyov

rsyuzyov commented Nov 5, 2017 •

Copy link
Copy Markdown
Author

#Получение списка баз и создание альясов в dns для каждой базы:
Import-Module /scripts/update-cname.ps1
invoke-sqlcmd "select name from sys.databases where database_id > 4" -ServerInstance "srv-mssql" | ForEach-Object {Update-CNAME $_.name "srv-mssql.domain.local" "dbs.domain.local" "srv-dc"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment