header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Removing Array Items
Last Post 29 Jun 2010 05:38 AM by Koldkut. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
KoldkutUser is Offline
New Member
New Member
Posts:28
Avatar

--
28 Jun 2010 08:12 AM

    The problem I'm having is removing items from an array.  We are auditing our computer accounts in AD every other week and if it falls into certain criteria, it gets deleted from AD, if no, we then crunch the array to compare it to a list of machines in our SCCM database. The problem lays with removing the items we delete from the main array before crunching it agains the SCCM numbers.  For example, here's the problem we are having:

    $a = "a","b","c","d"
    $b = @()
    foreach($entry in $a){[string]($b = $b + $entry)}
    $a += "e","f"
    $c = @($a | where{($b) -notcontains $_})


    Andrew

    KoldkutUser is Offline
    New Member
    New Member
    Posts:28
    Avatar

    --
    28 Jun 2010 08:41 AM
    It gets better, working with a co-worker on this one and we have found that we get different results if we run this in the shell and ISE.

    $a = "a","b","c","d"
    $b = @()
    foreach($entry in $a){[string]($b = $b + $entry)}
    $a += "e","f"
    $c = @($a | where{($b) -notcontains $_})

    In the shell, it builds $b as "a b c d" and you can specify each one with its item count individually.

    In ISE, it builds $b as "abcd" and you can calso specify each on with its item count individually.

    The shell works great, the ISE does not.

    KoldkutUser is Offline
    New Member
    New Member
    Posts:28
    Avatar

    --
    28 Jun 2010 09:33 AM
    Trying to re-format my messages with the < code > and < /code > and it's not working too well.
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    28 Jun 2010 09:39 AM
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    28 Jun 2010 01:38 PM

    Sounds like a generic list is what you need:

    $list1 = New-Object 'System.Collections.Generic.List[String]'
    $list1.AddRange([String[]]("a", "b", "c", "d"))

    $list2 = New-Object 'System.Collections.Generic.List[String]'
    $list2.AddRange($list1)

    $list1.AddRange([String[]]("e", "f"))

    $list3 = New-Object 'System.Collections.Generic.List[String]'
    $list3.AddRange($list2.FindAll({ $list2 -notcontains $_ }))

    That will do the same as your original script. Can you go into a bit more detail about your situation? Or will that suffice?

    KoldkutUser is Offline
    New Member
    New Member
    Posts:28
    Avatar

    --
    29 Jun 2010 05:38 AM

    For whatever reason, they call me Mr. Typo at work......seems there's a character or two off.  But the case here is my last line

    $c = @($a | where{($b) -notcontains $_})

    and if I use the "@" in front of the "($b)" it understands that it is an array item instead of formating it in a long single line.  The actual code from that portion of my script is(the dashes just show the start of each line  because they are long lines):

    #Get list of computers that have not changed password in 45 days
    -$over45 = @(foreach($entry in $computers){if (((new-timespan ([DateTime]::FromFileTime($Entry.properties.pwdlastset[0])) (get-date)).days) -ge 45){$entry.properties.cn}})
    -$activedirectoryentries = @($activedirectoryentries| where-object {@($over45) -notcontains $_})

    #find all machines older than 7 days with no machine authenticated to them
    -$notauth = @(foreach($entry in $computers){if (($entry.properties.operatingsystem -eq $NULL) -and (((new-timespan ([DateTime]::FromFileTime($Entry.properties.pwdlastset[0])) (get-date)).days) -ge 7)){$entry.properties.cn}})
    -$activedirectoryentries = @($activedirectoryentries| where-object {@($notauth) -notcontains $_})

    Thanks for the help, I just needed to sit down with the technet article.....again.  I must admit I was a bit frustrated when he posted that link alone, I'd been there all morning trying to figure it out.  Why things work in shell and not ISE.....I'll not bother myself with that until I have more time to play with each environment.

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer