Reece Alqotaibi
Reece Alqotaibi

Reputation: 23

Displaying the Next Available Number in a sequence

Our organisation renames all new computers before they join our domain and I am trying to create a script to display the next available number.

So, our naming convention is as follows

Example:

GBYOR-DT1

   GB      -   YOR     -   DT     -     1
[Country]     [City]    [Desktop]    [Number]

However my predecessor thought to number these in no logical order and that just isn't happening on my watch. (Did someone say OCD?)

Currently I run

> Get-ADComputer -Filter * | where{$_.Name -Like "GBYOR-DT*" | FL Name | Export-CSV -Path "X Location"

I then go into Excel, split the cells and the sort numerically.

What I would like is to create a script that just shows me the next available number.

For example, as it currently stands, we have X number of machines, one is GBYOR-DT4, another is GBYOR-DT245 but there is no GBYOR-DT15.

I would like to script this so that when I run it, it will look at all of the machines registered on our domain and then display the next one

> GBYOR-DT1 
> GBYOR-DT2 
> GBYOR-DT3 
> GBYOR-DT4 
> GBYOR-DT6

> 
> ...GBYOR-DT5 does not exist so this is the next number to use for renaming.

Any help would be appreciated.

Upvotes: 1

Views: 1797

Answers (2)

TessellatingHeckler
TessellatingHeckler

Reputation: 29003

$ComputerNames = 'GBYOR-DT1', 'GBYOR-DT4', 'GBYOR-DT6', 
                 'GBYOR-DT10', 'GBYOR-DT2', 'GBYOR-DT3'

Compare-Object (1..15) ($ComputerNames -replace '.*?(?=\d+$)' -as [int[]]) -PassThru

Where 15 is some number higher than the max, assuming you know or can guess such.

And the regex replaces all the text before any numbers, in each computer name.

Solution taken from: Find free number in a range

Upvotes: 1

user6811411
user6811411

Reputation:

To have it more universal I put together this script,
It's a bit different from PetSerAls comment and also from my suggestion.

For testing I feed names in and two real commands are commented out.

## Q:\Test\2018\07\26\SO_51543166.ps1

$Prefix = Read-Host -Prompt "Enter the computer name prefix to search the first free number for"
$Prefix -match '^(.*?)\d*$' | Out-Null # strip possibly trailing numbers
$Prefix = $Matches[1]

$Number = 0 # initialize last Number to zero
'GBYOR-DT1', 'GBYOR-DT4', 'GBYOR-DT6', 'GBYOR-DT10', 'GBYOR-DT2', 'GBYOR-DT3'|
Where-Object {$_ -match "^$Prefix(\d+)$" }|
#Get-ADComputer -Filter * |
#Where-Object {$_.Name -match "^$Prefix(\d+)$" }|
   Select-Object @{n='UsedNumber';e={[int]$Matches[1]}} | Sort-Object UsedNumber |
      ForEach-Object {
         While ($Number +1 -lt $_.UsedNumber){"Free: {0}{1}" -f $Prefix,++$Number}
         $Number = $_.UsedNumber
      }
# if there was no gap get the next one.
"Free: {0}{1}" -f $Prefix,++$Number

Sample run:

> Q:\Test\2018\07\26\SO_51543166.ps1
Enter the computer name prefix to search the first free number for: GBYOR-DT
Free: GBYOR-DT5
Free: GBYOR-DT7
Free: GBYOR-DT8
Free: GBYOR-DT9
Free: GBYOR-DT11

To have a life test remove or comment these two lines:

'GBYOR-DT1', 'GBYOR-DT4', 'GBYOR-DT6', 'GBYOR-DT10', 'GBYOR-DT2', 'GBYOR-DT3'|
Where-Object {$_ -match "^$Prefix(\d+)$" }|

and uncomment these:

Get-ADComputer -Filter * |
Where-Object {$_.Name -match "^$Prefix(\d+)$" }

Upvotes: 0

Related Questions