Getting the next largest number of the Workstation based on AD object?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Getting the next largest number of the Workstation based on AD object?

Post by ITEngineer »

Hi,

I need some assistance in the PowerShell scripting, so I can get the next available number of the Workstation based on Active Directory AD computer object.

My AD naming convention for computer objects like the below:
PRDVM001
PRDVM002
PRDVM003
...
PRDVM055
So how can I get the return from the Powershell script which basically says:

The next available AD computer name is PRDVM055
I just need to make sure that PRDVM055 is not already created in all of the AD domain before and is the largest number.

Thank you.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by jvierra »

You would have to query for all matching names and then find the one with the highest number.

$name = Get-AdComputer -Filter "Name -like 'PRDVM*'" | sort name | Select -Expand Name | select -last 1
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by ITEngineer »

jvierra wrote: Mon Oct 29, 2018 12:58 pm You would have to query for all matching names and then find the one with the highest number.

$name = Get-AdComputer -Filter "Name -like 'PRDVM*'" | sort name | Select -Expand Name | select -last 1
Wow that's cool, simple :)

Would it be possible to get the OU as well?
OU: domain.com/Servers/Production
Next available computer name: PRDVMSVR013

Thanks in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by jvierra »

Get what OU? A computer object does not have an OU property. Everything after the "CN=<name>," is the parent container.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by ITEngineer »

jvierra wrote: Mon Oct 29, 2018 5:18 pm Get what OU? A computer object does not have an OU property. Everything after the "CN=<name>," is the parent container.
Yes, how to get the CN where the object in $name was mentioned?
$name = Get-AdComputer -Filter "Name -like 'PRDVM*'" | sort name | Select -Expand Name | select -last 1
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by jvierra »

First get the distinguished name then extract the name and parent container portions.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by ITEngineer »

jvierra wrote: Mon Oct 29, 2018 6:04 pm First get the distinguished name then extract the name and parent container portions.
OK, I have managed to modify it like the below:

Code: Select all

$patterns = 'PROD', 'TEST', 'DR'
$CurrentADComputerName = $null
$NextADComputerName = $null

ForEach ( $prefix in $patterns) {
    
    $CurrentADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" | 
        Sort-Object Name | 
        Select-Object -Expand Name | 
        Select-Object -Last 1
    Write-Host "The current AD Computername for $prefix is: $CurrentADComputerName" -BackgroundColor Blue -ForegroundColor Yellow

    $CurrentOULocation = (Get-ADComputer $CurrentADComputerName -Properties CanonicalName).CanonicalName

    $NextADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" |
        ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
        Measure-Object -Maximum |
        ForEach-Object {$prefix + (++$_.Maximum).ToString('000')}
    Write-Host "The next available AD Computername for $prefix is: $NextADComputerName" -BackgroundColor Blue -ForegroundColor Green
    Write-Host "Which can be deployed in OU: $CurrentOULocation `n`n" -BackgroundColor Blue -ForegroundColor White
}
How to edit the $CurrentOULocation so it is displaying in domain.com/Server/Production format, without the Last computername ?

and there is some issue that I wanted to suppress or ignore:
Cannot convert value "PROD99-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
Cannot convert value "TEST67-DEV" to type "System.Int32". Error: "Input string was not in a correct format."
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by jvierra »

Why so much code? Just get the computers and the DistinguishedName and extract the information needed.

Code: Select all

$buttonTPMStatus_Click={
    $filters = @(
        "Name -like 'PROD*'",
        "Name -like 'TEST*'",
        "Name -like 'DR*'"
    )

    foreach($filter in $filters){
        $computer = Get-ADComputer -Filter $filter | 
                Sort-Object Name | 
                Select-Object -Last 1
                
        Write-Host "The current AD Computer is $computer" -BackgroundColor Blue -ForegroundColor Yellow
        $computer.Name
        ($computer.DistinguishedName -split ',')[1..99] -join ','
    }
}
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Getting the next largest number of the Workstation based on AD object?

Post by ITEngineer »

jvierra wrote: Mon Oct 29, 2018 7:03 pm Why so much code? Just get the computers and the DistinguishedName and extract the information needed.

Code: Select all

$buttonTPMStatus_Click={
    $filters = @(
        "Name -like 'PROD*'",
        "Name -like 'TEST*'",
        "Name -like 'DR*'"
    )

    foreach($filter in $filters){
        $computer = Get-ADComputer -Filter $filter | 
                Sort-Object Name | 
                Select-Object -Last 1
                
        Write-Host "The current AD Computer is $computer" -BackgroundColor Blue -ForegroundColor Yellow
        $computer.Name
        ($computer.DistinguishedName -split ',')[1..99] -join ','
    }
}
Mr. Vierra,

Pardon me, as I'm still learning in Powershell, I have found out that the below code is what I have managed to work but with the poor error handling, for example:
Cannot convert value "TEST-Server03-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
At line:19 char:25
+ ... ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger

Cannot convert value "PRD-CCTV-SITE1" to type "System.Int32". Error: "Input string was not in a correct format."
At line:19 char:25
+ ... ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
This is the below code, which I believe can be further modified to be more effective:

Code: Select all

$patterns = 'PROD', 'TEST', 'DEV'
$CurrentADComputerName = $null
$NextADComputerName = $null

ForEach ( $prefix in $patterns) {
    
    $CurrentADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" | 
        Sort-Object Name | 
        Select-Object -Expand Name | 
        Select-Object -Last 1
    Write-Host "The current AD Computername for $prefix is: $CurrentADComputerName" -ForegroundColor Yellow

    $CanonicalFullName = (Get-ADComputer $CurrentADComputerName -Properties *).CanonicalName
    $CurrentOULocation = [string]::join("/",$CanonicalFullName.split("/")[0..($CanonicalFullName.split("/").count -2)])

    $NextADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" |
        ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
        Measure-Object -Maximum |
        ForEach-Object {$prefix + (++$_.Maximum).ToString('000')} -ErrorAction SilentlyContinue
    Write-Host "The next available AD Computername for $prefix is: $NextADComputerName" -BackgroundColor Blue -ForegroundColor Green
    Write-Host "Which can be deployed in OU: $CurrentOULocation `n`n" -ForegroundColor White
}
Your help is greatly appreciated.
/* IT Engineer */
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked