[英]PowerShell append objects to variable list
Fairly new to PowerShell and wanting to learn how to append objects to a variable list.对 PowerShell 相当陌生,想了解如何将 append 对象添加到变量列表中。 Below is the error message:
以下是错误消息:
Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
At C:\Users\Username\Desktop\Sandbox\New folder\BoxProjectFiles.ps1:117 char:4
+ $MechDWGFile += $file
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
and code:和代码:
LogWrite "`n-------------Mechanical Drawing(s)------------"
foreach ($file in $MechDWGList)
{
# Where the file name contains one of these filters
foreach($filter in $MechDWGFilterList)
{
if($file.Name -like $filter)
{
$MechDWGFile += $file # this is where the error is happening, so I know it is probably something simple
LogWrite $file.FullName
}
}
}
PowerShell 5.1 and Windows 10 OS is being used.正在使用 PowerShell 5.1 和 Windows 10 操作系统。
Could someone help me understand what's wrong with my syntax?有人可以帮我理解我的语法有什么问题吗?
Based on the error message, $MechDWGFile
already contains a single [FileInfo]
object - the type of object returned by Get-Item
or Get-ChildItem
.根据错误消息,
$MechDWGFile
已经包含一个[FileInfo]
object - Get-Item
或Get-ChildItem
返回的 object 的类型。
The +=
operator is overloaded in PowerShell, meaning its behavior depends on the type of object you have on the left-hand side - in this case $MechDWGFile
which contains a [FileInfo]
object. +=
运算符在 PowerShell 中被重载,这意味着它的行为取决于左侧的 object 的类型- 在这种情况下$MechDWGFile
包含[FileInfo]
ZA8CFDE6331BD59EB2AC96F8911C4B666
$file
also contains such an object, but [FileInfo] + [FileInfo]
doesn't make any sense, which is why you see the error. $file
也包含这样一个 object,但是[FileInfo] + [FileInfo]
没有任何意义,这就是您看到错误的原因。
To make the +=
operator work, you need to create an array with the @()
array subexpression operator:要使
+=
运算符起作用,您需要使用@()
数组子表达式运算符创建一个数组:
$MechDWGFile = @()
# ...
# This now works
$MechDWGFile += $file
If you've initialized $MechDWGFile
with output from Get-Item
or Get-ChildItem
, simply nest the existing pipeline in @()
:如果您使用
Get-Item
或Get-ChildItem
中的 output 初始化$MechDWGFile
,只需将现有管道嵌套在@()
中:
# `Get-ChildItem |Select -First 1` will only output 1 object,
# but the @(...) makes PowerShell treat it as a 1-item array
# which in turn allows you to use `+=` later
$MechDWGFile = @(Get-ChildItem -Recurse -File |Select -First 1)
One big caveat about arrays in PowerShell—they are immutable .关于 PowerShell 中的 arrays 的一大警告——它们是不可变的。
Although you can "add" elements to an array in PowerShell using the overloaded +=
operator, what it essentially does is creates a new array with the elements of the first operand and the second operand combined.尽管您可以使用重载的
+=
运算符将元素“添加”到 PowerShell 中的数组中,但它实际上所做的是创建一个新数组,其中包含第一个操作数和第二个操作数的元素组合。
This may be more of a personal preference, but when I plan on looping over a bunch of items to populate an array, I use an [ArrayList]
instead.这可能更多是个人喜好,但是当我计划循环一堆项目以填充数组时,我使用
[ArrayList]
代替。 ArrayLists are mutable by design. ArrayList 在设计上是可变的。 Why this isn't the default in PowerShell, I have no idea.
为什么这不是 PowerShell 中的默认设置,我不知道。
Adding a new element to an existing array:向现有数组添加新元素:
$Array1 = Get-ChildItem -Path 'C:\'
$Array2 = @()
# Add only the directories to our second array
foreach ($Item in $Array1) {
if ($Item.PSIsContainer) {
$Array2 += $Item # This creates a new array every time
}
}
$Array2
Adding a new element to an existing [ArrayList]
:向现有
[ArrayList]
添加新元素:
$Array1 = Get-ChildItem -Path 'C:\'
$Array2 = [System.Collections.ArrayList]@()
# Add only the directories to our ArrayList
foreach ($Item in $Array1) {
if ($Item.PSIsContainer) {
$null = $Array2.Add($Item) # This does not create a new object each time.
# The $null= prepend is because [ArrayList].Add() will output the ID of the
# last element added, so we assign that to null, keeping our console clean.
}
}
$Array2
Side note: Like Mathias has stated, your variable $MechDWGFile
may not have been initialized as an array, which is why the +=
overload operator didn't work and instead threw an error.旁注:就像 Mathias 所说的,您的变量
$MechDWGFile
可能没有被初始化为数组,这就是+=
重载运算符不起作用而是引发错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.