File System - Grant Permissions

Octopus.Script exported 2017-10-26 by mcfozzy belongs to ‘File System’ category.

Grant read, write and modify permissions to folders or files

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Items

Items

A comma seperated list of full paths to the files or folders

Read Users

ReadPermissionsTo

A comma separated list of users to grant read permissions to

Write Users

WritePermissionsTo

A comma separated list of users to grant write permissions to

Modify Users

ModifyPermissionsTo

A comma separated list of users to grant modify permissions to

Script body

Steps based on this template will execute the following PowerShell script.

$itemsParameter = $OctopusParameters['Items']
$readPermissionsTo = $OctopusParameters['ReadPermissionsTo']
$writePermissionsTo = $OctopusParameters['WritePermissionsTo']
$modifyPermissionsTo = $OctopusParameters['ModifyPermissionsTo']

if($readPermissionsTo)
{
    $readUsers = $readPermissionsTo.Split(",")
}

if($writePermissionsTo)
{
    $writeUsers = $writePermissionsTo.Split(",")
}

if($modifyPermissionsTo)
{
    $modifyUsers = $modifyPermissionsTo.Split(",")
}

$items = $itemsParameter.Split(",")
foreach($item in $items) 
{
    # Check path exists
    if(!(Test-Path $item))
    {
        throw "$item does not exist"
    }

    Write-Host "Path: $item"
    # Assign read permissions
    foreach($user in $readUsers)
    {
        Write-Host "  Adding read permissions for $user"
        $acl = (Get-Item $item).GetAccessControl('Access')
        $acl.SetAccessRuleProtection($False, $False)
        $rule = 
            if ($acl -is [System.Security.AccessControl.DirectorySecurity])
                {
                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, "Read", "ContainerInherit, ObjectInherit", "None", "Allow")
                }
                else
                {
                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, "Read", "Allow")
                }
        $acl.AddAccessRule($rule)
        Set-Acl $item $acl
    }

    # Assign write permissions
    foreach($user in $writeUsers)
    {
        Write-Host "  Adding write permissions for $user"
        $acl = (Get-Item $item).GetAccessControl('Access')
        $acl.SetAccessRuleProtection($False, $False)
        $rule = 
            if ($acl -is [System.Security.AccessControl.DirectorySecurity])
                {
                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, "Write", "ContainerInherit, ObjectInherit", "None", "Allow")
                }
                else
                {
                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, "Write", "Allow")
                }
        $acl.AddAccessRule($rule)
        Set-Acl $item $acl
    }

    # Assign modify permissions
    foreach($user in $modifyUsers)
    {
        Write-Host "  Adding modify permissions for $user"
        $acl = (Get-Item $item).GetAccessControl('Access')
        $acl.SetAccessRuleProtection($False, $False)
        $rule = 
            if ($acl -is [System.Security.AccessControl.DirectorySecurity])
                {
                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, "Modify", "ContainerInherit, ObjectInherit", "None", "Allow")
                }
                else
                {
                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, "Modify", "Allow")
                }
        $acl.AddAccessRule($rule)
        Set-Acl $item $acl
    }
}

