not declaring an empty array duplicates the output?

This topic contains 1 reply, has 2 voices, and was last updated by  postanote 2 weeks, 1 day ago.

  • Author
    Posts
  • #101812

    Mallik
    Participant

    Hi I am new to PowerShell, just want to understand why not declaring an empty array at the beginning of my script duplicates the output.

    PS C:\Users\PC> $computername= 'MYPC','Yourpc'
    $arr1 = @()

    Foreach($c in $computername){

    $S= Invoke-Command -ComputerName $c -ScriptBlock {Get-Service | where {$_.Name -like "*Bits*"}}
    $s1= Invoke-Command -ComputerName $c -ScriptBlock {Get-Service | where {$_.Name -like "ALG*"}}

    $arr1 += $s, $s1

    }

    $arr1| Format-Table

    Status Name DisplayName PSComputerName
    —— —- ———– ————–
    Stopped BITS Background Intelligent Transfer Ser... MYPC
    Stopped ALG Application Layer Gateway Service MYPC
    Stopped BITS Background Intelligent Transfer Ser... Yourpc
    Stopped ALG Application Layer Gateway Service Yourpc

    ============================================================================================================

    PS C:\Users\pc> $computername= 'MYPC','Yourpc'

    Foreach($c in $computername){

    $S= Invoke-Command -ComputerName $c -ScriptBlock {Get-Service | where {$_.Name -like "*Bits*"}}
    $s1= Invoke-Command -ComputerName $c -ScriptBlock {Get-Service | where {$_.Name -like "ALG*"}}

    $arr1 += $s, $s1

    }

    $arr1| Format-Table

    Status Name DisplayName PSComputerName
    —— —- ———– ————–
    Stopped BITS Background Intelligent Transfer Ser... MYPC
    Stopped ALG Application Layer Gateway Service MYPC
    Stopped BITS Background Intelligent Transfer Ser... Yourpc
    Stopped ALG Application Layer Gateway Service Yourpc
    Stopped BITS Background Intelligent Transfer Ser... MYPC
    Stopped ALG Application Layer Gateway Service MYPC
    Stopped BITS Background Intelligent Transfer Ser... Yourpc
    Stopped ALG Application Layer Gateway Service Yourpc

  • #101875

    postanote
    Participant

    So, you ran the first block, did not clear all your variables before running the second block?

    If that is the case the, previous runs variable is still populated, then all you are doing in that immediate second run is appending to it. If you kept running the second block, you'll see it get appended again, and so on.

    If you ran each code block in completely different PoSH sessions, you see the first on work and the second is empty.

    As a rule in all you scripts, one-liners, if you use a resource, you must release the resource. This is done by shutting down you PoSH session or manually clearing your resources at the end of your script. AKA garbage collection.

    The OS will release resources when they are no longer in use after a specific time, but you can for it.

    Examples of resource release at the end or a script run

    # Clear resource environment
    Get-PSSession | Remove-PSSession
    $null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$Shell)
    [System.GC]::Collect()         
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()
    Get-Variable -Name MyShell -ErrorAction SilentlyContinue | Remove-Variable -WhatIf:$false 
    

    Or are you saying, you did the first block and close down your PoSH session, then ran the second and got the dups?

    Secondly, what are you calling for each service individually with separate Invokes?
    You can just to this once with a single Invoke.:

    '*bits*','*alg*' | %{Get-Service -Name $_ }
    

You must be logged in to reply to this topic.