将数组列表从第一个脚本传送到第二个脚本 [英] Piping an arraylist from first script to second script

查看:39
本文介绍了将数组列表从第一个脚本传送到第二个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个数组列表从脚本 n.1 传送到脚本 n.2 并用 while 循环修改它

I need to pipe an arraylist from script n.1 to script n.2 and modify it with a while cycle

[System.Collections.Arraylist]$ArrBkp=@()

$ArrObj = New-Object psobject -Property @{Type="Full";Id=1001}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Diff";Id=1002}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Full";Id=1003}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Diff";Id=1004}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Diff";Id=1005}
[void]$ArrBkp.Add($ArrObj)

$ArrBkp

脚本 n.2 - PS-Rest.ps1

[CmdletBinding()]Param(
[Parameter(ValueFromPipeline=$true)]
$ArrBkp,
[Parameter()]
$Id
)

$ArrBkp.Reverse()

foreach ($Bkp in $ArrBkp) {
    while ($Bkp.Id -gt $Id) {$Bkp;$ArrBkp.Remove($Bkp)}
}

$ArrBkp

我期望的是:

PS C:\Users\dio\Documents\script\PS-Rest> .\PS-GetBak.ps1 | .\PS-Rest.ps1 -Id 1004
   Id Type
   -- ----
 1004 Diff
 1003 Full
 1002 Diff
 1001 Full

说明:while 循环应该从数组列表中删除 Id 值为 gt 1004 的对象

Explained: while loop should remove object from arraylist that has Id value gt 1004

实际结果:只有来自 arraylist 的最后一个值通过管道传输到脚本 n.2 导致 while 循环错误

Actual result: Only the last value from arraylist is piped to the script n.2 causing while loop error

In C:\Users\dio\Documents\script\PS-Rest\PS-Rest.ps1:11 car:49
+     while ($Bkp.Id -gt $Id) {$Bkp;$ArrBkp.Remove <<<< ($Bkp)}
    + CategoryInfo          : InvalidOperation: (Remove:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

推荐答案

好的,我重新设计了它,所以它非常接近原版.除了我通过属性名称传递管道.我有点走私子属性下的数组列表.出于某种原因,PS 脚本无法返回顶层的数组列表.无论如何,管道一次只能处理一个元素.并收集要删除的元素,然后在第二个循环中删除.这样它就不会破坏第一个循环.此外,使用 while 语句没有意义,因为条件在移除后永远不会改变.

Ok, I reworked it, so it's pretty close to the original. Except I'm passing on the pipe by property name. I'm sort of smuggling the arraylist under a sub-property. For some reason, a PS script can't return an arraylist at the top level. And a pipeline would only process an element at a time anyway. And collecting the elements to delete, and then deleting in a second loop. This way it doesn't break the first loop. Also, having a while statement doesn't make sense, since the condition would never change after a remove.

ps-getbk.ps1:

ps-getbk.ps1:

[System.Collections.Arraylist]$ArrBkp=@()

$ArrObj = New-Object psobject -Property @{Type="Full";Id=1001}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Diff";Id=1002}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Full";Id=1003}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Diff";Id=1004}
[void]$ArrBkp.Add($ArrObj)

$ArrObj = New-Object psobject -Property @{Type="Diff";Id=1005}
[void]$ArrBkp.Add($ArrObj)

[pscustomobject]@{ArrBkp=$ArrBkp}

ps-rest.ps1:

ps-rest.ps1:

[CmdletBinding()]Param(
  [Parameter(ValueFromPipelineByPropertyName=$true)]
  $ArrBkp,
  [Parameter()]$Id
)

$ArrBkp.Reverse()

$dellist = foreach ($Bkp in $ArrBkp) {
    if ($Bkp.Id -gt $Id) {$Bkp}
}

foreach ($Bkp in $dellist) {
    $ArrBkp.remove($Bkp)
}

$ArrBkp

来自 .\PS-GetBak.ps1 的输出 |.\PS-Rest.ps1 -Id 1004

  Id Type
  -- ----
1004 Diff
1003 Full
1002 Diff
1001 Full

这篇关于将数组列表从第一个脚本传送到第二个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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