Functions/Restart-ActiveDirectory.ps1
|
function Restart-ActiveDirectory { <# .Notes AUTHOR: Skyler Hart CREATED: 09/08/2017 16:03:23 LASTEDIT: 2022-09-04 12:22:27 KEYWORDS: REQUIRES: -Modules ActiveDirectory -RunAsAdministrator .LINK https://wanderingstag.github.io #> [CmdletBinding()] Param ( [Parameter(Mandatory=$false, Position=0)] [Alias('Host','Name','Computer','CN','ComputerName')] [string]$DC = "$env:COMPUTERNAME", [Switch]$All ) if (Get-Module -ListAvailable -Name ActiveDirectory) { if (!($All)) { Write-Information "Restarting Active Directory service on $DC" try {Restart-Service -inputobject $(Get-Service -ComputerName $DC -Name NTDS -ErrorAction Stop) -Force -ErrorAction Stop} catch {Throw "Unable to connect to $DC or failed to restart service."} }#if not all elseif ($All) { $AllDCs = (Get-ADForest).Domains | ForEach-Object {Get-ADDomainController -Filter * -Server $_} foreach ($Srv in $AllDCs) { $SrvName = $Srv.HostName Write-Output "Restarting Active Directory service on $SrvName" try {Restart-Service -inputobject $(Get-Service -ComputerName $SrvName -Name NTDS) -Force} catch {Throw "Unable to connect to $DC or failed to restart service."} }#foreach dc }#elseif } else { Write-Warning "Active Directory module is not installed and is required to run this command." } } |