AD Script - Finding out time Source of all the DC's in the domain
<#Created By Abhishek Bansal
Read Note
Script Usage :: Useful in fetching sync time source for all the domain controllers of the domain.
Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". Once saved, run it as Administrator.
Execution & Outputs :: Once executed, there will be two files created as Timesyncdetails.csv containing output and Errorlogs.csv for any errors. #>
$dcs = (Get-ADDomainController -Filter *).Name
Write-Host -ForegroundColor Green "$((Get-ADDomain).PDCEmulator) is holding PDC role.."
$line = 0
$linecount = $dcs.Count
$pct = 0
foreach($dc in $dcs)
{
$line++
$dc = $dc.Trim()
$pct = $line/$linecount * 100
try{
$error.Clear()
Write-Progress -Activity " " -PercentComplete $pct
Invoke-Command -ComputerName $dc -ScriptBlock{"`nFetching Sync time for $($using:dc)"} -ErrorAction Stop
$dc | Select-Object -Property @{n="Hostname";e={$dc}},@{n="Source";e={Invoke-Command -ComputerName $dc -ScriptBlock{w32tm /query /source}}} | Export-Csv ./Timesyncdetails.csv -NoTypeInformation -Append
}
catch{
$dc | Select-Object -Property @{n="Hostname";e={$dc}},@{n="ErrorInfo";e={$($error.exception.Message)}} | Export-Csv ./Errorlogs.csv -NoTypeInformation -Append
}
}
Windows Script - Comparing Windows Services Status before and after reboot
<#Created By Abhishek Bansal
Read Note
Script Usage :: Validation Script, Useful in Comparing Windows Services Status before & after reboot.
Pre requisites :: Copy all the code into a text file, save it with an extension ".PS1". Once saved, run it as Administrator. You need to run this script twice, first prior reboot with Option 1 and second time will be after rebooting with option 2.
Execution & Outputs :: Once executed, there will be two files created as BeforeRestart.csv containing Services status before reboot & one with name AfterRestart.csv containing Services status post reboot. Differences among both the files will be visible directly on the console in Red color. #>
Write-Host -ForegroundColor Yellow "`nPress 1 to fetch services status before reboot"
Write-Host -ForegroundColor Yellow "Press 2 to fetch services status after reboot & proceed with validations.."
$choice = Read-Host "`nEnter your choice = "
if($choice -eq 1)
{
Get-Service | Select Name,DisplayName,Status | Export-Csv ./BeforeRestart.csv -NoTypeInformation
}
elseif($choice -eq 2)
{
Get-Service | Select Name,DisplayName,Status | Export-Csv ./AfterRestart.csv -NoTypeInformation
$Beforerestartdata = Import-Csv .\BeforeRestart.csv
$Afterrestartdata = Import-Csv .\AfterRestart.csv
#Comparing Services.....
foreach($row in $Beforerestartdata)
{
foreach($row1 in $Afterrestartdata)
{
if($($row.Name) -eq $($row1.Name))
{
if($($row.Status) -eq $($row1.Status))
{
Write-Host "$($row.Name) seems fine" -ForegroundColor Green
}
else
{
Write-Host "$($row.DisplayName) service is having differences. Before Reboot Status was $($row.Status) and after reboot status is $($row1.Status)" -ForegroundColor Red
}
}
else
{
continue
}
}
}
}
else
{
Write-Host "Invalid Choice..."
}