We help IT Professionals succeed at work.

Powershell Script Modification

Joseph Moody
Joseph Moody asked
on
Medium Priority
932 Views
Last Modified: 2012-06-27
I have a script below that I can't figure out how to modify. I am wanting to create two additional folders (Documents and Tests). That part works.

I am wanting to make it so that the Documents folder gives everyone read/execute access.
I am wanting to make the Tests folder have everyone full control.

How can I do this within the script below?
$BasePath = "\\arwen\gahomework"  
$DefaultFolders = "Block 1", "Block 2", "Block 3", "Block 4", "Tests", "Documents"  
  
Get-QADGroupMember "Test Group" | ForEach-Object {  
  $NewFolder = New-Item "$BasePath\$($_.LastName)-$($_.FirstName[0])" -ItemType Directory  
  $DefaultFolders | ForEach-Object {  
    New-Item "$($NewFolder.FullName)\$_" -ItemType Directory  
  }  
}

Open in new window

Comment
Watch Question

Jamie McKillopIT Director
CERTIFIED EXPERT

Commented:
Hello,

Try this.

JJ
$BasePath = "\\arwen\gahomework"  
$DefaultFolders = "Block 1", "Block 2", "Block 3", "Block 4", "Tests", "Documents"  
  
Get-QADGroupMember "Test Group" | ForEach-Object {  
  $NewFolder = New-Item "$BasePath\$($_.LastName)-$($_.FirstName[0])" -ItemType Directory
  $DefaultFolders | ForEach-Object {  
    New-Item "$($NewFolder.FullName)\$_" -ItemType Directory  
    If ($_ -eq "Tests") {    
       $ACL = Get-Acl "$($NewFolder.FullName)\$_"
       $permission = "Everyone","FullControl","Allow"
       $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
       $acl.SetAccessRule($accessRule)
       $acl | Set-Acl "$($NewFolder.FullName)\$_"
    }
  }  
  $ACL = Get-Acl "$BasePath\$($_.LastName)-$($_.FirstName[0])"
  $permission = "Everyone","ReadAndExecute","Allow"
  $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
  $acl.SetAccessRule($accessRule)
  $acl | Set-Acl "$BasePath\$($_.LastName)-$($_.FirstName[0])"  
}

Open in new window

Joseph MoodyBlogger and wearer of all hats.
CERTIFIED EXPERT

Author

Commented:
It set the permission perfectly on the sub folder named Tests. But it didn't set it for the sub folder named Documents.

Could you fix that?
IT Director
CERTIFIED EXPERT
Commented:
Sorry, I didn't notice you wanted Documents as well.
$BasePath = "\\arwen\gahomework"  
$DefaultFolders = "Block 1", "Block 2", "Block 3", "Block 4", "Tests", "Documents"  
  
Get-QADGroupMember "Test Group" | ForEach-Object {  
  $NewFolder = New-Item "$BasePath\$($_.LastName)-$($_.FirstName[0])" -ItemType Directory
  $DefaultFolders | ForEach-Object {  
    New-Item "$($NewFolder.FullName)\$_" -ItemType Directory  
    If ($_ -eq "Tests") {    
       $ACL = Get-Acl "$($NewFolder.FullName)\$_"
       $permission = "Everyone","FullControl","Allow"
       $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
       $acl.SetAccessRule($accessRule)
       $acl | Set-Acl "$($NewFolder.FullName)\$_"
    }
    If ($_ -eq "Documents") {    
       $ACL = Get-Acl "$($NewFolder.FullName)\$_"
       $permission = "Everyone","FullControl","Allow"
       $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
       $acl.SetAccessRule($accessRule)
       $acl | Set-Acl "$($NewFolder.FullName)\$_"
    }

  }  
  $ACL = Get-Acl "$BasePath\$($_.LastName)-$($_.FirstName[0])"
  $permission = "Everyone","ReadAndExecute","Allow"
  $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
  $acl.SetAccessRule($accessRule)
  $acl | Set-Acl "$BasePath\$($_.LastName)-$($_.FirstName[0])"  
}

Open in new window

Joseph MoodyBlogger and wearer of all hats.
CERTIFIED EXPERT

Author

Commented:
Awesome!
Joseph MoodyBlogger and wearer of all hats.
CERTIFIED EXPERT

Author

Commented:
One last thing - how can the script be set up to create the Tests and Documents folder if the root folder already exists. I get the following errors.
New-Item : Item with specified name \\arwen\gahomework\Moody-J already exists.
At line:2 char:24
+   $NewFolder = New-Item <<<<  "$BasePath\$($_.LastName)-$($_.FirstName[0])" -ItemType Directory
    + CategoryInfo          : ResourceExists: (\\arwen\gahomework\Moody-J:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Access to the path 'Block 1' is denied.
At line:4 char:13
+     New-Item <<<<  "$($NewFolder.FullName)\$_" -ItemType Directory
    + CategoryInfo          : PermissionDenied: (C:\Block 1:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Access to the path 'Block 2' is denied.
At line:4 char:13
+     New-Item <<<<  "$($NewFolder.FullName)\$_" -ItemType Directory
    + CategoryInfo          : PermissionDenied: (C:\Block 2:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Access to the path 'Block 3' is denied.
At line:4 char:13
+     New-Item <<<<  "$($NewFolder.FullName)\$_" -ItemType Directory
    + CategoryInfo          : PermissionDenied: (C:\Block 3:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Access to the path 'Block 4' is denied.
At line:4 char:13
+     New-Item <<<<  "$($NewFolder.FullName)\$_" -ItemType Directory
    + CategoryInfo          : PermissionDenied: (C:\Block 4:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Access to the path 'Tests' is denied.
At line:4 char:13
+     New-Item <<<<  "$($NewFolder.FullName)\$_" -ItemType Directory
    + CategoryInfo          : PermissionDenied: (C:\Tests:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

Get-Acl : Cannot find path '\Tests' because it does not exist.
At line:6 char:22
+        $ACL = Get-Acl <<<<  "$($NewFolder.FullName)\$_"
    + CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
    + FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand

You cannot call a method on a null-valued expression.
At line:9 char:26
+        $acl.SetAccessRule <<<< ($accessRule)
    + CategoryInfo          : InvalidOperation: (SetAccessRule:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.
At line:10 char:22
+        $acl | Set-Acl <<<<  "$($NewFolder.FullName)\$_"
    + CategoryInfo          : InvalidData: (:) [Set-Acl], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclComm
   nd

New-Item : Access to the path 'Documents' is denied.
At line:4 char:13
+     New-Item <<<<  "$($NewFolder.FullName)\$_" -ItemType Directory
    + CategoryInfo          : PermissionDenied: (C:\Documents:String) [New-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand

Get-Acl : Cannot find path '\Documents' because it does not exist.
At line:13 char:22
+        $ACL = Get-Acl <<<<  "$($NewFolder.FullName)\$_"
    + CategoryInfo          : ObjectNotFound: (:) [Get-Acl], ItemNotFoundException
    + FullyQualifiedErrorId : GetAcl_PathNotFound_Exception,Microsoft.PowerShell.Commands.GetAclCommand

You cannot call a method on a null-valued expression.
At line:16 char:26
+        $acl.SetAccessRule <<<< ($accessRule)
    + CategoryInfo          : InvalidOperation: (SetAccessRule:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.
At line:17 char:22
+        $acl | Set-Acl <<<<  "$($NewFolder.FullName)\$_"
    + CategoryInfo          : InvalidData: (:) [Set-Acl], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclComm
   nd

Open in new window

Jamie McKillopIT Director
CERTIFIED EXPERT

Commented:
Try this.
$BasePath = "\\arwen\gahomework"  
$DefaultFolders = "Block 1", "Block 2", "Block 3", "Block 4", "Tests", "Documents"  
  
Get-QADGroupMember "Test Group" | ForEach-Object {  
  $Path = "$BasePath\$($_.LastName)-$($_.FirstName[0])"
  If ((Test-Path $Path) -eq $False) {
    New-Item "$BasePath\$($_.LastName)-$($_.FirstName[0])" -ItemType Directory
  }
  $NewFolder = "$BasePath\$($_.LastName)-$($_.FirstName[0])"
  $DefaultFolders | ForEach-Object {  
    New-Item "$NewFolder\$_" -ItemType Directory  
    If ($_ -eq "Tests") {    
       $ACL = Get-Acl "$NewFolder\$_"
       $permission = "Everyone","FullControl","Allow"
       $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
       $acl.SetAccessRule($accessRule)
       $acl | Set-Acl "$NewFolder\$_"
    }
    If ($_ -eq "Documents") {    
       $ACL = Get-Acl "$NewFolder\$_"
       $permission = "Everyone","FullControl","Allow"
       $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
       $acl.SetAccessRule($accessRule)
       $acl | Set-Acl "$NewFolder\$_"
    }

  }  
  $ACL = Get-Acl "$BasePath\$($_.LastName)-$($_.FirstName[0])"
  $permission = "Everyone","ReadAndExecute","Allow"
  $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
  $acl.SetAccessRule($accessRule)
  $acl | Set-Acl "$BasePath\$($_.LastName)-$($_.FirstName[0])"  
}

Open in new window

Joseph MoodyBlogger and wearer of all hats.
CERTIFIED EXPERT

Author

Commented:
Thank you so much!