【问题标题】:Auditing Permissions with Powershell使用 Powershell 审核权限
【发布时间】:2020-07-15 13:08:08
【问题描述】:

首先介绍一下背景知识:作为 Scrum 团队的一员,我被要求准备一个 Powershell 脚本,该脚本将整理一份 SMB 共享列表,这些共享已被标记为安全,可以从提供给的 CSV 文件中存档我嵌套在一个文件夹结构中(每个共享等同于一个 CSV 文件,其中包含一个文件列表,但唯一相关的是它们的标题为 SHARENAME.csv),以及每个共享的当前权限,以防万一他们需要日后恢复。

目前,我的第一步有以下内容(很好也很简单):

get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv"

这为我提供了一个 CSV 文件,其中包含单个列中所有 188 个共享的名称。第二步,为每个人导出一个包含权限列表的 CSV 文件,但进展并不顺利。到目前为止,我有以下内容:

$share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName'
ForEach ($share in $share) {
get-smbshareaccess -name $share -cimsession euukpopdfs005
}

显然目前那里没有 export-csv 命令,但基本上我希望脚本使用它调用的相同共享名称,并使用“get-smbshareaccess -name”作为 CSV 文件的文件名。不幸的是,此时我遇到了以下格式的 188 个错误:

get-smbshareaccess : euukpopdfs005: No MSFT_SMBShare objects found with property 'Name' equal to '@{BaseName=Brand}'.  Verify the value of the property and retry.
At line:3 char:1
+ get-smbshareaccess -name $share -cimsession euukpopdfs005
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{BaseName=Brand}:String) [Get-SmbShareAccess], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_Name,Get-SmbShareAccess
    + PSComputerName        : euukpopdfs005

因此,它调用的是 (@{BaseName=Brand}:String),而不是调用 BaseName(例如,在第一种情况下为“Brand”),然后它找不到任何东西。

无论如何我可以整理一下吗?理想情况下,以某种方式适用于 Get-SmbShareAccess 命令和 export-csv 命令,但我不反对根据需要做更多的诡计。

这是我的第一篇文章,如果这篇文章太长、缺乏细节等,我深表歉意。如果您需要其他内容,请告诉我!提前致谢。


建议后的最终(工作)代码:

    get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv"
$share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName'
$share | Format-List
ForEach ($item in $share) {
get-smbshareaccess -name $item.BaseName -cimsession euukpopdfs005
}

【问题讨论】:

  • 我对命令不熟悉,但有一个明显的错误。将ForEach ($A in $A) {更改为ForEach ($item in $A) {,然后将以下行更改为get-smbshareaccess -name $item -cimsession euukpopdfs005
  • 能否提供$A | 的输出?格式列表 * ?看起来 $A 对象不是您认为的那样。也尽量不要太用力地使用变量范围,比如在 foreach($A in $A);你应该根据它们是什么来命名变量,例如$shares = Import-Csv [...] 和 foreach($share in $shares) { ... }。这让每个人都更清楚代码。
  • 感谢@GzehNiert,$A | Format-List * 给我:BaseName : A6$ BaseName : AnnWood BaseName : Apollo 等。我还将对象更改为 $share 以避免混淆。
  • 感谢@Itchydon,我在$A 中将其更改为$item 并在get-smbshareaccess 中调用$item 时遇到同样的错误。
  • @Itchydon,非常感谢!这有效:get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv" $share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName' $share | Format-List ForEach ($item in $share) { get-smbshareaccess -name $item.BaseName -cimsession euukpopdfs005 }

标签: powershell csv


【解决方案1】:

最终的工作代码(谢谢大家!):

get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv"
$share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName'
$share | Format-List
ForEach ($item in $share) {
get-smbshareaccess -name $item.BaseName -cimsession euukpopdfs005
}

【讨论】:

    猜你喜欢
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多