DNSSuffix.psm1
Function Set-PrimaryDNSSuffix { [cmdletbinding(SupportsShouldProcess)] [OutputType("None")] Param( [Parameter(Position = 0, HelpMessage = "Enter the new primary DNS Suffix name e.g. company.pri")] [ValidateNotNullOrEmpty()] [ValidatePattern("\w+\.\w+")] [string]$DNSSuffix, [switch]$SynchronizeSuffix, [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [Alias("cn","host")] [string[]]$ComputerName = $env:ComputerName, [ValidateNotNullOrEmpty()] [Alias("RunAs")] [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, [ValidateNotNullOrEmpty()] [Alias("RunAs")] [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 |