Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add string param to array

I have a simple problem. I’m trying to solve my problem below. I hope someone can help and guide me on my problem.

I want to add a string to an array in a function. Here’s my function.

function myFunc {
    Param(
    [string]$Logs,
    [string]$Status
    )
    
    if ($Status -eq $True) {
        $logsArray += $Logs
        Write-Host "Completed!`n" -ForegroundColor Green
        $logsArray
        } else {
        $logsArray += $Logs
    }
}

If I try to enter these 3 strings

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

myFunc -Logs "[Success] Test 1" -Status $False
myFunc -Logs "[Success] Test 2" -Status $False
myFunc -Logs "[Success] Test Final" -Status $True

The only result I’m getting is

Completed!

[Success] Test Final

and this is what I want to get

Completed!

[Success] Test 1
[Success] Test 2
[Success] Test Final

Thank you so much in advanced!

>Solution :

From feedback provided in comments, it seems that what you are actually looking to do is to collect all input and output all once the argument to -Status is $true. For that you have two options, but note, this is not a common pattern you should follow from PowerShell perspective as the UX is not great if this is meant to be shared. Its very likely the problem you’re facing could be solved in a cleaner way.

  1. Define an array or list at script scope from within your function and check in each function call that its not previously defined (to not overwrite it):
function myFunc {
    Param(
        [string] $Logs,
        [bool] $Status
    )

    if(-not $Script:LogsArray) {
        $Script:LogsList = [System.Collections.Generic.List[string]]::new()
    }

    $Script:LogsList.Add($Logs)

    if ($Status) {
        Write-Host "Completed!`n" -ForegroundColor Green
        $Script:LogsList
    }
}

myFunc -Logs '[Success] Test 1' -Status $False
myFunc -Logs '[Success] Test 2' -Status $False
myFunc -Logs '[Success] Test Final' -Status $True
  1. Second option is to define the list or array outside the function, in which case the if condition can be removed:
$LogsList = [System.Collections.Generic.List[string]]::new()

function myFunc {
    Param(
        [string] $Logs,
        [bool] $Status
    )

    $LogsList.Add($Logs)

    if ($Status) {
        Write-Host "Completed!`n" -ForegroundColor Green
        $LogsList
    }
}

myFunc -Logs '[Success] Test 1' -Status $False
myFunc -Logs '[Success] Test 2' -Status $False
myFunc -Logs '[Success] Test Final' -Status $True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading