# Help Understanding What's Happening

**URL:** <https://forums.powershell.org/t/help-understanding-whats-happening/19796>\
**Category:** PowerShell Help\
**Created:** [July 5, 2022, 3:23pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796 "2022-07-05T15:23:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevinGagel](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kevingagel/32/5982_2.png) [@KevinGagel](https://forums.powershell.org/u/KevinGagel)\
**Post date:** [July 5, 2022, 3:23pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/1 "2022-07-05T15:23:02Z")

</div>

I am trying to use an arraylist instead of the += to add items to an array. Unfortunately in this script I’m not getting the data I’m expecting.

This iteration of the script is producing the data (IP addresses) but runs very slow (I know it’s building the array with each addition)

```auto
#Result File
$Results = '.\ClientSitesResults.txt'

#Remove previous results file
Remove-Item -Path $Results -Force -ErrorAction SilentlyContinue

#Path to the netlogon.log file
$NetLogon = ".\netlogon.log"

#Load the netlogon.log file data
$data = Get-Content -Path $NetLogon

#Create Array to hold the split up entries
$NoClientSite = @()

$NoClientSite = $data | ForEach-Object -Process {
    $LineData = $_.Split(" ")
    if ($LineData[4] -eq 'NO_CLIENT_SITE:') {
        $NoClientSite += $LineData[6]
    }
}

$NoClientSites = $NoClientSite | Sort-Object

Add-Content -Path $Results -Value $NoClientSites -Encoding UTF8

```

However, when I convert the script to use the ArrayList instead of the expected IP addresses in the data. I’m getting int32 numbers…

```auto

#Result File
$Results = '.\ClientSitesResults.txt'

#Remove previous results file
Remove-Item -Path $Results -Force -ErrorAction SilentlyContinue

#Path to the netlogon.log file
$NetLogon = ".\netlogon.log"

#Load the netlogon.log file data
$data = Get-Content -Path $NetLogon

#Create Array to hold the split up entries
$NoClientSite = [System.Collections.ArrayList]@()

$NoClientSite = $data | ForEach-Object -Process {
    $LineData = $_.Split(" ")
    if ($LineData[4] -eq 'NO_CLIENT_SITE:') {
        $NoClientSite.Add($LineData[6])
    }
}

$NoClientSites = $NoClientSite | Sort-Object

Add-Content -Path $Results -Value $NoClientSites -Encoding UTF8

```

Something is turning the IP addresses into int32 numbers. I end up with a count from 1 to 280,000.  
I’m using the ArrayList in other code and this isn’t happening there.

What gives?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 5, 2022, 3:47pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/2 "2022-07-05T15:47:22Z")

</div>

> [@KevinGagel](#):
>
> I am trying to use an arraylist instead of the += to add items to an array. Unfortunately in this script I’m not getting the data I’m expecting.

In the vast majority of the cases that’s totally unnecessary.

Here an example :

Instead of _adding_ every single element in each iteration to the array - what’s actually creating a new array with the old and the newly added elements like this:

```auto
$ServiceList = Get-Service
$Result = @()

foreach ($Service in $ServiceList) {
    $SingleService = 
    [PSCustomObject]@{
        Name = $Service.Name
        Status = $Service.Status
    }
    $Result += $SingleService
}
$Result

```

You simply collect the output of the loop in a variable like this:

```auto
$ServiceList = Get-Service
$Result = 
    foreach ($Service in $ServiceList) {
        [PSCustomObject]@{
            Name = $Service.Name
            Status = $Service.Status
        }
    }
$Result

```

… cleaner .. easier to read and usually way faster … 😉

---

<div class="post-metadata">

**Author:** ![KevinGagel](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kevingagel/32/5982_2.png) [@KevinGagel](https://forums.powershell.org/u/KevinGagel)\
**Post date:** [July 5, 2022, 4:08pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/3 "2022-07-05T16:08:28Z")

</div>

I’m parsing the netlog file line by line and grabbing only the IP address listed and putting that into the array. It’s a simple array of IP addresses, nothing else.

Then I want to sort the array and make them unique. With nearly 300,000 lines of multiple duplicate entries of IP addresses the += takes forever to build the array (over an hour).

The ArrayList takes a couple of minutes but the data morphs into an integer instead of remaining the IP address.

I’m trying to understand what is happening to the data. Not find a different way of iterating through a set of data.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 5, 2022, 4:14pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/4 "2022-07-05T16:14:19Z")

</div>

> [@KevinGagel](#):
>
> The ArrayList takes a couple of minutes but the data morphs into an integer instead of remaining the IP address.

This still feels way too long. 😉

Can you share a few sanitized example lines of your log file? (formatted as code please)

---

<div class="post-metadata">

**Author:** ![KevinGagel](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kevingagel/32/5982_2.png) [@KevinGagel](https://forums.powershell.org/u/KevinGagel)\
**Post date:** [July 5, 2022, 4:20pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/5 "2022-07-05T16:20:40Z")

</div>

Here’s six lines of the log file.

```auto
09/06 23:59:34 [1112] DOMAIN: NO_CLIENT_SITE: (null) 192.168.XX.XX
09/06 23:59:59 [1112] DOMAIN: NO_CLIENT_SITE: (null) 192.168.XX.XX
09/07 00:55:48 [1112] DOMAIN: NO_CLIENT_SITE: (null) 192.168.XX.XX
09/07 00:58:07 [1112] DOMAIN: NO_CLIENT_SITE: XXXX21DD2 10.25.XX.XXX
09/07 00:58:24 [1112] DOMAIN: NO_CLIENT_SITE: XXXX21DD2 10.25.XX.XXX
09/07 00:58:42 [1112] DOMAIN: NO_CLIENT_SITE: XXXX21DD2 10.25.XX.XXX

```

The last column is the IP addresses I’m after. After storing them in an array the plan is to sort the array and make them unique. The vast majority of the entries are duplicates. The above shows just two IP addresses.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 5, 2022, 4:23pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/6 "2022-07-05T16:23:14Z")

</div>

Arraylist are deprecated. It is recommended to use system.collections.generic.list instead. Arraylist output the index when you add an item, so if you insist on building a list vs capturing to a variable like Olaf showed, and insist on using the deprecated arraylist, you should void or null the output it creates.

```auto
$null = $deprecatedarraylist.Add($stuff)

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 5, 2022, 4:26pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/7 "2022-07-05T16:26:21Z")

</div>

It should only taky seconds

```auto
Get-Content -Path .\netlogon.log |
ForEach-Object {
    if ($_ -match 'NO_CLIENT_SITE:') {
            ($_ -split ' ')[-1]        
    }
} | 
Sort-Object -Unique

```

Of course this code should run on the same machine where the log file is. Please do not access the log file via an UNC path. 😉

---

<div class="post-metadata">

**Author:** ![KevinGagel](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kevingagel/32/5982_2.png) [@KevinGagel](https://forums.powershell.org/u/KevinGagel)\
**Post date:** [July 5, 2022, 4:34pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/8 "2022-07-05T16:34:04Z")

</div>

> [@Olaf](#):
>
> ```auto
> Get-Content -Path .\netlogon.log |
> ForEach-Object {
> if ($_ -match 'NO_CLIENT_SITE:') {
> ($_ -split ' ')[-1]        
> }
> } | 
> Sort-Object -Unique
> 
> ```

That’s really helpful. Thank you Olaf.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/help-understanding-whats-happening/19796/9 "2024-05-16T20:17:45Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


