Author Posts

April 2, 2015 at 12:59 am

Hi NG,

I have the following issue. In the script below you'll see that I select the last ad computer account by name in a specific OU. Our naming standard is like USLAX123456. From the whole range of numbers we only use USLAX6 and USLAX7.
My idea was to split the 5 digits after the 6 or 7, iterate it and but it back together. This works as long as the 2nd digit is no 0 😉
Of course there are many AD computer account names like USLAX600123 or USLAX701234. So I'm quiet often confronted with the '0' issue.

The issue with the '0' comes up when I convert the strint value into a integer value:

$intnumber = [int] $strnumber

I would like to know how to handle this 0 which is currently missing when I reattach both parts of the new AD computer name.

script part
_______________________________________________

# Get the AD object with the highest count
$lastADaccount = get-adcomputer -filter * -SearchBase $entry.OU | where-object {$_.name -match $adaccountname1} | sort name | select -ExpandProperty name -last 1
#split the 5 numbers from the chars (keep the digit 6 and 7 to distinguish DCN and DCB)
$strnumber = $LastADaccount.Substring(8)
$strname = $lastADaccount.remove(8)

# convert string to integer
$intnumber = [int] $strnumber
}

#interate by one
$intnumber++

#combine new AD computer account name
$NewADaccount = $strname + $intnumber

———————–

Regards

Christian

April 2, 2015 at 3:21 am

maybe try some regex to detect the digits and split them then. something like.

$ComputerName= "USLAX123456"
$ComputernameNumbers = $ComputerName -replace "\D",""
$ComputerNameNumbersSplit= $ComputerNameNumbers -split ""
[int]$ComputerNameNumbersSplit[-2] # This should have the last character in the split
[int]$ComputerNameNumbersSplit[1] # This should have the first character in the split

April 2, 2015 at 4:13 am

Ah, I see. When you are dealing with an [int] value, leading zeroes don't matter. You convert the number (such as "123") back to a string, and lose your padded zeroes. There are quite a few ways in .NET and PowerShell to get those zero padding back; here's one that requires the least amount of changes to your code:

# Instead of this:

$NewADaccount = $strname + $intnumber

# do this:

$NewADaccount = $strname + $intnumber.ToString('D5')

I think your code may be off, though, when you use Substring(8) and Remove(8). That will split the strings into "USLAX12" and "3456", rather than "USLAX1" and "23456", which is how I understood your text. I would tend to favor a regular expression here, which would reliably grab the letters and the first number, regardless of length. This requires a bit more of a change to your code, but makes it much more robust. Now both the name prefix and the numbers can have varying lengths, without breaking your code:

# Get the AD object with the highest count
$lastADaccount = get-adcomputer -filter * -SearchBase $entry.OU | where-object {$_.name -match $adaccountname1} | sort name | select -ExpandProperty name -last 1

if ($lastADaccount -notmatch '^(\D*\d)(\d+)$')
{
    throw "'$lastADaccount' does not match the expected name pattern."
}

#split the 5 numbers from the chars (keep the digit 6 and 7 to distinguish DCN and DCB)
$strnumber = $matches[2]
$strname = $matches[1]

# convert string to integer
$intnumber = [int] $strnumber

#interate by one
$intnumber++

#combine new AD computer account name
$NewADaccount = $strname + $intnumber.ToString('D' + $strnumber.Length)

April 2, 2015 at 4:18 am

Here is my take on this :-). Using the Format operator.

$adaccountname1 = 'USLAX6'

$lastADaccount = Get-ADComputer -Filter "Name -like '$adaccountname1*'" -SearchBase $entry.OU | 
    Sort-Object -Property Name | Select-Object -ExpandProperty Name -Last 1

$AccountNumber = [int]($lastADaccount .Name.Substring($adaccountname1.Length))

$NewADaccountName = '{0}{1:00000}' -f $adaccountname1, $AccountNumber + 1
$NewADaccountName