param(
        [string]$serviceuser,
        [bool]$silent = $false
)

if ($silent -eq $false)
{
    $title    = 'Altering folder access for IFS Print Agent'
    $question = 'This script will change the ownership of the folder to the user executing this script(' + $env:USERNAME+
                ').The inherited premissions will be removed. FullControl access will be granted to the objects "BUILTIN\Administrators","SYSTEM",Current User('+$env:USERNAME+
                '),provided Service User'
    $choices  = '&Yes', '&No'
    $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
    if ($decision -eq 1) {
        exit
    }
}
try{
    if (!$serviceuser){
        $serviceuser = Read-Host -Prompt 'Enter the service user to run the IFS Print Agent'
    }
    $folderpath =  (Get-Item .).FullName
    $acl = Get-Acl $folderpath

    Write-Host "Setting the owner to Current user $env:USERNAME"
    $acl.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME)
    Set-Acl $folderpath $acl | Out-Null
    $acl.SetAccessRuleProtection($True, $False)

    Write-Host "Removing the Access Rules of the folder $folderpath"
    $rules = $acl.access
    ForEach($rule in $rules) {
        $acl.RemoveAccessRule($rule) | Out-Null
    }

    Write-Host "Add Access Rule for BUILTIN\Administrators"
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\Administrators', 'FullControl', 'ContainerInherit, ObjectInherit', 'None','Allow')
    $acl.AddAccessRule($rule)

    Write-Host "Add Access Rule for SYSTEM"
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('SYSTEM', 'FullControl', 'ContainerInherit, ObjectInherit', 'None','Allow')
    $acl.AddAccessRule($rule)

    Write-Host "Add Access Rule for $env:USERNAME"
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule([System.Security.Principal.NTAccount] $env:USERNAME, 'FullControl', 'ContainerInherit, ObjectInherit', 'None','Allow')
    $acl.AddAccessRule($rule)

    Write-Host "Add Access Rule for $serviceuser"
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule([System.Security.Principal.NTAccount] $serviceuser, 'FullControl', 'ContainerInherit, ObjectInherit', 'None','Allow')
    $acl.AddAccessRule($rule)

    Write-Host "Granting Access Rule for $folderpath"
    Set-Acl $folderpath $acl | Out-Null
    if ($silent -eq $false)
    {
	    pause
    }
}
catch{
    Write-Output $PSItem.ToString()
    throw
}