Write-Host "Complete"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "b77d55ae-7f54-45ab-ba57-7afff97c93a2",
  "Name": "File System - Grant Permissions",
  "Description": "Grant read, write and modify permissions to folders or files",
  "Version": 5,
  "ExportedAt": "2017-10-26T07:11:38.851Z",
  "ActionType": "Octopus.Script",
  "Author": "mcfozzy",
  "Parameters": [
    {
      "Id": "e67e2b43-b1e4-482d-bb5d-e4b6acb2b90f",
      "Name": "Items",
      "Label": "Items",
      "HelpText": "A comma seperated list of full paths to the files or folders",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "b830e92e-ceaa-472e-b1ac-e70a19d9386b",
      "Name": "ReadPermissionsTo",
      "Label": "Read Users",
      "HelpText": "A comma separated list of users to grant read permissions to",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "141f465e-6998-4715-be65-6d5df2604239",
      "Name": "WritePermissionsTo",
      "Label": "Write Users",
      "HelpText": "A comma separated list of users to grant write permissions to",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "c150331e-ea88-4170-8bca-52a8cfd30220",
      "Name": "ModifyPermissionsTo",
      "Label": "Modify Users",
      "HelpText": "A comma separated list of users to grant modify permissions to",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$itemsParameter = $OctopusParameters['Items']\n$readPermissionsTo = $OctopusParameters['ReadPermissionsTo']\n$writePermissionsTo = $OctopusParameters['WritePermissionsTo']\n$modifyPermissionsTo = $OctopusParameters['ModifyPermissionsTo']\n\nif($readPermissionsTo)\n{\n    $readUsers = $readPermissionsTo.Split(\",\")\n}\n\nif($writePermissionsTo)\n{\n    $writeUsers = $writePermissionsTo.Split(\",\")\n}\n\nif($modifyPermissionsTo)\n{\n    $modifyUsers = $modifyPermissionsTo.Split(\",\")\n}\n\n$items = $itemsParameter.Split(\",\")\nforeach($item in $items) \n{\n    # Check path exists\n    if(!(Test-Path $item))\n    {\n        throw \"$item does not exist\"\n    }\n\n    Write-Host \"Path: $item\"\n    # Assign read permissions\n    foreach($user in $readUsers)\n    {\n        Write-Host \"  Adding read permissions for $user\"\n        $acl = (Get-Item $item).GetAccessControl('Access')\n        $acl.SetAccessRuleProtection($False, $False)\n        $rule = \n            if ($acl -is [System.Security.AccessControl.DirectorySecurity])\n                {\n                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, \"Read\", \"ContainerInherit, ObjectInherit\", \"None\", \"Allow\")\n                }\n                else\n                {\n                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, \"Read\", \"Allow\")\n                }\n        $acl.AddAccessRule($rule)\n        Set-Acl $item $acl\n    }\n\n    # Assign write permissions\n    foreach($user in $writeUsers)\n    {\n        Write-Host \"  Adding write permissions for $user\"\n        $acl = (Get-Item $item).GetAccessControl('Access')\n        $acl.SetAccessRuleProtection($False, $False)\n        $rule = \n            if ($acl -is [System.Security.AccessControl.DirectorySecurity])\n                {\n                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, \"Write\", \"ContainerInherit, ObjectInherit\", \"None\", \"Allow\")\n                }\n                else\n                {\n                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, \"Write\", \"Allow\")\n                }\n        $acl.AddAccessRule($rule)\n        Set-Acl $item $acl\n    }\n\n    # Assign modify permissions\n    foreach($user in $modifyUsers)\n    {\n        Write-Host \"  Adding modify permissions for $user\"\n        $acl = (Get-Item $item).GetAccessControl('Access')\n        $acl.SetAccessRuleProtection($False, $False)\n        $rule = \n            if ($acl -is [System.Security.AccessControl.DirectorySecurity])\n                {\n                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, \"Modify\", \"ContainerInherit, ObjectInherit\", \"None\", \"Allow\")\n                }\n                else\n                {\n                    New-Object System.Security.AccessControl.FileSystemAccessRule($user, \"Modify\", \"Allow\")\n                }\n        $acl.AddAccessRule($rule)\n        Set-Acl $item $acl\n    }\n}\n\nWrite-Host \"Complete\"\n",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.FeedId": null,
    "Octopus.Action.Package.PackageId": null
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-grant-permissions.json",
  "Website": "/step-templates/b77d55ae-7f54-45ab-ba57-7afff97c93a2",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKhQTFRF/////78A/6oAVVVV/++//+q/gICA1dXV/79A/9R///ff/8MQ/9dg/8cg//vv/68A/+ef//PP//rv/7Ug/68Q/+Sv/8sw/9tw/9NQ/89w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