Identify Servers with odd numbers

Comments

3 comments

  • Luke Nichols

    Robbie,

    Interesting problem, and I'm not sure if it would have been possible before the new PowerShell scanner. This may not be the most elegant method, but I think you could do it with this method:

    Create a new PowerShell scanner containing the following:

    #-----------------------------------------------------------------------------------

    $ComputerName = $env:COMPUTERNAME
    $ComputerNameNumber = $ComputerName.SubString(6,2)

    if (($ComputerNameNumber % 2) -eq 0) {
    #Remainder of number/2 is 0, so it must be even
    $ServerGroup = 1
    } elseif (($ComputerNameNumber % 2) -ne 0) {
    #Remainder of number/2 is >0, so it must be odd
    $ServerGroup = 2
    } else {
    Write-Host "Unhandled exception, format of `$env:COMPUTERNAME ($env:COMPUTERNAME) is probably wrong."
    $ServerGroup = "ERROR"
    }

    [PSCustomObject]@{
    ServerGroup = $ServerGroup
    }

     

    #-----------------------------------------------------------------------------------

    ...Then, scan the servers with the new profile. You should then be able to place them into dynamic collections based on their new table containing the data from the new scanner.

    Please note that this method will only work if all of the computer names match the format you have given exactly. If the number of digits or the placement of the characters changes, this will break down. I put in an exception in case the substring pulled is not a valid number that will populate the "ServerGroup" value with the text "ERROR", so if you see that you should know why.

    As always, you should probably only scan a few machines at first to make sure it behaves as expected. Don't just blast it out to all of your computers right away.

    1
    Comment actions Permalink
  • Chris James

    Love the ingenuity with the PowerShell Scanner, you could also do this with more basic approach that won't require an additional scan. 

    Assuming all your servers match the pattern you posted above, you can do this with a simple Matches Pattern filter looking at the computer's names.

    Here's an example of some computers in our lab here:

     

    Then here's using your example host names:

     

    Depending how your real host names are formatted you may need to change up the filter logic some.

    Here's the documentation in the screenshot above: https://documentation.pdq.com/PDQInventory/19.0.40.0/index.html?value-filter-settings.htm

    1
    Comment actions Permalink
  • Luke Nichols

    Good call, Chris. I suspected there was an easier way, I just couldn't see it.

    When you have a shiny new hammer, everything looks like a nail.

    0
    Comment actions Permalink

Please sign in to leave a comment.