Get the next available unused AD ComputerName and OU location?

Welcome Forums General PowerShell Q&A Get the next available unused AD ComputerName and OU location?

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #119835

    Participant
    Points: 65
    Rank: Member

    People,

    I wonder if anyone can assist me in modifying the below PowerShell code that shows the OU where the Computer Object name can be used:

    $patterns = 'PRD', 'DR', 'TEST'

    ForEach( $prefix in $patterns) {
    Get-ADComputer-Filter "Name -like '$($prefix)*'"|
    ForEach-Object {[int]($_.Name-replace'(?:.*?)(\d+)\Z','$1')} |
    Measure-Object-Maximum |
    ForEach-Object {$prefix+ (++$_.Maximum).ToString('000')}
    }
    Because at the moment it is just showing the next available name, and I'd like the expected output to be:
    OU: domain.com/Servers/Production
    Next available computer name: PRDSVR003
    OU: domain.com/Servers/Test
    Next available computer name: TESTSVR027
    OU: domain.com/Servers/DR Site
    Next available computer name: DRSVR089
    Without the error:
    Cannot convert value "PRD23-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
    Cannot convert value "TEST72-DEV" to type "System.Int32". Error: "Input string was not in a correct format."
    Any help would be greatly appreciated.
  • #119841

    Participant
    Points: 270
    Helping Hand
    Rank: Contributor

    You cannot cast a type [int] when the value contains others than numbers.

    # this will fail
    [int]'nineteen'
    # this not
    [int]'19'

    Could you please format your code as code? It will make your life and ours easier. Thanks.

  • #119847

    Participant
    Points: 65
    Rank: Member

    Hi Olaf,

    $patterns = 'PRD', 'DR', 'TEST'
    
    ForEach( $prefix in $patterns) {
        Get-ADComputer -Filter "Name -like '$($prefix)*'" |
            ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
            Measure-Object -Maximum |
            ForEach-Object {$prefix + (++$_.Maximum).ToString('000')}
    }

    OK, so I have format the code like the above.
    Is it possible to ignore the non-numerical value?

  • #119853

    Participant
    Points: 123
    Rank: Participant

    Is this "[int]($_.Name" not exactly what Olaf was saying, about not being able to define [int]'nineteen', except you're trying to define [int]'PRD", [int]'DR', [int]'TEST'.

  • #119862

    Participant
    Points: 65
    Rank: Member

    Is this "[int]($_.Name" not exactly what Olaf was saying, about not being able to define [int]'nineteen', except you're trying to define [int]'PRD", [int]'DR', [int]'TEST'.

    $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 *).DistinguishedName
    
        $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
    
    }

    OK, I have modified it like the above code, but so far I need to suppress the error with the AD object that is like the below:

    Cannot convert value "PRD23-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
    Cannot convert value "TEST72-DEV" to type "System.Int32". Error: "Input string was not in a correct format."

    and how to display the OU location like: domain.com/SITE 1/Server/Production not as DN with CN ?

  • #119868

    Participant
    Points: 270
    Helping Hand
    Rank: Contributor

    Is it possible to ignore the non-numerical value?

    No. You need to "extract/separate" the numbers from whatever string you have, cast it to [INT], treat it accordingly and use this for whatever further action you need to do.

  • #119920

    Participant
    Points: 270
    Helping Hand
    Rank: Contributor

    OK, I have modified it like the above code, .....

    No you have not. You still have the "[int]" in your code.
    Change your code line 16 from

    ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |

    ... to ....

    ForEach-Object {($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |

    I'd recommend for you to make a little step back and take your time to learn the basics of Powershell. You cannot learn a technology properly by guessing.

You must be logged in to reply to this topic.