Advertisement
Dennisaa

New-Folder

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