Powershell:将 pracl 命令的输出管道传输到数组 [英] Powershell: Piping output of pracl command to array

查看:35
本文介绍了Powershell:将 pracl 命令的输出管道传输到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pracl 是一个 sysinternal 命令,可用于列出目录的 ACL.我有一个共享列表,我想创建一个 csv 文件,以便对于每个 ACL 条目,我希望在一列中共享路径并在下一列中共享权限.我试图通过使用以下代码来做到这一点

pracl is a sysinternal command that can be used to list the ACLs of a directory. I have a list of shares and I want to create a csv file such that for each ACL entry, I want the share path in one column and share permission in the next. I was trying to do that by using the following code

$inputfile = "share.txt"

$outputFile = "out.csv"

foreach( $path in Get-Content $inputfile)

{

$results=.\pracl.exe $path

      {

foreach ($result in $results) {write-host $path,$line}



      }

$objResult = [pscustomobject]@{

Path = $Path

Permission = $line

}

$outputArray += $objResult

    $objresult

}

$outputArray | Export-Csv -Path $outputfile -NoTypeInformation

由于以下错误而失败:-

It failed with the following error :-

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

At C:\Users\re07393\1\sample.ps1:14 char:1

+ $outputArray += $objResult

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

有什么建议吗?

推荐答案

您正在尝试创建 [pscustomobject]arrays 在您的 $outputArray 变量中使用+=迭代,但您没有初始化 $outputArray 作为数组 - 请参阅底部部分以了解结果行为的解释.

You're trying to create an array of [pscustomobject]s in your $outputArray variable iteratively, using +=, but you're not initializing $outputArray as an array - see the bottom section for an explanation of the resulting behavior.

因此,解决您的问题的直接方法就是这样做:

Thus, the immediate solution to your problem is to do just that:

# Do this before your `foreach` loop, then `+=` will work for appending elements.
$outputArray = @()

然而,使用+=添加到数组是低效的,因为实际上每次都必须创建一个数组实例,因为数组是不可变数据结构.也就是说,每次使用 += 时,PowerShell 都会在幕后创建一个新的数组实例,将现有元素和新元素复制到该实例中.

However, using += to add to arrays is inefficient, because in reality a new array instance must be created every time, because arrays are immutable data structures. That is, every time += is used, PowerShell creates a new array instance behind the scenes to which the existing elements as well as the new element are copied.

一种更简单、更有效的方法是让 PowerShell 为您创建一个数组,方法是使用 foreach 循环作为 表达式 并将其分配给一个变量作为一个整体:也就是说,在循环的每次迭代中输出的任何内容都会被 PowerShell 自动收集:

A simpler and much more efficient approach is to let PowerShell create an array for you, by using the foreach loop as an expression and assigning it to a variable as a whole: That is, whatever is output in every iteration of the loop is automatically collected by PowerShell:

一个简化的例子:

# Create an array of 10 custom objects
[array] $outputArray = foreach ($i in 1..10) {
   # Create and implicitly output a custom object in each iteration.
   [pscustomobject] @{
     Number = $i
   }
}

注意$outputArray左边的类型约束[array]的使用,它确保变量值总是一个数组,即使循环恰好产生 一个 输出对象(在这种情况下,PowerShell 只会存储该对象本身,而不是将其包装在数组中).

Note the use of type constraint [array] to the left of $outputArray, which ensures that the variable value is always an array, even if the loop happens to produce just one output object (in which case PowerShell would otherwise just store that object itself, and not wrap it in an array).

注意你可以类似地使用forifdo/while/switch 语句作为表达式.

Note that you can similarly use for, if, do / while / switch statements as expressions.

然而,在所有情况下,这些语句只能作为表达式本身;遗憾的是,将它们用作管道的第一段将它们嵌入到更大的表达式中不起作用 - 请参阅GitHub 问题 #6817.

In all cases, however, these statements can only serve as expressions by themselves; regrettably, using them as the first segment of a pipeline or embedding them in larger expressions does not work - see GitHub issue #6817.

至于你尝试了什么:

$outputArray += $objResult

由于您没有在循环之前初始化 $outputArray,变量是在循环的第一次迭代中隐式创建的:

Since you didn't initialize $outputArray before the loop, the variable is implicitly created in the loop's first iteration:

如果 LHS 变量还不存在,+= 实际上与 = 相同:也就是说,RHS 按原样存储在 LHS 变量中,以便 $outputArray 现在包含一个 [pscustomobject] 实例.

If the LHS variable doesn't exist yet, += is effectively the same as =: that is, the RHS is stored as-is in the LHS variable, so that $outputArray now contains a [pscustomobject] instance.

第二次迭代中,因为 $outputArray 现在有一个值,+= 现在尝试执行适合类型的 + 操作(例如数字的数字加法,字符串的连接),但没有为 类型定义 + (op_Addition()) 操作[pscustomobject],因此操作失败并显示您看到的错误消息.

In the second iteration, because $outputArray now has a value, += now tries to perform a type-appropriate + operation (such as numeric addition for numbers, and concatenation for strings), but no + (op_Addition()) operation is defined for type [pscustomobject], so the operation fails with the error message you saw.

这篇关于Powershell:将 pracl 命令的输出管道传输到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