I need to make a script that writes user id's with certain attributes to a csv file. Before I lookup all the attributes I want to collect the user id's from different sources into an array. When I add the first users to the array, the array is holding these user id's. However, when I want to add user id's for the second time, the array is empty again.
$array3 = @()
$array = @()
function get-start {
Write-Host "Function Get-Start started"
$text = Get-Content -Path C:\Data\text.txt
foreach ($Line in $text)
{
If ($Line -eq "text1.txt")
{
Write-Host "$Line is found"
Get-Text1
}
Elseif ($Line -eq "text2.txt")
{
Write-Host "$Line is found"
Get-Text2
}
}
}
function Get-Text1 {
Write-Host "function Get-Text1 started"
$array = Get-Content -Path C:\Data\text1.txt
Write-Host "content of text1.txt is $array"
Add-Arrays
}
function Get-Text2 {
Write-Host "function Get-Text2 started"
$array = Get-Content -Path C:\Data\text2.txt
Write-Host "content of text2.txt is $array"
Add-Arrays
}
function Add-Arrays {
Write-Host "function Add-Arrays started"
Write-Host "content of array3 is $array3"
$array3 += $array
Write-Host "content of array3 now is $array3"
}
get-start
the outcome of this code is the following
PS D:\DMS> C:\Data\Test_Arrays.ps1
Function Get-Start started
text1.txt is found
function Get-Text1 started
content of text1.txt is Rood Groen Blauw Oranje Paars Zwart Wit
function Add-Arrays started
content of array3 is
content of array3 now is Rood Groen Blauw Oranje Paars Zwart Wit
text2.txt is found
function Get-Text2 started
content of text2.txt is Appel Peer Banaan Kiwi Appelsien Mandarijn Perzik
function Add-Arrays started
content of array3 is
content of array3 now is Appel Peer Banaan Kiwi Appelsien Mandarijn Perzik
As you can see the content of $array3 is empty before I add content to it and is empty again when I want to add content for a second time.
How can I let powershell remember the content?
Thank you in advance