Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save zjorz/93f5407552d30c70378384962c079fb4 to your computer and use it in GitHub Desktop.

Select an option

Save zjorz/93f5407552d30c70378384962c079fb4 to your computer and use it in GitHub Desktop.
Triggering Scheduled Task On DCs On Demand To Initiate DSRM Password Sync
# SOURCE: https://gist.github.com/zjorz/93f5407552d30c70378384962c079fb4/
Invoke-Command -ScriptBlock {
Clear-Host
$scriptMode = "ADSIorSDSP" # "ADSIorSDSP" Or "ADPoSH"
Write-Host ""
Write-Host "###############################################################################" -Foregroundcolor Yellow
Write-Host "### TRIGGERING SCHEDULED TASK ON DCs ON DEMAND TO INITIATE DSRM PWD SYNC ###" -Foregroundcolor Yellow
Write-Host "###############################################################################" -Foregroundcolor Yellow
Write-Host ""
$scheduledTaskName = "AD MGMT - Sync DSRM Account PWD From AD Account To DCs (GPO)" # CONFIGURE THIS!
$dateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host " > Date/Time.............: $dateTime" -Foregroundcolor Yellow
Write-Host " > Scheduled Task Name...: $scheduledTaskName" -Foregroundcolor Yellow
Write-Host ""
If ($scriptMode -eq "ADSIorSDSP") {
$adDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
$adDomainDN = $adDomain.GetDirectoryEntry().Properties["DistinguishedName"].Value
$rwdcPDCFSMOFQDN = $adDomain.PdcRoleOwner.Name
$adsiSearcher = New-Object DirectoryServices.DirectorySearcher
$adsiSearcher.SearchRoot = [ADSI]"LDAP://$rwdcPDCFSMOFQDN/OU=Domain Controllers,$adDomainDN"
$adsiSearcher.Filter = "(|(primaryGroupID=516)(primaryGroupID=521))"
$dcComputerAccountObjects = $adsiSearcher.FindAll()
$dcComputerAccountObjects | ForEach-Object {
If (-not [string]::IsNullOrEmpty($_.Properties.dnshostname)) {
$rwdcFQDN = $_.Properties.dnshostname[0]
Try {
# Force Inbound AD Replication For Domain/Default NC From DC With PDC FSMO To Targeted Destination DC To Make Sure All Objects Exist After Creation
REPADMIN.EXE /REPLICATE $rwdcFQDN $rwdcPDCFSMOFQDN $adDomainDN > $null
Start-Sleep -s 1
# Force A GPUpdate To Make Sure The Scheduled Task Exists On The Targeted Destination DC
Invoke-GPUpdate -Computer $rwdcFQDN -RandomDelayInMinutes 0 -Force -ErrorAction SilentlyContinue
Start-Sleep -s 1
# Trigger The Scheduled Task To Execute
$cimSession = New-CimSession -Name $rwdcFQDN -ComputerName $rwdcFQDN -ErrorAction Stop
Start-ScheduledTask -TaskPath "\" -TaskName $scheduledTaskName -CimSession $cimSession -ErrorAction Stop
Remove-CimSession -Name $rwdcFQDN -ErrorAction Stop
Write-Host "Scheduled Task Triggered On '$rwdcFQDN'..." -ForegroundColor Green
Write-Host ""
} Catch {
If ($Error[0].Exception -match "Verify that the specified computer name is valid, that the computer is accessible over the network") {
$errorReason = "Unable To Contact Server"
} ElseIf ($Error[0].Exception -match "The WinRM client cannot process the request because the server name cannot be resolved.") {
$errorReason = "Unable To Resolve The Name Or Server Does Not Exist"
} ElseIf ($Error[0].Exception -match "The system cannot find the file specified.") {
$errorReason = "Scheduled Task Not Found"
} Else {
$errorReason = "Oops, Something Unknown Went Wrong"
}
Write-Host "Scheduled Task NOT Triggered On '$rwdcFQDN' (Reason: $errorReason)..." -ForegroundColor Red
Write-Host ""
}
}
}
}
If ($scriptMode -eq "ADPoSH") {
$adDomain = Get-ADdomain -Current LocalComputer
$adDomainDN = $adDomain.DistinguishedName
$rwdcPDCFSMOFQDN = $adDomain.PDCEmulator
Get-ADComputer -SearchBase "OU=Domain Controllers,$adDomainDN" -LDAPFilter "(|(primaryGroupID=516)(primaryGroupID=521))" -Properties dNSHostName -Server $rwdcPDCFSMOFQDN | ForEach-Object {
If (-not [string]::IsNullOrEmpty($_.dNSHostName)) {
$rwdcFQDN = $_.dNSHostName
Try {
# Force Inbound AD Replication For Domain/Default NC From DC With PDC FSMO To Targeted Destination DC To Make Sure All Objects Exist After Creation
REPADMIN.EXE /REPLICATE $rwdcFQDN $rwdcPDCFSMOFQDN $adDomainDN > $null
Start-Sleep -s 1
# Force A GPUpdate To Make Sure The Scheduled Task Exists On The Targeted Destination DC
Invoke-GPUpdate -Computer $rwdcFQDN -RandomDelayInMinutes 0 -Force -ErrorAction SilentlyContinue
Start-Sleep -s 1
# Trigger The Scheduled Task To Execute
$cimSession = New-CimSession -Name $rwdcFQDN -ComputerName $rwdcFQDN -ErrorAction Stop
Start-ScheduledTask -TaskPath "\" -TaskName $scheduledTaskName -CimSession $cimSession -ErrorAction Stop
Remove-CimSession -Name $rwdcFQDN -ErrorAction Stop
Write-Host "Scheduled Task Triggered On '$rwdcFQDN'..." -ForegroundColor Green
Write-Host ""
} Catch {
If ($Error[0].Exception -match "The WinRM client cannot process the request because the server name cannot be resolved.") {
$errorReason = "Unable To Contact Server Or Server Does Not Exist"
} ElseIf ($Error[0].Exception -match "The system cannot find the file specified.") {
$errorReason = "Scheduled Task Not Found"
} Else {
$errorReason = "Oops, Something Unknown Went Wrong"
}
Write-Host "Scheduled Task NOT Triggered On '$rwdcFQDN' (Reason: $errorReason)..." -ForegroundColor Red
Write-Host ""
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment