繁体   English   中英

Powershell:将数据从阵列中取出并放入新阵列中

[英]Powershell: get data out of array and put it into into new array

我正在尝试使用以下命令从数组中获取数据:

$newarray = $current_ds2_data -match $codenumber

在这种情况下,$ current_ds2_data是“ Import-Csv”命令的结果数组,而$ codenumber包含我要在数组中搜索的值。 这项工作还可以。

以下是$ newarray的值的示例:

P_SYS_InternalName  : #D_OCEV_ABC-
P_OCEV_Price        : 0.15
P_NDS_ValidPN       : 12345678
P_OCEV_PriceUnit    : 
P_NDS_VersionNumber : 1

现在,我想通过执行以下操作来修改P_OCEV_Price字段的值

$newarray.P_OCEV_Price = 0.2

但是,这似乎不起作用。 似乎$ newarray.P_OCEV_Price不包含任何值。 PS以某种方式无法将P_OCEV_Price识别为数组的单元格。

我也尝试使用

$newarray.["P_OCEV_Price"] = 0.2

遵守哈希表格式

接下来,我尝试通过使用将$ newarray显式定义为数组或哈希表。

$newarray = @()

要么

$newarray = @{}

到目前为止,似乎没有任何工作。 我究竟做错了什么??

由于$newarray变量是一个数组,因此您将无法使用简单的$newarray.P_OCEV_Price语法更改该值。 根据您的源数据,以下两个替代选项可能会对您有所帮助:

# Change the price of the first matching item
$newarray[0].P_OCEV_Price = 0.2

# Change the price for all matching items
$newarray | Foreach-Object { $_.P_OCEV_Price = 0.2 }

在这种情况下,我通常想指出大小为1的数组很容易与Powershell中的单个对象混淆。 如果尝试使用简单的输出语句查看$newarray$newarray[0] ,则结果可能看起来相同。 在这种情况下,最好使GetType()方便。

# This will show a type of Object[]
$newarray.GetType()

# The type here will show PSCustomObject instead
($newarray[0]).GetType()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM