当我将 PS 脚本存储在变量中时,为什么会得到不同的结果?

Why am I getting a different result when I store in a variable my PS script?

我制作了一个 Powershell 脚本,它在字符串数组上执行 foreach 并仅收集它们的一些信息。 这是代码:

begin {
    $IMAGELIST = @(
    "IMAGE Srv01 something something something Srv01_999888777 something"
    "FRAG 1 something something"
    "FRAG 2 something something"
    )
}   

process {
    foreach ($LINE in $IMAGELIST) {
        if ($LINE.StartsWith("IMAGE")) {
            $IMAGELINE = $LINE.split()
            $BKP_OBJ = [pscustomobject]@{
                Server=$IMAGELINE[1]
                Id=$IMAGELINE[5]
            }
        }

        elseif ($LINE.StartsWith("FRAG")) {
            $FRAGLINE = $LINE.split()
            $BKP_OBJ | Add-Member -Force @{
                CopyNumber = $FRAGLINE[1]
            }
            Write-Output -InputObject $BKP_OBJ
        }
    }
}

当我 运行 脚本时,我有一个输出,其中包含两个具有不同 CopyNumber 值的对象(预期结果):

PS C:\Users\db> .\BkpScript.ps1
Server            : Srv01
Id                : Srv01_999888777
CopyNumber        : 1

Server            : Srv01
Id                : Srv01_999888777
CopyNumber        : 2

如果我尝试将输出存储在一个变量中,我得到相同的 CopyNumber(不是预期的结果):

PS C:\Users\db> $myvar = .\BkpScript.ps1
PS C:\Users\db> $myvar
Server            : Srv01
Id                : Srv01_999888777
CopyNumber        : 2

Server            : Srv01
Id                : Srv01_999888777
CopyNumber        : 2

我做错了什么?

我猜你想要这样的东西?

begin {
    $BKP_OBJ = $null
    $IMAGELIST = @(
    "IMAGE Srv01 something something something Srv01_999888777 something",
    "FRAG 1 something something",
    "FRAG 2 something something"
    )
}   

process {
    $Ximglist = @()
    $Xfrglist = @()
    foreach ($LINE in $IMAGELIST) {
        if ($LINE.StartsWith("IMAGE")) {
            $Ximglist += $LINE
        }
        elseif ($LINE.StartsWith("FRAG")) {
            $Xfrglist += $LINE
        }
    }
    foreach ($Ximg in $Ximglist) {
        $Xfrglist | % {($_.split())[1]} | % {$BKP_OBJ = [pscustomobject]@{
                    Server= ($Ximg.Split())[1]
                    Id= ($Ximg.Split())[5]
                    CopyNumber=$_
                }; 
                Write-Output $BKP_OBJ}
        }
    }

你只制作了一个对象,修改并输出了两次。在显示任何输出之前,赋值语句首先使其完成。两个输出的指针相同。 (.\BkpScript) 会做同样的事情。

cat myscript.ps1

$a = [pscustomobject]@{name='Joe'}
$a
$a.name = 'John'
$a


./myscript      

name
----
Joe
John


(./myscript)  # or ./myscript | sort

name
----
John
John

此代码将在 output/piped:

时创建一个新对象
begin {
    $IMAGELIST = @(
    "IMAGE Srv01 something something something Srv01_999888777 something"
    "FRAG 1 something something"
    "FRAG 2 something something"
    )
}   

process {
    $Server = ""
    $Id = ""
    $CopyNumber = 0
    foreach ($LINE in $IMAGELIST) {
        if ($LINE.StartsWith("IMAGE")) {
            $IMAGELINE = $LINE.split()
            $Server=$IMAGELINE[1]
            $Id=$IMAGELINE[5]
        } elseif ($LINE.StartsWith("FRAG")) {
            $FRAGLINE = $LINE.split()
            $CopyNumber = $FRAGLINE[1]
            Select-Object @{n='Server'; e={$Server}}, @{n='Id'; e={$Id}}, @{n='CopyNumber'; e={$CopyNumber}} -InputObject ''
        }
    }
}

输出看起来像您在问题中所期望的那样:

PS C:\Users\db\Desktop> .\test.ps1

Server Id              CopyNumber
------ --              ----------
Srv01  Srv01_999888777 1
Srv01  Srv01_999888777 2


PS C:\Users\db\Desktop> $myvar = .\test.ps1
PS C:\Users\db\Desktop> $myvar

Server Id              CopyNumber
------ --              ----------
Srv01  Srv01_999888777 1
Srv01  Srv01_999888777 2