Identify Servers with odd numbers
Hi we use server naming convention to identify certain workloads, I would like to pull together a collection based on these conventions. I would like to have a dynamic collection, call it group 1 & group 2, in group 1 will be all the even numbers and in group 2 is all the odd numbers.
How can I do this?
Server10aUK1
Server10bUK1
Server11aUK1
Server11bUK1
Server12aUK1
Server12aUK1
-
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.
-
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
Please sign in to leave a comment.
Comments
3 comments