Powershell SCCM Script Help

Welcome Forums General PowerShell Q&A Powershell SCCM Script Help

Viewing 3 reply threads
  • Author
    Posts
    • #223293
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      I am new to powershell scripting, I have done the basics to create scripts but I am now trying to create a script to use with SCCM and I can see it in my head but I have no clue how to implement this, find the commands and understand Object Arrays etc. So this is what I am doing. I have computer collection departments in SCCM and I want to read the list of computers and determine if a person has two computers. I will know this by the computer name. Example: AAA-JCOO-DE5040. That tells me the department, name and model computer. So I want to collect all the machines into a variable and then compare each line to one another to determine if the middle section JCOO is the same to another machine in that collection lets say AAA-JCOO-DE3050. I would then compare those two machines and Delete the 5040 from SCCM since the 3050 is the newer machine and it never got deleted out of SCCM when we did the computer upgrade. We do about 100 upgrades a month so as you can tell my Console is over run with old machines. I have over 2000 computers and do not want to do this manually. I know how to pull the computer names per collection into a variable but I am having trouble comparing the list especially the JCOO of the machine name. This is how I am getting the machines into a variable

      $Admissions = Get-CMCollectionMember -CollectionName “Admissions (ADM)” | Select Name

       

    • #223308
      Participant
      Topics: 4
      Replies: 2248
      Points: 5,490
      Helping Hand
      Rank: Community MVP

      Jonathan,, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      If your computer names have the structure you showed above it’s very easy to split them into their parts like this:

      'AAA-JCOO-DE5040' -split '-'

      Now you can use this to create calculated properties you can use in a further step to group the results by the part of the name you’re after

      $Admissions = Get-CMCollectionMember -CollectionName "Admissions (ADM)" | 
          Select-Object -Property Name,
                                  @{Name = 'Part1'; Expression = { ($_.Name -split '-')[0] } },
                                  @{Name = 'Part2'; Expression = { ($_.Name -split '-')[1] } },
                                  @{Name = 'Part3'; Expression = { ($_.Name -split '-')[2] } }

      Now you can group this by the seconds part of the name and extract only the computers with more than one occurrence of this part like this:

      $Admissions | 
          Group-Object -Property Part2 | 
              Where-Object -Property Count -GT -Value 1 |
                  Select-Object -ExpandProperty Group

      BTW: You know you can set up SCCM to purge all assets haven’t had a heartbeat for an adjustable amount of days, don’t you? 😉

    • #223341
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      Another fun method to split into vars. Fun with Powershell:

      $part1,$part2,$part3 = ('AAA-JCOO-DE5040' -split '-')
      
    • #223446
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Since we’re having fun 🙂

      $part1,$part2,$part3 = 'AAA-JCOO-DE5040'.split('-')

Viewing 3 reply threads
  • You must be logged in to reply to this topic.