DNSSuffix.psm1

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Function Set-PrimaryDNSSuffix {
    [cmdletbinding(SupportsShouldProcess)]
    Param(
        [Parameter(Position = 0, HelpMessage = "Enter the new primary DNS Suffix name e.g. company.pri")]
        [string]$DNSSuffix,
        [switch]$SynchronizeSuffix,
        [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias("cn","host")]
        [string[]]$Computername = $env:computername,
        [pscredential]$Credential
    )
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Using a primary DNS Suffix of $DNSSuffix"
        if ($SynchronizeSuffix) {
            $synch = 1
        }
        else {
            $Synch = 0
        }
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Synchronize DNS with domain membership: $SynchronizeSuffix [$synch])]"
        #define the scriptblock to run on each computer
        $sb = {
            Param([string]$Value,[int]$Synch)
            Set-ItemProperty -path HKLM:\system\CurrentControlSet\Services\tcpip\parameters -Name Domain -Value $value
            Set-ItemProperty -path HKLM:\system\CurrentControlSet\Services\tcpip\parameters -Name 'NV Domain' -Value $Value
            Set-ItemProperty -path HKLM:\system\CurrentControlSet\Services\tcpip\parameters -Name SyncDomainWithMembership -Value $Synch
        }
        #hashtable of parameters to splat to Invoke-Command
        $invoke = @{
            Scriptblock  = $sb
            ArgumentList = $DNSSuffix,$synch
            ErrorAction  = "Stop"
        }
        if ($Credential) {
            $invoke.add("Credential", $Credential)
        }
    } #begin
    
    Process {
        foreach ($computer in $computername) {
            Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Setting Primary DNS Suffix on $computer"
            $invoke.computername = $computer
            if ($PSCmdlet.ShouldProcess("$($DNSSuffix) on $computer")) {
                Try {
                    Invoke-Command @invoke
                }
                Catch {
                    Write-Warning "[$((Get-Date).TimeofDay) PROCESS] Error with command on $Computer. $($_.Exception.Message)"
                }
            }
        } #foreach
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
    } #end

} #close Set-PrimaryDNSSuffix

Function Get-PrimaryDNSSuffix {
    [cmdletbinding()]
    [OutputType("PSCustomObject")]

    Param(
        [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias("cn","host")]
        [string[]]$Computername = $env:computername,
        [pscredential]$Credential
    )
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
        #define the scriptblock to run on each computer
        $sb = {
            $reg = Get-ItemProperty  HKLM:\system\CurrentControlSet\Services\tcpip\parameters 
            [pscustomobject]@{
                Computername = $reg.hostname
                Domain       = $reg.Domain
                'NV Domain'  = $reg.'NV Domain'
                SynchronizeSuffix = $reg.SyncDomainWithMembership -as [bool]
                
            }
        }
        #hashtable of parameters to splat to Invoke-Command
        $invoke = @{
            Scriptblock      = $sb
            ErrorAction      = "Stop"
            HideComputername = $True
        }
        if ($Credential) {
            $invoke.add("Credential", $Credential)
        }
    } #begin

    Process {
        
        foreach ($computer in $computername) {
            Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting Primary DNS Suffix settings from $computer."
            $invoke.computername = $computer

            Try {
                Invoke-Command @invoke | Select-object * -ExcludeProperty RunspaceID
            }
            Catch {
                write-warning "[$((Get-Date).TimeofDay) PROCESS] Error with command on $Computer. $($_.Exception.Message)"
            }
        } #foreach
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
    } #end

} #close Get-PrimaryDNSSuffix