Public/Get-AdminsBeforeChange.ps1
Function Get-AdminsBeforeChange { <# .Synopsis Sets a baseline for the Get-AdminsAfterChange function .Description Get list of administrators before eventid 4735 occurs, this is usefull in a security driven environment and you want to log changes to the administrators group in your domain. 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 Baseline output file (xml) .Example Get-AdminsBeforeChange -before adminbefore.xml Generates baseline file for use with Get-AdminsAfterChange .Inputs [String] .LINK Get-AdminsAfterChange .LINK about_functions_advanced .LINK about_CommonParameters #> [CmdletBinding( SupportsPaging = $true, ConfirmImpact = 'Medium' )] [OutputType('Microsoft.ActiveDirectory.Management.ADAccount')] 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" )] [String]$before ) 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-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 $before" $adminmembers | Export-Clixml -Path $before } Catch { Write-Warning $_.Exception.Message } } End { If ($PSBoundParameters['Debug']) { $DebugPreference = 'SilentlyContinue' } Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended" } } Set-Alias -Name Find-AdminsBeforeChange -Value Get-AdminsBeforeChange -Description "Get Admin users before Admin group change" -Option ReadOnly -PassThru -ErrorAction SilentlyContinue |