Set-AppDirPermissions

  • 14 April 2018
  • Sean Huggans
Description: 

Function can be used in application package/adjustments/shimming scripts to set directory permissions for applications that run as the user which require non-standard modify access to non-user profile (appdata) specific paths. Disclaimer here: You only want to do this if you absolutely have to! In an ideal world, this wouldn't be necessary at all, however, there are many vendors today who still release applications which run as the user but write to Program Files or ProgramData instead of the User's AppData directory. In these cases, it is better to make the adjustment to that directory to allow write access than to give users local admin rights!

Language: 
Powershell
Usage Type: 
Packaging Function
Script Source: 
function Set-AppDirPermissions ($Setting, $DirPath) {
########################################
# Modify Dirrctory Permissions
# Function Date: 19.7.5.1
# Function By: Sean Huggans
########################################
# Usage Note 1: function is dependent on the standard logging function being present
# Example 1 - "Set-AppDirPermissions -Setting "Modify" -DirPath "C:\Program Files\Application X"
    if (!(test-path -Path $DirPath)) {
        new-item -ItemType directory -Path $DirPath -force -erroraction SilentlyContinue
    }
    if (($Setting) -and ($DirPath)) {
        try {
            Log-Action -Message "Setting ""$($Setting)"" permission on ""$($DirPath)""..."
            $InstallDirObject = Get-Item -LiteralPath $DirPath -ErrorAction Stop
            $ACL = $InstallDirObject.GetAccessControl()
            $Rule = New-Object System.Security.AccessControl.FileSystemAccessRule("everyone","$($Setting)","ContainerInherit,Objectinherit","none","Allow")
            $ACL.SetAccessRule($Rule)
            $InstallDirObject.SetAccessControl($ACL)
            Log-Action -Message " - Success!"
            return $true
        } catch {
            Log-Action -Message " - Error!"
            return $false
        }
    } else {
        Log-Action -Message " - Error! (Incomplete Parameters Passed)"
        return $false
    }
}
Note: that all applications posted here are posted for use, both commercial and non-commercial, free of charge, and as such are provided as-is, without warranty of any kind whatsoever. visuaFUSION, FMSCUG or any other program listed here's author are not responsible for any damages or shortcomings that result from usage of any of these applications.
Error | visuaFUSION Systems Solutions Blog

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /mnt/home/visuafus/public_html/bahusa.net/includes/common.inc:2861) in drupal_send_headers() (line 1551 of /mnt/home/visuafus/public_html/bahusa.net/includes/bootstrap.inc).
  • Error: Call to undefined function mail() in DefaultMailSystem->mail() (line 79 of /mnt/home/visuafus/public_html/bahusa.net/modules/system/system.mail.inc).

Error

The website encountered an unexpected error. Please try again later.