help with the script. Issue when trying to accept multiple values. How do I make it accept more than one value.

I need the script to accept more than one directory and group. Why doesn’t it accepts multiple values?? It doens't produce the report when I do this
[String[]



#DOES NOT WORK. I get empty report.

  param(
    [Parameter(Mandatory = $True)]
    [String[]$Group,
    [ValidateScript({Test-Path -Path $_})]
    [Parameter(Mandatory = $True)]
    [String[]$Directory
)


THE FOLLOWING works.

  param(
    [Parameter(Mandatory = $True)]
    [String]$Group,
    [ValidateScript({Test-Path -Path $_})]
    [Parameter(Mandatory = $True)]
    [String]$Directory
)
#Requires -Version 4


$DataToExport = @()
$Dirs = Get-ChildItem -Path $directory -Recurse -Directory

Foreach($dir in $Dirs)
{
    $Acls = (Get-Acl -Path $dir.FullName).Access

    Foreach($acl in $Acls)
    {
        If($acl.IdentityReference.Value.ToString() -like $Group)
        {
            $props = @{
                "Path" = $dir.FullName
                "IdentityReference" = $acl.IdentityReference.Value.ToString()
                "AccessControlType" = $acl.AccessControlType
                "InheritanceFlags" = $acl.InheritanceFlags
                "PropagationFlags" = $acl.PropagationFlags
                "FileSystemRights" = $acl.FileSystemRights
                "IsInherited" = $acl.IsInherited
            }
            $DataToExport += (New-Object psobject -Property $props)
        }
    }
}

$DataToExport | Export-Csv "PermissionsDomainUsers1.csv" -NoTypeInformation
creative555Asked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
This is how an improved script could look like:
param(
    [Parameter(Mandatory = $True)]
    [String[]] $Group,

    [ValidateScript({Test-Path -Path $_})]
    [Parameter(Mandatory = $True)]
    [String[]] $Directory
) 
#Requires -Version 4

Get-ChildItem -Path $directory -Recurse -Directory |
% {
  $dirName = $_.FullName
  $Acls = (Get-Acl $dirName).Access 
  $(
    foreach ($grp in $group)
    {
      $Acls | ? { $_.IdentityReference.Value.ToString() -like $grp }
    }
  ) |
  % {
    [PsCustomObject] @{
                "Path"              = $dirName
                "IdentityReference" = $_.IdentityReference.Value.ToString()
                "AccessControlType" = $_.AccessControlType
                "InheritanceFlags"  = $_.InheritanceFlags
                "PropagationFlags"  = $_.PropagationFlags
                "FileSystemRights"  = $_.FileSystemRights
                "IsInherited"       = $_.IsInherited
            }
  }
} | Export-Csv "PermissionsDomainUsers1.csv" -NoTypeInformation

Open in new window

It makes use of pipelining, which is the preferred processing method for PowerShell.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Your type needs to be [String[] - the closing square bracket is missing in your code.

For a path array your code works without further changes. But for groups you need to add another loop, as the like operator does only allow for a single search pattern.
0
 
creative555Author Commented:
Thank you so much!!! Great help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.