Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Sort list and select object less than X

$
0
0

Hey guys,

I’m using the following code to grab all computer objects in AD, Sort them and select the next available name in the list and amend it to be our naming convention.

$searcher = [ADSISearcher]'(&(objectCategory=computer)(name=0000*))' $searcher.PageSize = 1000 $last = $searcher.FindAll() | Foreach-Object { [int]($_.Properties.name -replace 'D').Trim() } | Sort-Object | Select-Object -Last 5 $digitLength = "$last".Length $NewComputerName = "{0}{1:D$digitLength}" -f '0000', ($last + 1) + 'Live' $NewComputerName 

Running this produces a list like the following:

430 431 432 904 905

So the next number the script will choose is 906, However numbers over 900 are for testing machines. How can i amend this to ignore numbers over 900 so that it will choose (for example) 433 as the next available number?

I think im missing something simple here but an extra set of eyes always helps.

submitted by /u/ginger_sysadmin
[link] [comments]

The post Sort list and select object less than X appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles