creative555
asked on
Trying to make test-path validate on mutliple remote computers and export the log. Only works on the local computer.
Hello
I am having an issue with making “test-path” work and export some kind of log either to the directory or to the event log…..
So the challenge is to make this work on the remote computers. It only worked for the local computer.
See attached script.
I am having an issue with making “test-path” work and export some kind of log either to the directory or to the event log…..
So the challenge is to make this work on the remote computers. It only worked for the local computer.
See attached script.
There is no attached script..
ASKER
ops sorry. here is the script. I am trying to add error handling using TRY and catch. However it broke the script. HEre is the bad one where I put Try and Catch
Function Get-xACL {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $True)]
[String[]$Group,
[Parameter(Mandatory = $True)]
[String[]$Directory,
[Parameter(ValueFromPipeli neByProper tyName = $True,
ValueFromPipeline = $True)]
[String[] $ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $false)]
[String] $FileName = "C:\temp\NTFSPermissions.c sv",
#Switch to turn on Error logging
[Switch]$ErrorLog,
[String]$LogFile = 'c:\temp\errorlog.txt'
)
Begin {
$Report = @()
Clear-Variable ACLs -ErrorAction SilentlyContinue
New-EventLog –LogName Application –Source “Get-xAcl” -ErrorAction SilentlyContinue
$DumpAcls = {
param(
$Directory,
$Group
)
$DataToExport = @()
$Dirs = @()
Foreach ($d in $Directory) {
if (Test-Path $d) {
$Dirs += $d -AS [System.IO.DirectoryInfo]
$Dirs += Get-ChildItem -Path $d -Recurse -Directory
}
else {
# possible directory didn't exist. Write message to application log
#Write-EventLog -LogName Application -Source "Get-xACL" -EventId 2202 -EntryType Error -Message "unable to find directory: $d"
}
}
Foreach ($dir in $Dirs) {
$Acls = (Get-Acl -Path $dir.FullName).Access
Foreach ($acl in $Acls) {
Try {
If ($acl.IdentityReference.Va lue.ToStri ng() -in $Group) {
$props = @{
"Path" = $dir.FullName
"IdentityReference" = $acl.IdentityReference.Val ue.ToStrin g()
"AccessControlType" = $acl.AccessControlType
"InheritanceFlags" = $acl.InheritanceFlags
"PropagationFlags" = $acl.PropagationFlags
"FileSystemRights" = $acl.FileSystemRights
"IsInherited" = $acl.IsInherited
}
}# end if
$DataToExport += (New-Object psobject -Property $props)
}
catch {write-warning "Error blah blah $Acls"}
}# end foreach
}# end foreach
return $DataToExport
}# end DumpAcls
}# end Begin
Process {
Foreach ($Computer in $ComputerName) {
try {
If ($Computer -ne $env:COMPUTERNAME) {
Write-Verbose "Remote Computer - $Computer"
$ACLs = Invoke-Command -ComputerName $Computer -ScriptBlock $DumpAcls -ArgumentList $Directory, $Group
}
Else {
Write-Verbose "Local computer - $Computer"
$ACLs = Invoke-Command $DumpAcls -ArgumentList $Directory, $Group
}
$Report += $ACLs
}
catch {
Write-Warning "You made a boo-boo with computer $computername "
}
}
}
End {
$Report | Export-Csv $FileName -NoTypeInformation
}
}
Function Get-xACL {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $True)]
[String[]$Group,
[Parameter(Mandatory = $True)]
[String[]$Directory,
[Parameter(ValueFromPipeli
ValueFromPipeline = $True)]
[String[] $ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $false)]
[String] $FileName = "C:\temp\NTFSPermissions.c
#Switch to turn on Error logging
[Switch]$ErrorLog,
[String]$LogFile = 'c:\temp\errorlog.txt'
)
Begin {
$Report = @()
Clear-Variable ACLs -ErrorAction SilentlyContinue
New-EventLog –LogName Application –Source “Get-xAcl” -ErrorAction SilentlyContinue
$DumpAcls = {
param(
$Directory,
$Group
)
$DataToExport = @()
$Dirs = @()
Foreach ($d in $Directory) {
if (Test-Path $d) {
$Dirs += $d -AS [System.IO.DirectoryInfo]
$Dirs += Get-ChildItem -Path $d -Recurse -Directory
}
else {
# possible directory didn't exist. Write message to application log
#Write-EventLog -LogName Application -Source "Get-xACL" -EventId 2202 -EntryType Error -Message "unable to find directory: $d"
}
}
Foreach ($dir in $Dirs) {
$Acls = (Get-Acl -Path $dir.FullName).Access
Foreach ($acl in $Acls) {
Try {
If ($acl.IdentityReference.Va
$props = @{
"Path" = $dir.FullName
"IdentityReference" = $acl.IdentityReference.Val
"AccessControlType" = $acl.AccessControlType
"InheritanceFlags" = $acl.InheritanceFlags
"PropagationFlags" = $acl.PropagationFlags
"FileSystemRights" = $acl.FileSystemRights
"IsInherited" = $acl.IsInherited
}
}# end if
$DataToExport += (New-Object psobject -Property $props)
}
catch {write-warning "Error blah blah $Acls"}
}# end foreach
}# end foreach
return $DataToExport
}# end DumpAcls
}# end Begin
Process {
Foreach ($Computer in $ComputerName) {
try {
If ($Computer -ne $env:COMPUTERNAME) {
Write-Verbose "Remote Computer - $Computer"
$ACLs = Invoke-Command -ComputerName $Computer -ScriptBlock $DumpAcls -ArgumentList $Directory, $Group
}
Else {
Write-Verbose "Local computer - $Computer"
$ACLs = Invoke-Command $DumpAcls -ArgumentList $Directory, $Group
}
$Report += $ACLs
}
catch {
Write-Warning "You made a boo-boo with computer $computername "
}
}
}
End {
$Report | Export-Csv $FileName -NoTypeInformation
}
}
ASKER
the script now works with the remote computers!! However, I was trying to add error handling and it broke it. If you remove Try and catch, then it will work fine. Please let me know what is wrong with Try and Catch.
thank you so much!
thank you so much!
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.