Public/Get-AdminsAfterChange.ps1

Function Get-AdminsAfterChange {
    <#
        .Synopsis
            Get list of administrators after eventid 4735 occurs
 
        .Description
            Get list of administrators after eventid 4735 occurs, this is usefull in a security driven environment and you want to log changes to the administrators group in your domain.
            It compares to the file generated with the Get-AdminsBeforeChange function, so you need to use both together.
            Run Get-AdminsBeforeChange once to set a baseline. Then create a scheduled task that runs Get-AdminsAfterChange first and then runs Get-AdminsBeforeChange again.
            This will prevent you from getting false positives.
 
        .Parameter AuthType
            Specifies the authentication method to use. Possible values for this parameter include:
 
            Negotiate or 0
 
            Basic or 1
 
            The default authentication method is Negotiate.
 
            A Secure Sockets Layer (SSL) connection is required for the Basic authentication method.
 
            The following example shows how to set this parameter to Basic.
 
            -AuthType Basic
 
        .Parameter Credential
            Specifies the user account credentials to use to perform this task. The default credentials are the credentials of the currently logged on user unless the cmdlet is run from an Active Directory PowerShell provider drive. If the cmdlet is run from such a
            provider drive, the account associated with the drive is the default.
 
            To specify this parameter, you can type a user name, such as "User1" or "Domain01\User01" or you can specify a PSCredential object. If you specify a user name for this parameter, the cmdlet prompts for a password.
 
            You can also create a PSCredential object by using a script or by using the Get-Credential cmdlet. You can then set the Credential parameter to the PSCredential object The following example shows how to create credentials.
 
            $AdminCredentials = Get-Credential "Domain01\User01"
 
            The following shows how to set the Credential parameter to these credentials.
 
            -Credential $AdminCredentials
 
            If the acting credentials do not have directory-level permission to perform the task, Active Directory PowerShell returns a terminating error.
 
        .Parameter SearchScope
            Specifies the scope of an Active Directory search. Possible values for this parameter are:
 
            Base or 0
 
            OneLevel or 1
 
            Subtree or 2
 
            A Base query searches only the current path or object. A OneLevel query searches the immediate children of that path or object. A Subtree query searches the current path or object and all children of that path or object.
 
            The following example shows how to set this parameter to a subtree search.
 
            -SearchScope Subtree
 
        .Parameter Server
            Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a corresponding domain name or directory server. The service may be any of the following: Active Directory Lightweight Domain Services, Active
            Directory Domain Services or Active Directory Snapshot instance.
 
            Domain name values:
 
            Fully qualified domain name
 
            Examples: corp.contoso.com
 
            NetBIOS name
 
            Example: CORP
 
            Directory server values:
 
            Fully qualified directory server name
 
            Example: corp-DC12.corp.contoso.com
 
            NetBIOS name
 
            Example: corp-DC12
 
            Fully qualified directory server name and port
 
            Example: corp-DC12.corp.contoso.com:3268
 
            The default value for the Server parameter is determined by one of the following methods in the order that they are listed:
 
            -By using Server value from objects passed through the pipeline.
 
            -By using the server information associated with the Active Directory PowerShell provider drive, when running under that drive.
 
            -By using the domain of the computer running Powershell.
 
            The following example shows how to specify a full qualified domain name as the parameter value.
 
            -Server "corp.contoso.com"
 
        .Parameter before
            The file generated by Get-AdminsBeforeChange, this file is used as a ReferenceObject and compared to the file as provided with the -after parameter.
 
        .Parameter after
            Output file for this function (.xml), this file is used as a DifferenceObject and compared to the file generated with Get-AdminsBeforeChange.
 
        .Example
            Get-AdminsAfterChange -before adminbefore.xml -after adminafter.xml
            Get's list of admins from adminbefore.xml as generated by Get-AdminsBeforeChange and creates list of admins after the change in adminafter.xml
 
        .LINK
            Get-AdminsBeforeChange
 
        .LINK
            about_functions_advanced
 
        .LINK
            about_CommonParameters
    #>

    [CmdletBinding(
        SupportsPaging = $true,
        ConfirmImpact = 'Medium'
    )]
    [OutputType('None, or the objects that are different')]
    Param (
        [ValidateSet("Basic", "1", "Negotiate", "0")]
        [Microsoft.ActiveDirectory.Management.ADAuthType]$AuthType = "Negotiate",
        [PSCredential][System.Management.Automation.Credential()]$Credential,
        [int]$ResultPageSize = 256,
        [int]$ResultSetSize = $null,
        [ValidateSet('Base', 'OneLevel', 'Subtree')]
        [Microsoft.ActiveDirectory.Management.ADSearchScope]$SearchScope = "Subtree",
        [string]$Server,
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            HelpMessage = 'Provide .xml file created with Get-AdminsBeforceChange'
        )]
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {
                Test-Path $_
            })]
        [string]$before,
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            HelpMessage = 'Provide .xml file to compare to'
        )]
        [string]$after
    )
    Begin {
        Write-Debug "PsBoundParameters:"
        $PSBoundParameters.GetEnumerator() | ForEach-Object { Write-Debug $_ }
        If ($PSBoundParameters['Debug']) {
            $DebugPreference = 'Continue'
        }
        Write-Debug "DebugPreference: $DebugPreference"

        Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"
    }
    Process {
        Try {
            Write-Debug "Getting EventID 4735 from EventLog"
            Write-Verbose "$($MyInvocation.MyCommand.Name):: Getting EventID 4735 from EventLog"
            Get-EventLog -LogName Security -InstanceId 4735 -Newest 1 -ErrorAction Stop
        } Catch [System.ArgumentException] {
            Write-Debug "Caugth Error"
            Write-Warning "Event 4735 not found"
            Get-ErrorInfo $_
        }
        Try {
            Write-Verbose "$($MyInvocation.MyCommand.Name):: Getting content from file $before"
            Write-Debug "Getting content from file $before"
            Get-Content $before -ErrorAction Stop
        } Catch {
            Write-Debug "Caugth Error"
            Write-Warning "Did you run Get-AdminsBeforeChange first?"
            Get-ErrorInfo $_
        }
        Try {
            Write-Verbose "$($MyInvocation.MyCommand.Name):: Getting current members of Administrators group"
            $GetADGroupParams = @{
                AuthType    = $AuthType
                Identity    = "Administrators"
                Properties  = "Member"
                ErrorAction = "Stop"
            }
            If ($PSBoundParameters['Credential']) { $GetADGroupParams.Credential = $PSBoundParameters['Credential'] }
            If ($PSBoundParameters['Server']) { $GetADGroupParams.Server = $PSBoundParameters['Server'] }

            $adminmembers = Get-ADGroup $GetADGroupParams | Select-Object -ExpandProperty Member
            Write-Verbose "$($MyInvocation.MyCommand.Name):: Exporting current members of Administrators group to $after"
            $adminmembers | Export-Clixml -Path $after
            $ref = Get-Content -Path $before
            $diff = Get-Content -Path $after
        } Catch [System.ArgumentException] {
            Write-Warning "No entries found in Security log, no changes to Administrator memberships detected!"
        } Catch [Microsoft.ActiveDirectory.Management.ADServerDownException] {
            Write-Error "Unable to connect to Active Directory, are you in the domain?"
        } Catch {
            Get-ErrorInfo $_
        }
        Try {
            Write-Verbose "$($MyInvocation.MyCommand.Name):: Comparing data"
            $compare = Compare-Object -ReferenceObject $ref -DifferenceObject $diff
        } Catch {
            Get-ErrorInfo $_
        }
    }
    End {
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Cleaning up"
        Remove-Variable before, after, adminmembers, ref, diff -ErrorAction SilentlyContinue
        If ($PSBoundParameters['Debug']) {
            $DebugPreference = 'SilentlyContinue'
        }
        Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"
        Write-Output $compare

    }
}
Set-Alias -Name Find-AdminsAfterChange -Value Get-AdminsAfterChange -Description "Get Admin users after Admin group change" -Option ReadOnly -PassThru -ErrorAction SilentlyContinue