gpt4 book ai didi

powershell - 收集文件权限,导出为 CSV,导出 "Access Denied"文件路径

转载 作者:行者123 更新时间:2023-12-03 10:40:15 25 4
gpt4 key购买 nike

我正在收集有关网络驱动器权限的数据,我已经能够获取我想要的数据,但我希望能够更好地处理错误,特别是对于我没有权限的“访问被拒绝”查看文件夹权限。

$PathtoScan = "\\folderxxx\folderyyy"

$GetFileInfo=Get-ChildItem "$PathtoScan" -Recurse -ErrorAction Stop |
Where {$_.Mode -match "d"} |
Get-NTFSAccess | Select-Object Name,FullName,InheritanceEnabled,IsInherited,
InheritedFrom, AccessRights, Account |
Export-Csv C:\Scripts\dump.csv -NoTypeInformation

try {

$GetFileInfo

}
catch [System.UnauthorizedAccessException] {Write-Host "Message:
[$($_.Exception.Message)" | Out-File C:\Scripts\test.txt ]

}

我试图捕获的错误消息:

Get-NTFSAccess : (5) Access is denied: [\\folderxxx\folderyyy]
At C:\Scripts\ACL.ps1:4 char:1
+ Get-NTFSAccess | Select-Object Name,FullName,InheritanceEnabled,IsInh ...

+ CategoryInfo : WriteError: (\\folderxxx\folderyyy) [Get-NTFSAccess], UnauthorizedAccessException
+ FullyQualifiedErrorId : ReadSecurityError,NTFSSecurity.GetAccess

理想情况下,我想将路径导出到我无权查看修复权限的位置。

我使用:$Error[0].exception | Get-Member 以更好地了解我试图捕获的错误:System.UnauthorizedAccessException

有什么想法吗?

谢谢!

最佳答案

这里有很多错误。

首先,您正在执行以下操作:

$GetFileInfo = Get-ChildItem [...] |
[...] |
Export-Csv C:\Scripts\dump.csv -NoTypeInformation

Export-Csv 根本不会将任何对象返回到标准输出。您可以使用 -PassThru 参数覆盖某些执行此操作的 cmdlet,但 Export-Csv 不支持该参数。 $GetFileInfo 始终将是$null。您的脚本结构让我认为您不了解管道和输出的工作原理。当你写下类似这样的内容时:

$x = Get-Item -Path C:\

您没有编写稍后在调用 $x 时执行的命令。这些命令立即执行,并且这些命令的标准输出被分配给变量$x

稍后您似乎在 catch block 中犯了同样的错误:

Write-Host "Message: [$($_.Exception.Message)" | Out-File C:\Scripts\test.txt ]

我不知道它想做什么,但我保证它不会做你认为它会做的事情。同样,Write-HostOut-File 都不将输出发送到标准输出,并且两个 cmdlet 都不支持 -PassThru。看:

PS C:\> Write-Host Hello World | Out-File C:\test.txt
Hello World
PS C:\> Get-Content C:\test.txt
PS C:\>

我所做的只是打印“Hello World”并在 C:\test.txt 中创建一个空文件。 Write-Host 非常具有欺骗性,因为它看起来像是在写入标准输出,但事实并非如此。它直接写入控制台。这就是为什么您经常看到人们说“您可能需要 Write-Output 而不是 Write-Host”,因为 Write-Output 确实写入标准输出。然而,这并不是真正必要的。您通常可以根本不使用 cmdlet 进行输出。

我猜你想要这个:

"Message: [$($_.Exception.Message)]" | Out-File C:\Scripts\test.txt -Append

接下来,您还可以执行此操作来过滤目录:

$GetFileInfo = Get-ChildItem "$PathtoScan" -Recurse -ErrorAction Stop | 
Where {$_.Mode -match "d"} |

您永远不想使用Mode 属性。它极其慢。事实上,ModeGet-ChildItem 在默认输出下速度缓慢的原因。如果您只想要目录,请执行以下操作:

$GetFileInfo = Get-ChildItem "$PathtoScan" -Recurse -Directory -ErrorAction Stop |
Get-NTFSAccess | [...]

或者,在 PowerShell v2 及更早版本中:

$GetFileInfo = Get-ChildItem "$PathtoScan" -Recurse -ErrorAction Stop |
Where-Object { $_.PSIsContainer } |
Get-NTFSAccess | [...]

所以,以上内容让我们更接近:

$PathtoScan = "\\folderxxx\folderyyy"

try {
Get-ChildItem "$PathtoScan" -Recurse -Directory -ErrorAction Stop |
Get-NTFSAccess | Select-Object Name,FullName,InheritanceEnabled,IsInherited,
InheritedFrom, AccessRights, Account |
Export-Csv C:\Scripts\dump.csv -NoTypeInformation
}
catch {
"Message: [$($_.Exception.Message)" | Out-File C:\Scripts\test.txt ]
}

但是,在 Get-ChildItem 抛出第一个错误时,这仍然会完全停止执行。再说一遍,这可能不是您真正想要的。我猜你想要更接近这个的东西:

$PathtoScan = "\\folderxxx\folderyyy"

# Clear the automatic error variable
$Error.Clear()

# Run the commands, but don't stop for any errors encountered and export the results to file
Get-ChildItem "$PathtoScan" -Recurse -Directory -ErrorAction SilentlyContinue |
Get-NTFSAccess | Select-Object Name,FullName,InheritanceEnabled,IsInherited,
InheritedFrom, AccessRights, Account |
Export-Csv C:\Scripts\dump.csv -NoTypeInformation

# If there were errors
if ($Error) {
# Reverse the $Error ArrayList variable to put the oldest events first
$Error.Reverse()

# Write all the errors from the automatic error variable to file
$Error | ForEach-Object {
"Message: [$($_.Exception.Message)]"
} | Out-File C:\Scripts\test.txt # You might want an -Append here
}

关于powershell - 收集文件权限,导出为 CSV,导出 "Access Denied"文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45201082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com