<#
Copyright (c) 2015, Adam Beardwood
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 
 
1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#>

#Requires -version 2

<#
.SYNOPSIS
	Permissions CDS user homedrives
.DESCRIPTION
    Sets or resets permissions for given user's homedrive
.PARAMETER homepath
	Path to user's homedrive
.PARAMETER samaccountname
	User's samaccountname
.PARAMETER resetpermissions
	Enable to reset permissions
.EXAMPLE
    C:\>Permission-Homedrive.ps1 -homepath "\\controlsdata.com\fs\homes\a" -samaccountname "abc123" -resetpermissions
.NOTES
    Author: Adam Beardwood
    Date: April 14, 2015
	v1.0: Initial Release
#>

function permissionHomedrive
{
	[CmdletBinding()]

	param
	(
	[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage="Path to user's homedrive")][String]$homepath,
	[Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true, HelpMessage="User's samaccountname")][String]$samaccountname,
	[Parameter(Mandatory=$false, Position=2, ValueFromPipeline=$true, HelpMessage="Enable to reset permissions")][Switch]$resetpermissions
	)
	
	$directory = "$HomePath\$samaccountname"
	$acl = Get-Acl $directory
	$acl.SetGroup($(New-Object System.Security.Principal.NTAccount("CDS","Domain Users")))
	$acl.SetOwner($(New-Object System.Security.Principal.NTAccount("CDS","folder.drive.homes.admin")))
	$acl | Set-acl
	
	$acl = Get-Acl $directory
		
	if($resetpermissions){
		write-verbose "Reset permissions to default on homedrive folder"
		try{
			$acl.SetAccessRuleProtection($false,$false)
			$acl.access | %{$acl.RemoveAccessRule($_)} | Out-Null
			$acl | set-acl
		}catch{
			write-host "Error: Couldn't reset homedrive permissions for $samaccountname`n$_" -foregroundcolor yellow;return
		}
	}
	
	write-verbose "Set explicit permissions on homedrive folder"
	try{
		$inherit = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
		$propagation = [system.security.accesscontrol.PropagationFlags]"None"
		$acl.SetAccessRuleProtection($true,$true)
		$acl | set-acl
		
		write-verbose "Re-acquire ACL after inheritance changes"
		$acl = Get-Acl $directory
		
		write-verbose "Remove unwanted inherited permissions"
		foreach($rule in $acl.access){
			foreach($value in $rule.identityReference.Value){
				if($value -match "Domain Users"){
					$acl.RemoveAccessRule($rule) | Out-Null
				}
			}
		}
		
		write-verbose "Add user CDS & AECUK accounts to ACL"
		$accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("CDS\$samaccountname", "Modify", $inherit, $propagation, "Allow")
		$acl.AddAccessRule($accessrule)
		$accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("AECUK\$samaccountname", "Modify", $inherit, $propagation, "Allow")
		$acl.AddAccessRule($accessrule)
		$acl | set-acl
	}catch{
		write-host "Error: Couldn't set homedrive permissions for $samaccountname`n$_" -foregroundcolor yellow;return
	}
}