Advertisement
Dennisaa

New-Folder

Mar 9th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Synopsis
  3.    For 1 or more computers, for a given folder root, create the folders, and the subfolders in the script here
  4. .Description
  5.    Also see http://technet.microsoft.com/en-us/library/ff730951.aspx
  6.    $session = New-PSSession -ComputerName "jan-pc"
  7.    Enter-PSSession -Session $session
  8.    Get-ACL "C:\sandbox\PowerShell\artefacts\subFolder" | Format-List
  9.    Which returns eg
  10.    Path   : Microsoft.PowerShell.Core\FileSystem::C:\sandbox\PowerShell\artefacts\subFolder
  11.         Owner  : BUILTIN\Administrators
  12.         Group  : Jan-pc\denni_000
  13.         Access : BUILTIN\Administrators Allow  FullControl
  14.                  NT AUTHORITY\SYSTEM Allow  FullControl
  15.                 BUILTIN\Users Allow  ReadAndExecute, Synchronize
  16.                 NT AUTHORITY\Authenticated Users Allow  Modify, Synchronize
  17.                 NT AUTHORITY\Authenticated Users Allow  -536805
  18.         Audit  :
  19.         Sddl   : O:BAG:S-1-5-(snip)OICIID;0x1200a9;;;BU)(A;ID;0x1301bf;;;AU)(A;OICIIOID;SDGXGWGR;;;AU)
  20.  
  21. .Example
  22.    New-Folder -ComputerName "dennis-pc","Jan-pc","emma-pc" -FolderRoot "\Test3"
  23. #>
  24. function New-Folder
  25. {
  26.     Param (
  27.         [Parameter(Mandatory=$true, Position=0)]
  28.         [string[]]
  29.         $ComputerName,
  30.         [Parameter(Mandatory=$true, Position=1)]
  31.         [string]
  32.         $FolderRoot = "C:\TestFolder1",
  33.         [Parameter(Mandatory=$false, Position=2)]
  34.         [string]
  35.         $UserName = "BUILTIN\users"
  36.     )
  37.     Begin {
  38.           $fileRights = [System.Security.AccessControl.FileSystemRights]"FullControl"
  39.           $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit
  40.           $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly
  41.           $objType =[System.Security.AccessControl.AccessControlType]::Allow
  42.     }
  43.     Process {
  44.         foreach ($computer in $ComputerName) {
  45.           "Processing $computer..."
  46.           Invoke-Command -ComputerName $computer -Credential $cred -ScriptBlock {
  47.                 param($FolderRoot1, $UserName1, $fileRights1, $InheritanceFlag1, $PropagationFlag1, $objType1)
  48.                 if (!(Test-Path -Path $FolderRoot1)) {
  49.                     "Creating folders in $FolderRoot1..."
  50.                     New-Item -ItemType directory -Path $FolderRoot1
  51.                     New-Item -ItemType directory -Path $FolderRoot1/Backup
  52.                     New-Item -ItemType directory -Path $FolderRoot1/Error
  53.                 }
  54.  
  55.                 $objUser = New-Object System.Security.Principal.NTAccount($UserName1)
  56.                 $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $fileRights1, $InheritanceFlag1, $PropagationFlag1, $objType1)
  57.                 $objACL = Get-ACL $FolderRoot1
  58.                 $objACL.AddAccessRule($objACE)
  59.                 Set-ACL $FolderRoot1 $objACL
  60.             } -ArgumentList ($FolderRoot, $UserName, $fileRights, $InheritanceFlag, $PropagationFlag, $objType)
  61.          }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement