Function Set-ADSafeMode {
<#
.CREATED BY:
Matthew A. Kerfoot
.CREATED ON:
2/18/2015
.Synopsis
Enables the AD recycling Bin and prevents all OUs\computer\users accounts from accidental deletion.
.DESCRIPTION
Checks the AD recycling bin to see if it is enabled, if not it will enable it. Then it checks all OUs `
in the domain to see if they are being prevented from accidental deletion, if not it will check the `
check box that prevents them from accidental deletion. This is also done for all user and computer accounts on your domain.
.FUNCTIONALITY
PowerShell v2 (must be ran from a DC(the activedirectory module is required))
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[string]$VerbosePreference = "SilentlyContinue",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[string]$ErrorActionPreference = "SilentlyContinue",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[string[]]$LogDate = (get-date -format "MM-d-yy-HH")
)
Function global:Write-Verbose { [string] $Message }
Import-Module activedirectory
$VerbosePreference = "Continue"
Start-Transcript -Path "C:\Windows\temp\$LogDate.log"
#More or less turns on and\or enables logging to begin.
Write-Verbose
$Bin = Get-ADOptionalFeature -Filter 'name -like "Recycle Bin Feature"' -Properties IsDisableable | `
Select @{ Name = "NeedsToBeEnabled" ; Expression = { ( $_.IsDisableable ) } }
If ($Bin.NeedsToBeEnabled -eq $False)
{
Write-Host "The Active Directory Recycling Bin has already been enabled!" -ForegroundColor "Green"
}
Else
{
Write-Host "The Active Directory Recycling Bin NEEDS TO BE ENABLED!" -ForegroundColor "Red"
Enable-ADOptionalFeature 'Recycle Bin Feature' -Scope ForestOrConfigurationSet -Target $env:USERDOMAIN -WhatIf
Write-Host "The Active Directory Recycling has now been enabled!!" -ForegroundColor "Red"
}
#Sets all OU to be protewcted against accidental deletion
Get-ADOrganizationalUnit -filter * -Properties ProtectedFromAccidentalDeletion | `
Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $true -Verbose -WhatIf
#Sets all user and Computer accounts to be protected from accidental deletion
Get-ADObject -filter * -Properties ProtectedFromAccidentalDeletion | Where-Object {$_.ObjectClass -eq "user" -or $_.ObjectClass -eq "Computer"} | Set-ADObject -ProtectedFromAccidentalDeletion $true -WhatIf
Write-Host "Script Completed Successfully!" -ForegroundColor "Green" | Tee-Object "C:\Windows\temp\$LogDate.log" -Verbose
#Ends the logfile
Stop-Transcript
} Set-ADSafeMode