Posts tagged “Active Directory

PowerShell module with functions to Get AD FSMO roles, Get and Set NTP server setting


This module can be downloaded from the Microsoft Script Center Repository.

The module does not require ActiveDirectory PS module, and includes 3 functions:

Get-ADRole: This is a function to return one or all DC FSMO role holders in the current AD forest

Get-NTPDCs: This is a function to return NTP server settings for one or all DCs in current AD forest

Set-NTP: This is a function to change NTP server settings for one or more DCs in current AD forest

Example:

$DCNames = ((([DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Sites).Servers).Name
$DCNames -notmatch (Get-ADRole PdcRole).DCName | % { Set-NTP $_ time-c.nist.gov }

This example will change NTP server setting on all DCs except PDC emulator to ‘time-c.nist.gov’

Of course for this to work, it needs to to be run under an AD user that has permission to write to the DCs’ registry.

Getting started:

  • To use the functions/cmdlets in this module, download this module, extract the files using Winrar for example.
  • Open PowerShell ISE as Administrator
  • Open and execute the Install-Module.ps1 script
Advertisements

Using Powershell to get list of all Domain Controllers with static IPs and email it as CSV file attachment


This is an update of a prior post. It adds the functionality of exporting the output to CSV format that can be opened in Excel, and emailing it out.

DC2

# Script to list domain controllers with static IP addresses
# Sam Boutros
# 6/13/2014 – Rev 1
# 6/16/2014 – Rev 2 – Write output to CSV file, email it out
#
$MailRelay = “192.168.xxx,yyy” # Put the FQDN or IP address of your SMTP relay server here
$SMTPPort = 25 # Replace port number if needed
$Sender = “Sam Boutros <abcd@bcde.com>” # Edit this line to update the email sender
$Recipients = “Recipient1 <name1@domain2.com>”, “R2 <name3@domain4.com>” # Put email recipients here
$EmailSubject = “List of domain controllers with static IPv4 addresses”
$EmailBody = “Please see attached list of domain controllers with static IPv4 addresses”
# End data entry section
#
$Loc = Get-Location
$Date = Get-Date -format yyyyMMdd_hhmmsstt
$CSVfile = $Loc.path + “\DCsWithStaticIPs_” + $Date + “.csv”
write-output “Listing all domain controllers with static IPs:”
$DCs = Get-ADDomainController -Filter *
write-output ‘DC Name,Static IPv4’ | out-file -Filepath $CSVfile -append -encoding ASCII
foreach ($DC in $DCs.Hostname)
{
$ints = Get-WmiObject -ComputerName $DC -Query “select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and DHCPEnabled=FALSE”
ForEach ($int in $ints.IPAddress)
{
if ($int.Contains(“.”)) # This is IPv4, not IPv6
{
write-output “DC Name: $DC, Static IPv4: $int”
$csvline = $DC + ‘,’ + $int
write-output $CSVline | out-file -Filepath $CSVfile -append -encoding ASCII

}
}
}
send-mailmessage -from $Sender -to $Recipients -subject $EmailSubject -body $EmailBody -Attachments $CSVfile -smtpServer $MailRelay -Port $SMTPPort