If you are only looking for the script and are not interested what else I say, just grab them here:
#Define the function Enable-Auditing. The URL parameter accepts pipeline input. It also enables log trimming, and log retention time is set to 30 days. This part is kind of "hardcoded", but it should not be too difficult to change it. function Enable-Auditing { param([Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]$Url, [Parameter(Mandatory=$True)]$AuditedActions); $site = Get-SPSite $Url; $site.TrimAuditLog = $true; $site.AuditLogTrimmingRetention = 30; $site.Audit.AuditFlags = $AuditedActions; $site.Audit.Update(); $site.Dispose(); } <# Run the commands to apply the settings to specific Site Collections. For the -AuditedAction parameter, input any of the following: "All" to audit all auditable actions. "None" to disable auditing An array of action names to enable auditing a specific set of actions to audit, e.g. "Update", "Delete", "Search". Check MSDN documentation for a complete list of auditable actions: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spauditmasktype.aspx #> Enable-Auditing -URL https://teamsite.contoso.com -AuditedActions "Update", "Delete"
As the -URL parameter accepts pipeline inputs, batch action can be done to multiple site collection with one line of command such as the one below:
#The command below enables auditing Update and Delete actions on all Site Collections whose URL contains "hr". Get-SPSite -WebApplication http://teamsite.contoso.com -Limit All | ? {$_.url -like "*hr*"} | ForEach-Object {Enable-Auditing -Url $_.url -AuditedActions "Update", "Delete"}
OK, if you are interested in my monologue discussing this function, please read on. Otherwise, the content above is all you need.
How do you make sure auditing is enabled all the time? How may Site Collections do you need to manage? Does each Site Collection has its own Site Collection Administrator?
Site Collection Administrators has the permission to change site audit settings. That’s a potential risk since they can intentionally or unintentionally change the audit settings, while auditing is usually an organization-wide policy that needs to be enforced. Sometimes, turning on unnecessary auditing is bad as well as it will make the content DB grow faster. A single piece of audit log is about 1KB. Imagine, 1000 people are visiting 100 locations in a day!
One solution is to create a PowerShell scripts running under a task scheduler that enforces auditing policies, including:
- Actions to audit
- Whether to enable audit log trimming
- If log trimming enabled, how many days of log to retain
In SharePoint Management Shell, there is no direct cmdlet for this purpose yet. We can define a function to make batch operations easier.
If you run Get-Member on a SPSite object, you will find that there are a few properties related to auditing:
- Audit
- AuditLogTrimmingCallout
- AuditLogTrimmingRetention
To enable/disable auditing, the trick is to set the value of the SPSite.Audit.AuditFlags property. Based on tests, it accepts strings or array of strings. So there comes the code at the beginning of this post.