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

查看:62
本文介绍了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] s的数组使用 + = 来迭代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 = @()

但是,使用 + = 添加到数组的效率很低,因为实际上每次都必须创建一个 new 数组实例,因为数组是不可变数据结构.也就是说,每次使用 + = 时,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).

请注意,您可以类似地将用作 if do / while /语句转换为表达式.

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

但是,在所有情况下,从PowerShell 7.0开始,这些语句只能用作 的表达式;遗憾的是,将它们用作管道的第一部分或将它们嵌入较大的表达式中并不能奏效-请参见此GitHub问题.

In all cases, however, as of PowerShell 7.0, 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 this GitHub issue.

关于您尝试过的事情:

$ outputArray + = $ objResult

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

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天全站免登陆