creative555
asked on
help with the script needed to get folder owners for multiple shares instead of one share
Hi,
I have this script that exports folder owners to the csv file. It only does it for one share. I would like to use import file with multiple shares listed in UNC format (\\servername\share) and to export results for each share into each share's respective file name - share1.csv, share2.csv, etc. Something like this but it is from another script and I would not be able to combine them since the folder owner script is different. Please help. Thank you so much!
and add
Script is below
I have this script that exports folder owners to the csv file. It only does it for one share. I would like to use import file with multiple shares listed in UNC format (\\servername\share) and to export results for each share into each share's respective file name - share1.csv, share2.csv, etc. Something like this but it is from another script and I would not be able to combine them since the folder owner script is different. Please help. Thank you so much!
Get-Content shares.txt | ForEach `
{
$share = ($_ -split "\\")[-1]
and add
| Export-CSV "F:\NTFSReports\$share.csv"
........................Script is below
function Get-FolderOwner {
param(
[string] $Path = "\\NYCFileServer01\sharename",
[int] $RecurseDepth = 1
)
$RecurseDepth--
Get-ChildItem $Path -Directory | ForEach-Object {
# Get-Acl throws terminating errors, so we need to wrap it in
# a ForEach-Object block; included -ErrorAction Stop out of habit
try {
$Owner = $_ | Get-Acl -ErrorAction Stop | select -exp Owner
}
catch {
$Owner = "Error: {0}" -f $_.Exception.Message
}
[PSCustomObject] @{
Path = $_.FullName
Owner = $Owner
}
if ($RecurseDepth -gt 0) {
Get-FolderOwner -Path $_.FullName -RecurseDepth $RecurseDepth
}
}
}
Try
{
Get-FolderOwner \\NYC-Fileserver01\SHarename -RecurseDepth 100 | Export-Csv E:\scripts\NTFS\FolderOwner-Sharename.csv
}
Catch [System.OutOfMemoryException]
{
# Restart-Computer localhost
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
# Send-MailMessage -From ExpensesBot@MyCompany.Com -To WinAdmin@MyCompany.Com -Subject "HR File Read Failed!" -SmtpServer EXCH01.AD.MyCompany.Com -Body "We failed to read file $FailedItem. The error message was $ErrorMessage"
Break
}
Finally
{
$Time=Get-Date
"This script made a read attempt at $Time" | out-file e:\scripts\NTFS\NTFSscriptlog.log -append
}
Hi creative555,
Here's a script based on others that I've written. See if this is what you're looking for.
A few things to note:
1. You need to modify the variables at the top of the script to reflect your environment.
2. I have changed the output file name from "sharename.csv" to "servername_sharename.csv" in case there's any name collision. This can easily be changed back to just the share name.
Here's a script based on others that I've written. See if this is what you're looking for.
$shareList = 'shares.txt' #Enter the path to the share list file
$OutputDirectory = 'C:\output\' #Enter a path if you want to save the CSVs to something besides the working directory.
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
$outCSV #Location to output CSV results
)
$table = New-Object system.Data.DataTable "OwnerInfo"
$col1 = New-Object system.Data.DataColumn Path,([string])
$table.columns.add($col1)
$col2 = New-Object system.Data.DataColumn Owner,([string])
$table.columns.add($col2)
#Get all directories in root directory
$dirs = Get-ChildItem $rootdir -Recurse -Directory
#Get root directory Owner info
$rootacl = Get-Acl $rootdir
$row = $table.NewRow()
$row.Path = $rootacl.path.Replace('Microsoft.PowerShell.Core\FileSystem::','')
$row.Owner = $rootacl.Owner
$table.Rows.Add($row)
#Get Owner for all subdirectories
$diracls = $dirs | Get-Acl
#Loop through permissions
Foreach($acl in $diracls) {
$row = $table.NewRow()
$row.Path = $acl.path.Replace('Microsoft.PowerShell.Core\FileSystem::','')
$row.Owner = $acl.Owner
$table.Rows.Add($row)
}
#Write results to CSV file
$table | Export-Csv $outCSV -NoTypeInformation
#Clean up variables
Remove-Variable table,dirs,diracls,rootacl,acl,dir
}
# Get list of directories to scan
Get-Content $shareList | ForEach{
$shareCSVName = ($_ -split "\\")[-2] +'_'+($_ -split "\\")[-1]+'.csv'
If($OutputDirectory.Length -gt 0){
If($OutputDirectory.EndsWith('\')){
$shareCSVName = $OutputDirectory+$shareCSVName
}else{
$shareCSVName = $OutputDirectory+'\'+$shareCSVName
}
}
Get-FolderOwner -rootdir $_ -outCSV $shareCSVName
}
A few things to note:
1. You need to modify the variables at the top of the script to reflect your environment.
2. I have changed the output file name from "sharename.csv" to "servername_sharename.csv"
ASKER
Oh. excellent. Will test it tonight and let you know if that works!! :)
ASKER
Hello,
Thank you so much. It worked partially. In particular the files with owners that had access denied didn't work. Below are the errors that I received. Thank you so much
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Get-ChildItem : Could not find a part of the path '\\USWESWNE3I60\Installs-T EST\test'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:20 char:13
+ $dirs = Get-ChildItem $rootdir -Recurse -Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~
+ CategoryInfo : ReadError: (\\USWESWNE3I60\Installs-T EST\test:S tring) [Get-Chi
ldItem], DirectoryNotFoundException
+ FullyQualifiedErrorId : DirIOError,Microsoft.Power Shell.Comm ands.GetCh ildItemCom mand
Directory: \\USWESWNE3I60\Installs-TE ST
+ CategoryInfo : NotSpecified: (:) [Get-Acl], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoun dException ,Microsoft .PowerShel l.Command
s.GetAclCommand
Remove-Variable : Cannot find a variable with name 'diracls'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (diracls:String) [Remove-Variable], ItemNotFoundE
xception
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Remove-Variable : Cannot find a variable with name 'acl'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply
an argument that is not null or empty and then try the command again.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:23 char:24
+ $rootacl = Get-Acl $rootdir
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Acl], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Pow erShell.Co mmands.Ge
tAclCommand
You cannot call a method on a null-valued expression.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:26 char:5
+ $row.Path = $rootacl.path.Replace('Mic rosoft.Pow erShell.Co re\FileSys tem::',' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Remove-Variable : Cannot find a variable with name 'rootacl'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (rootacl:String) [Remove-Variable], ItemNotFoundE
xception
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Remove-Variable : Cannot find a variable with name 'acl'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComm
and
Thank you so much. It worked partially. In particular the files with owners that had access denied didn't work. Below are the errors that I received. Thank you so much
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Get-ChildItem : Could not find a part of the path '\\USWESWNE3I60\Installs-T
At C:\NYC\Scripts\NTFS-Permis
:20 char:13
+ $dirs = Get-ChildItem $rootdir -Recurse -Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\\USWESWNE3I60\Installs-T
ldItem], DirectoryNotFoundException
+ FullyQualifiedErrorId : DirIOError,Microsoft.Power
Directory: \\USWESWNE3I60\Installs-TE
+ CategoryInfo : NotSpecified: (:) [Get-Acl], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoun
s.GetAclCommand
Remove-Variable : Cannot find a variable with name 'diracls'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (diracls:String) [Remove-Variable], ItemNotFoundE
xception
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Remove-Variable : Cannot find a variable with name 'acl'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply
an argument that is not null or empty and then try the command again.
At C:\NYC\Scripts\NTFS-Permis
:23 char:24
+ $rootacl = Get-Acl $rootdir
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Acl], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
tAclCommand
You cannot call a method on a null-valued expression.
At C:\NYC\Scripts\NTFS-Permis
:26 char:5
+ $row.Path = $rootacl.path.Replace('Mic
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Remove-Variable : Cannot find a variable with name 'rootacl'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (rootacl:String) [Remove-Variable], ItemNotFoundE
xception
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Remove-Variable : Cannot find a variable with name 'acl'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\NYC\Scripts\NTFS-Permis
:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundExcep
tion
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
and
Looks like the main issue is the directory isn't valid. Here's the relevant error:
Make sure all your shares are valid. Or if you don't know if the shares will be valid or not, we can add a test to the script to check the path before trying to get owner information.
Get-ChildItem : Could not find a part of the path '\\USWESWNE3I60\Installs-TEST\test'
Make sure all your shares are valid. Or if you don't know if the shares will be valid or not, we can add a test to the script to check the path before trying to get owner information.
ASKER
hm. strange. I can access the shares i specified. I have them listed in the shares.txt like this:
\\USWESWNE3I60751\Installs
\\USWESWNE3I60751\Installs -TEST
\\USWESWNE3I60751\Installs
\\USWESWNE3I60751\Installs
ASKER
hm strange. It adds additional Test directory at the end of share name
This is what I have
\\USWESWNE3I60751\Installs -TEST
It adds \\USWESWNE3I60751\Installs -TEST\Test ???
But it also says this:
PS C:\nyc\scripts\NTFS-Permis sionsGetAn dGrantScri pts> .\GET-NTFSpermissionsvOwne rMultipleS hares.ps1
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\nyc\scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1:48 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
Get-ChildItem : Could not find a part of the path '\\USWESWNE3I60751\Install sTEST\test '.
At C:\dhl\scripts\NTFS-Permis sionsGetAn dGrantScri pts\GET-NT FSpermissi onsvOwnerM ultipleSha res.ps1:20 char:13
+ $dirs = Get-ChildItem $rootdir -Recurse -Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~
+ CategoryInfo : ReadError: (\\USWESWNE3I60751\Install sTEST\test :String) [Get-ChildItem], DirectoryNotFou
ndException
+ FullyQualifiedErrorId : DirIOError,Microsoft.Power Shell.Comm ands.GetCh ildItemCom mand
Directory: \\USWESWNE3I60751\Installs TEST
Path Owner Access
---- ----- ------
1 AMER-AD\svc-admt TESTDOMAIN\user1 Allow FullControl...
ActiveDirectory BUILTIN\Administrators TESTDOMAIN\user1 Allow FullControl...
ADPermissionsReporterFree BUILTIN\Administrators TESTDOMAIN\user1 Allow FullControl...
dumpacl BUILTIN\Administrators TESTDOMAIN\user1 Allow FullControl...
This is what I have
\\USWESWNE3I60751\Installs
It adds \\USWESWNE3I60751\Installs
But it also says this:
PS C:\nyc\scripts\NTFS-Permis
Remove-Variable : Cannot find a variable with name 'dir'.
At C:\nyc\scripts\NTFS-Permis
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
Get-ChildItem : Could not find a part of the path '\\USWESWNE3I60751\Install
At C:\dhl\scripts\NTFS-Permis
+ $dirs = Get-ChildItem $rootdir -Recurse -Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\\USWESWNE3I60751\Install
ndException
+ FullyQualifiedErrorId : DirIOError,Microsoft.Power
Directory: \\USWESWNE3I60751\Installs
Path Owner Access
---- ----- ------
1 AMER-AD\svc-admt TESTDOMAIN\user1 Allow FullControl...
ActiveDirectory BUILTIN\Administrators TESTDOMAIN\user1 Allow FullControl...
ADPermissionsReporterFree BUILTIN\Administrators TESTDOMAIN\user1 Allow FullControl...
dumpacl BUILTIN\Administrators TESTDOMAIN\user1 Allow FullControl...
ASKER
l : \\USWESWNE3I60751\Installs TEST\test
nyc\scripts\NTFS-Permissio nsGetAndGr antScripts \GET-NTFSp ermissions vOwnerMult ipleShares .ps1:32 char:24
$diracls = $dirs | Get-Acl
~~~~~~~
CategoryInfo : NotSpecified: (:) [Get-Acl], DirectoryNotFoundException
FullyQualifiedErrorId : System.IO.DirectoryNotFoun dException ,Microsoft .PowerShel l.Commands .GetAclCom mand
-Variable : Cannot find a variable with name 'diracls'.
nyc\scripts\NTFS-Permissio nsGetAndGr antScripts \GET-NTFSp ermissions vOwnerMult ipleShares .ps1:48 char:5
Remove-Variable table,dirs,diracls,rootacl ,acl,dir
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
CategoryInfo : ObjectNotFound: (diracls:String) [Remove-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
-Variable : Cannot find a variable with name 'acl'.
nyc\scripts\NTFS-Permissio nsGetAndGr antScripts \GET-NTFSp ermissions vOwnerMult ipleShares .ps1:48 char:5
Remove-Variable table,dirs,diracls,rootacl ,acl,dir
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
-Variable : Cannot find a variable with name 'dir'.
dhl\scripts\NTFS-Permissio nsGetAndGr antScripts \GET-NTFSp ermissions vOwnerMult ipleShares .ps1:48 char:5
Remove-Variable table,dirs,diracls,rootacl ,acl,dir
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
nyc\scripts\NTFS-Permissio
$diracls = $dirs | Get-Acl
~~~~~~~
CategoryInfo : NotSpecified: (:) [Get-Acl], DirectoryNotFoundException
FullyQualifiedErrorId : System.IO.DirectoryNotFoun
-Variable : Cannot find a variable with name 'diracls'.
nyc\scripts\NTFS-Permissio
Remove-Variable table,dirs,diracls,rootacl
~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (diracls:String) [Remove-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft
-Variable : Cannot find a variable with name 'acl'.
nyc\scripts\NTFS-Permissio
Remove-Variable table,dirs,diracls,rootacl
~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft
-Variable : Cannot find a variable with name 'dir'.
dhl\scripts\NTFS-Permissio
Remove-Variable table,dirs,diracls,rootacl
~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft
ASKER
Here is the script that I am running:
$shareList = 'shares.txt' #Enter the path to the share list file
$OutputDirectory = 'C:\NYC\Scripts\NTFS-Permi ssionsGetA ndGrantScr ipts' #Enter a path if you want to save the CSVs to something besides the working directory.
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
$outCSV #Location to output CSV results
)
$table = New-Object system.Data.DataTable "OwnerInfo"
$col1 = New-Object system.Data.DataColumn Path,([string])
$table.columns.add($col1)
$col2 = New-Object system.Data.DataColumn Owner,([string])
$table.columns.add($col2)
#Get all directories in root directory
$dirs = Get-ChildItem $rootdir -Recurse -Directory
#Get root directory Owner info
$rootacl = Get-Acl $rootdir
$row = $table.NewRow()
$row.Path = $rootacl.path.Replace('Mic rosoft.Pow erShell.Co re\FileSys tem::','')
$row.Owner = $rootacl.Owner
$table.Rows.Add($row)
#Get Owner for all subdirectories
$diracls = $dirs | Get-Acl
#Loop through permissions
Foreach($acl in $diracls) {
$row = $table.NewRow()
$row.Path = $acl.path.Replace('Microso ft.PowerSh ell.Core\F ileSystem: :','')
$row.Owner = $acl.Owner
$table.Rows.Add($row)
}
#Write results to CSV file
$table | Export-Csv $outCSV -NoTypeInformation
#Clean up variables
Remove-Variable table,dirs,diracls,rootacl ,acl,dir
}
# Get list of directories to scan
Get-Content $shareList | ForEach{
$shareCSVName = ($_ -split "\\")[-2] +'_'+($_ -split "\\")[-1]+'.csv'
If($OutputDirectory.Length -gt 0){
If($OutputDirectory.EndsWi th('\')){
$shareCSVName = $OutputDirectory+$shareCSV Name
}else{
$shareCSVName = $OutputDirectory+'\'+$shar eCSVName
}
}
Get-FolderOwner -rootdir $_ -outCSV $shareCSVName
}
$shareList = 'shares.txt' #Enter the path to the share list file
$OutputDirectory = 'C:\NYC\Scripts\NTFS-Permi
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
$outCSV #Location to output CSV results
)
$table = New-Object system.Data.DataTable "OwnerInfo"
$col1 = New-Object system.Data.DataColumn Path,([string])
$table.columns.add($col1)
$col2 = New-Object system.Data.DataColumn Owner,([string])
$table.columns.add($col2)
#Get all directories in root directory
$dirs = Get-ChildItem $rootdir -Recurse -Directory
#Get root directory Owner info
$rootacl = Get-Acl $rootdir
$row = $table.NewRow()
$row.Path = $rootacl.path.Replace('Mic
$row.Owner = $rootacl.Owner
$table.Rows.Add($row)
#Get Owner for all subdirectories
$diracls = $dirs | Get-Acl
#Loop through permissions
Foreach($acl in $diracls) {
$row = $table.NewRow()
$row.Path = $acl.path.Replace('Microso
$row.Owner = $acl.Owner
$table.Rows.Add($row)
}
#Write results to CSV file
$table | Export-Csv $outCSV -NoTypeInformation
#Clean up variables
Remove-Variable table,dirs,diracls,rootacl
}
# Get list of directories to scan
Get-Content $shareList | ForEach{
$shareCSVName = ($_ -split "\\")[-2] +'_'+($_ -split "\\")[-1]+'.csv'
If($OutputDirectory.Length
If($OutputDirectory.EndsWi
$shareCSVName = $OutputDirectory+$shareCSV
}else{
$shareCSVName = $OutputDirectory+'\'+$shar
}
}
Get-FolderOwner -rootdir $_ -outCSV $shareCSVName
}
Hmm... I just tested it and am not getting any errors. It's not adding anything for me. Are you sure that's the script you're running?
ASKER
Hello,
Yes. I am sure
$shareList = 'shares.txt' #Enter the path to the share list file
$OutputDirectory = 'C:\NYC\Scripts\NTFS-Permi ssionsGetA ndGrantScr ipts' #Enter a path if you want to save the CSVs to something besides the working directory.
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
$outCSV #Location to output CSV results
)
$table = New-Object system.Data.DataTable "OwnerInfo"
$col1 = New-Object system.Data.DataColumn Path,([string])
$table.columns.add($col1)
$col2 = New-Object system.Data.DataColumn Owner,([string])
$table.columns.add($col2)
#Get all directories in root directory
$dirs = Get-ChildItem $rootdir -Recurse -Directory
#Get root directory Owner info
$rootacl = Get-Acl $rootdir
$row = $table.NewRow()
$row.Path = $rootacl.path.Replace('Mic rosoft.Pow erShell.Co re\FileSys tem::','')
$row.Owner = $rootacl.Owner
$table.Rows.Add($row)
#Get Owner for all subdirectories
$diracls = $dirs | Get-Acl
#Loop through permissions
Foreach($acl in $diracls) {
$row = $table.NewRow()
$row.Path = $acl.path.Replace('Microso ft.PowerSh ell.Core\F ileSystem: :','')
$row.Owner = $acl.Owner
$table.Rows.Add($row)
}
#Write results to CSV file
$table | Export-Csv $outCSV -NoTypeInformation
#Clean up variables
Remove-Variable table,dirs,diracls,rootacl ,acl,dir
}
# Get list of directories to scan
Get-Content $shareList | ForEach{
$shareCSVName = ($_ -split "\\")[-2] +'_'+($_ -split "\\")[-1]+'.csv'
If($OutputDirectory.Length -gt 0){
If($OutputDirectory.EndsWi th('\')){
$shareCSVName = $OutputDirectory+$shareCSV Name
}else{
$shareCSVName = $OutputDirectory+'\'+$shar eCSVName
}
}
Get-FolderOwner -rootdir $_ -outCSV $shareCSVName
}
Yes. I am sure
$shareList = 'shares.txt' #Enter the path to the share list file
$OutputDirectory = 'C:\NYC\Scripts\NTFS-Permi
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
$outCSV #Location to output CSV results
)
$table = New-Object system.Data.DataTable "OwnerInfo"
$col1 = New-Object system.Data.DataColumn Path,([string])
$table.columns.add($col1)
$col2 = New-Object system.Data.DataColumn Owner,([string])
$table.columns.add($col2)
#Get all directories in root directory
$dirs = Get-ChildItem $rootdir -Recurse -Directory
#Get root directory Owner info
$rootacl = Get-Acl $rootdir
$row = $table.NewRow()
$row.Path = $rootacl.path.Replace('Mic
$row.Owner = $rootacl.Owner
$table.Rows.Add($row)
#Get Owner for all subdirectories
$diracls = $dirs | Get-Acl
#Loop through permissions
Foreach($acl in $diracls) {
$row = $table.NewRow()
$row.Path = $acl.path.Replace('Microso
$row.Owner = $acl.Owner
$table.Rows.Add($row)
}
#Write results to CSV file
$table | Export-Csv $outCSV -NoTypeInformation
#Clean up variables
Remove-Variable table,dirs,diracls,rootacl
}
# Get list of directories to scan
Get-Content $shareList | ForEach{
$shareCSVName = ($_ -split "\\")[-2] +'_'+($_ -split "\\")[-1]+'.csv'
If($OutputDirectory.Length
If($OutputDirectory.EndsWi
$shareCSVName = $OutputDirectory+$shareCSV
}else{
$shareCSVName = $OutputDirectory+'\'+$shar
}
}
Get-FolderOwner -rootdir $_ -outCSV $shareCSVName
}
ASKER
It is adding "test" to the share.....\\USWESWNE3I6075 1\Installs TEST\test
In my input file shares, I have \\USWESWNE3I60751\Installs TEST\ NOT \\USWESWNE3I60751\Installs TEST\test
I get error + FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar
iableCommand
Get-Acl : \\USWESWNE3I60751\Installs TEST\test
At C:\NYC\Scripts\NTFS-Permis sionsGetAn dGrantScri pts\2GET-N TFSpermiss ionsvOwner Multiple
Shares.ps1:32 char:24
+ $diracls = $dirs | Get-Acl
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Acl], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoun dException ,Microsoft .PowerShel
In my input file shares, I have \\USWESWNE3I60751\Installs
I get error + FullyQualifiedErrorId : VariableNotFound,Microsoft
iableCommand
Get-Acl : \\USWESWNE3I60751\Installs
At C:\NYC\Scripts\NTFS-Permis
Shares.ps1:32 char:24
+ $diracls = $dirs | Get-Acl
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Acl], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoun
ASKER
Also,
I didn't put anything for the following : $rootdir variable......Do I need to put anything there?? I thought it suppose to read the shares from shares.txt. I have different shares on different servers that I want to query, For example, \\server01\share1 \\server2\share2
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
I didn't put anything for the following : $rootdir variable......Do I need to put anything there?? I thought it suppose to read the shares from shares.txt. I have different shares on different servers that I want to query, For example, \\server01\share1 \\server2\share2
function Get-FolderOwner{
Param (
$rootdir, #Directory that is the base/root of the scan
The $rootdir variable is in the function and the directory gets passed to it when it's called. So nothing needs to be defined there.
Do the directory and Output look correct when you run the following script?
Do the directory and Output look correct when you run the following script?
$shareList = 'shares.txt'
$OutputDirectory = 'C:\NYC\Scripts\NTFS-PermissionsGetAndGrantScripts'
Get-Content $shareList | ForEach{
$shareCSVName = ($_ -split "\\")[-2] +'_'+($_ -split "\\")[-1]+'.csv'
If($OutputDirectory.Length -gt 0){
If($OutputDirectory.EndsWith('\')){
$shareCSVName = $OutputDirectory+$shareCSVName
}else{
$shareCSVName = $OutputDirectory+'\'+$shareCSVName
}
}
$_
$shareCSVName
}
ASKER
hm. I tried on different server and still getting this error. The script works partially. I do get outputs but not will all permissions:
Remove-Variable : Cannot find a variable with name 'dir'.
At E:\scripts\ntfs\2GET-NTFSp ermissions vOwnerMult ipleShares .ps1:63 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
Remove-Variable : Cannot find a variable with name 'dir'.
At E:\scripts\ntfs\2GET-NTFSp ermissions vOwnerMult ipleShares .ps1:63 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
Remove-Variable : Cannot find a variable with name 'acl'.
At E:\scripts\ntfs\2GET-NTFSp ermissions vOwnerMult ipleShares .ps1:63 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
Remove-Variable : Cannot find a variable with name 'dir'.
At E:\scripts\ntfs\2GET-NTFSp ermissions vOwnerMult ipleShares .ps1:63 char:5
+ Remove-Variable table,dirs,diracls,rootacl ,acl,dir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft .PowerShel l.Commands .RemoveVar iableComma nd
Remove-Variable : Cannot find a variable with name 'dir'.
At E:\scripts\ntfs\2GET-NTFSp
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
Remove-Variable : Cannot find a variable with name 'dir'.
At E:\scripts\ntfs\2GET-NTFSp
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
Remove-Variable : Cannot find a variable with name 'acl'.
At E:\scripts\ntfs\2GET-NTFSp
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (acl:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
Remove-Variable : Cannot find a variable with name 'dir'.
At E:\scripts\ntfs\2GET-NTFSp
+ Remove-Variable table,dirs,diracls,rootacl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dir:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This works now!!! Thank you so much! I was able to get great reports and save a lot of time with this script.
ASKER