Reputation: 51
I've been working with a script that does almost exactly what I need it to do. It will take a drive share path, grab the access control list, and export it to a .csv file for later editing. My problem is that I can't figure out how to pass multiple drive path values to the script. I would like to use a .txt or .csv file to feed the shares to the script, and let it run overnight. The origin site says to assign a variable at the command line, and use this command:
$allServersVariable -FilePath C:\Scripts\MyPermissionsScript.ps1 -ArgumentList "E:\list" | Export-Csv C:\Scripts\ScanResults.csv –NoTypeInformation
I cannot however get this command to work. I can however do the following, and process one share at a time:
.\MyPermissionsScript.ps1 \\\192.168.1.1\SharedFiles | Export-Csv C:\Scripts\ScanResults.csv -NoTypeInformation
Here is the script:
function Get-PathPermissions {
param (
[Parameter(Mandatory=$true)]
[System.String]${Path}
)
begin {
$root = Get-Item $Path
($root | get-acl).Access | Add-Member -MemberType NoteProperty -Name "Path" -Value $($root.fullname).ToString() -PassThru
}
process {
$containers = Get-ChildItem -path $Path -recurse | ? {$_.psIscontainer -eq $true}
if ($containers -eq $null) {break}
foreach ($container in $containers)
{
(Get-ACL $container.fullname).Access | format-list -property Path, AccessToString
}
}
}
Get-PathPermissions $args[0]
This has been driving me crazy. Any help is greatly appreciated!
Upvotes: 0
Views: 950
Reputation: 3163
The simplest way to do this would be:
Get-Content C:\PathToInputFile.txt | ForEach-Object { .\MyPermissionsScript.ps1 $_ } | Export-Csv C:\Scripts\ScanResults.csv -NoTypeInformation
Get-Content reads in the contents of a file as an array. It will then call your script for each element of the array, and all of the output will be exported to the file.
However, if you want to export to CSV, I'd remove the Format-List from your function. It turns the results into strings, rather than objects, while Export-Csv expects objects. If you just want to select those properties, use Select-Object instead of Format-List.
Upvotes: 2