ActiveDirectory/ServerRake.ActiveDirectory.Read.ps1

#########################################
### ACTIVEDIRECTORY FUNCTIONS: READ ###
#########################################

# ALL FUNCTIONS WILL NEED TO RUN FROM A JUMPHOST IMPORT THE ACTIVEDIRECTORY MODULE

function Get-ADCOs {

    [CmdletBinding()]
    param (

        [Parameter(Mandatory = $true)]
        [String] $Jumphost,

        [Parameter(Mandatory = $true)]
        [String[]] $ComputerName

    )

    if (!$secret) {

        throw "No secret sessions are currently opened. Start a session with 'Open-SecretSession' and try again."
    
    }

    $response = Invoke-Command -ComputerName $Jumphost -Credential $secret -ArgumentList $secret, $ComputerName -ScriptBlock {
    
        param ($secret, $ComputerName)

        Import-Module ActiveDirectory -WarningAction SilentlyContinue

        $domainController = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().FindDomainController()).Name

        $results = @()
        $results += $ComputerName | % {Get-ADComputer -Identity $_ -Server $domainController -Properties * -Credential $secret}

        return $results

    }

    return $response

}

function Get-ADComputerObjectPath {
    
    [CmdletBinding()]
    param (

        [Parameter(Mandatory = $true)]
        [String] $Jumphost,

        [Parameter(Mandatory = $true)]
        [String[]] $ComputerName

    )

    $COs = Get-ADCOs -Jumphost $Jumphost -ComputerName $ComputerName

    $response = @()
    foreach ($co in $COs) {

        $response += [PSCustomObject]@{

            Server                  =   $co.Name
            "AD CO Location"        =   $co.CanonicalName

        }
    
    }

    return $response

}

function Get-ADGPOReport {

    [CmdletBinding()]
    param (

        [Parameter(Mandatory = $true)]
        [String[]] $ComputerName
    )  

    $remoteScript = @(

        try {

            $logFile = "C:\Temp\gpo_output.xml"
            New-Item -Path $logFile -ItemType File -Force | Out-Null

            gpresult /X $logFile /F

            [xml]$report = Get-Content -Path $logFile
            Remove-Item -Path $logFile -Force

            return $report.Rsop.ComputerResults.GPO | select -Property Name, Enabled, `
                IsValid, FilterAllowed, AccessDenied, SecurityFilter
            
        } catch {

            $_.Exception.Message

        }

    )

    $response = Send-RemoteScript -ComputerName $ComputerName -RemoteScript $remoteScript
    $response | % { $_ | sort -Property Server -Descending | ft }

}