karnak
karnak

Reputation: 69

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

I made a Powershell script that does a foreach on a strings array and collects only some info of them. This is the code:

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
        }
    }
}

When I run the script I have an output with two objects with different CopyNumber values (expected result):

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

Server            : Srv01
Id                : Srv01_999888777
CopyNumber        : 2

If I try to store the output in a variable, I have the same CopyNumber (not expected result):

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

What am I doing wrong?

Upvotes: 1

Views: 92

Answers (3)

js2010
js2010

Reputation: 27438

You're only making one object, that you modify and output twice. The assignment statement makes it complete first, before any output is shown. The pointer is the same to both outputs. (.\BkpScript) would do the same thing.

cat myscript.ps1

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


./myscript      

name
----
Joe
John


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

name
----
John
John

Upvotes: 0

stackprotector
stackprotector

Reputation: 13432

This code will create a new object whenever it shall be 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 ''
        }
    }
}

The output looks like you expect in your question:

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

Upvotes: 1

IT M
IT M

Reputation: 447

I guess you want something like this?

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}
        }
    }

Upvotes: 0

Related Questions