# encoding: ascii
# api: powershell
# title: Search AD Forest
# description: This is essentially a snap-in for an existing script that leverages Active Directory. Typically, you’ll be working with AD objects in your own domain; however, in some instances you may need to work with AD objects that are in a different domain within your forest. This code snippet allows the flexibility to drop in an existing domain-based script and either run it on all domains in the forest (no command line arguments) or a single domain in the forest that matches a command line argument placed into a Where-Object filter.
# version: 0.1
# type: script
# author: AlphaSun
# license: CC0
# x-poshcode-id: 2933
# x-archived: 2012-01-14T07:05:39
# x-published: 2012-08-31T13:53:00
#
# Scenario: Your user account resides on DomainA and you are logged into a workstation that is a member of DomainA. You need to run a script on the Active Directory structure of DomainC which is in the same Active Directory Forest as DomainA. Simply copy the contents of your working script into this code inside the ForEach loop (where noted), save the script, and run from the PoSh command line with the argument “DomainC” (no quotes are necessary—Usage and Examples provided below).
#
########################################################################################
## Search Active Directory Forest
## Search-ADForest
########################################################################################
## NOTE: You have to have sufficient privileges on the target domain for this to work.
## NOTE: This script requires modification/customization prior to use.
########################################################################################
## USAGE:
##   Search-ADForest DomainA
##     * Executes the inserted Active Directory-based script on DomainA only
##   Search-ADForest
##     * Executes the inserted Active Directory-based script on all domains in the forest
########################################################################################
## EXAMPLES:
##
##  * Script run with no command line parameters. This prompts a search of all domains in
##	the current Active Directory Forest.
##
##         PoSH C:\> .\Search-ADForest.ps1
##         Checking DomainA
##         --- script runs and provides normal output for DomainA ---
##
##         Checking DomainB
##         --- script runs and provides normal output for DomainB ---
##
##         Checking DomainC
##         --- script runs and provides normal output for DomainC ---
##
##         Checking DomainD
##         --- script runs and provides normal output for DomainD ---
##
##
##  * Script run with a domain name as a command line parameter. This prompts a search of
##	all domains in the current Active Directory Forest that are 'like' the input.
##
##         PoSH C:\> .\Search-ADForest.ps1 DomainB
##         Checking DomainB
##         --- script runs and provides normal output for DomainB ---
##
##
##  * Script run with an FQDN as a command line parameter. This prompts a search of
##	all domains in the current Active Directory Forest that are 'like' the input.
##
##         PoSH C:\> .\Search-ADForest.ps1 DomainC.foo.bar.fabrikam.com
##         Checking DomainC.foo.bar.fabrikam.com
##         --- script runs and provides normal output for DomainC.foo.bar.fabrikam.com ---
########################################################################################

# Get Domain List
[string]$arg = $Args[0]
$objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$DomainList = @($objForest.Domains | Select-Object Name | Where-Object {$_ -like "*$arg*"})
$Domains = $DomainLIst | foreach {$_.Name}

# Act on all applicable domains
foreach ($Domain in ($Domains))
{
    Write-Host "Checking $Domain" -foregroundcolor Red
    Write-Host ""

    #############################
    ##  YOUR SCRIPT GOES HERE  ##
    #############################

}
