Public/Move-ADSIDomainControllerToSite.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function Move-ADSIDomainControllerToSite
{
<#
 .SYNOPSIS
  Move-ADSIDomainControllerToSite moves the current DC to another site.
  
 .DESCRIPTION
  Move-ADSIDomainControllerToSite moves the current DC to another site.
   
  MSDN Documention on 'DirectoryServer.MoveToAnotherSite Method'
  https://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.directoryserver.movetoanothersite(v=vs.110).aspx
  
 .PARAMETER ComputerName
  Specifies the Domain Controller
  
 .PARAMETER Credential
  Specifies alternate credentials to use. Use Get-Credential to create proper credentials.
  
 .PARAMETER Site
  Name of the Active Directory site
  
 .EXAMPLE
  Move-ADSIDomainControllerToSite -ComputerName dc1.ad.local -site "Paris"
   
  Connects to remote domain controller dc1.ad.local using current credentials and
  moves it to the site "Paris".
  
 .NOTES
  Version History
   1.0 Initial Version (Micky Balladelli)
   1.1 Update (Francois-Xavier Cat)
     Rename from Move-ADSIReplicaToSite to Move-ADSIDomainControllerToSite
     Add New-ADSIDirectoryContext to take care of the Context
     Other minor modifications
  
  Authors
   Micky Balladelli
    balladelli.com
    micky@balladelli.com
    @mickyballadelli
    
   Francois-Xavier Cat
    lazywinadmin.com
    @lazywinadm
    github.com/lazywinadmin
#>

    
    [CmdletBinding()]
    PARAM
    (
        [Parameter(Mandatory)]
        [string]$ComputerName,
        
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [Parameter(Mandatory = $true)]
        [string]$Site
    )
    PROCESS
    {
        TRY
        {
            # DirectoryContext Splatting
            $Splatting = $PSBoundParameters.Remove("Site")
            # Create the Context
            $Context = New-ADSIDirectoryContext -ContextType 'DirectoryServer' @Splatting
            
            $DomainController = [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController($context)
            
            Write-Verbose -Message "[Move-ADSIDomainControllerToSite][PROCESS] $($DomainController.name) to site $Site"
            $DomainController.MoveToAnotherSite($Site)
        }
        CATCH
        {
            Write-Error -Message "[Move-ADSIDomainControllerToSite][PROCESS] Something wrong happened"
            $Error[0].Exception.Message
        }
    }
}