This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 16th, 2012  |  syntax: Power Shell  |  size: 1.71 KB  |  views: 516  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function Get-NextComputerName {
  2.  
  3.         #Workstation Name Scheme IE: NFDW0001
  4.         $sitecode ="NFDW"
  5.  
  6.         $searcher= new-object System.DirectoryServices.DirectorySearcher("(&(objectClass=computer)(name=$sitecode*))")
  7.         $comps = $searcher.findall()
  8.         $names= $comps  | %{$_.Properties.name}
  9.         $numbers= @($names | %{$_ -replace "$sitecode",""} | %{[int]$_} | sort)  
  10.                      #make sure single items still get treated as a list
  11.  
  12.         $i= 0
  13.         $prev= 0   #Assume you start at "0001"... $prev should be one less than your first expected number
  14.         $next= $numbers[0]
  15.         while (  ($next- $prev) -le 1     -and     $i -le ($numbers.count-1)) {
  16.              $i++
  17.              $prev= $next
  18.              $next= $numbers[$i]
  19.         }
  20.  
  21.         if ($i -le $numbers.count) {
  22.              $nextopen= "$sitecode{0:0###}" -f ($prev+1)
  23.         } else {
  24.              $nextopen= "$sitecode{0:0###}" -f ($next+1)
  25.         }
  26.  
  27.         write-host "Available Workstation: $nextopen"
  28.  
  29.         #Mobile Name Scheme IE: NFDM0001
  30.         $sitecode ="NFDM"
  31.  
  32.         $searcher= new-object System.DirectoryServices.DirectorySearcher("(&(objectClass=computer)(name=$sitecode*))")
  33.         $comps = $searcher.findall()
  34.         $names= $comps  | %{$_.Properties.name}
  35.         $numbers= @($names | %{$_ -replace "$sitecode",""} | %{[int]$_} | sort)  
  36.                      #make sure single items still get treated as a list
  37.  
  38.         $i= 0
  39.         $prev= 0   #Assume you start at "0001"... $prev should be one less than your first expected number
  40.         $next= $numbers[0]
  41.         while (  ($next- $prev) -le 1     -and     $i -le ($numbers.count-1)) {
  42.              $i++
  43.              $prev= $next
  44.              $next= $numbers[$i]
  45.         }
  46.  
  47.         if ($i -le $numbers.count) {
  48.              $nextopen= "$sitecode{0:0###}" -f ($prev+1)
  49.         } else {
  50.              $nextopen= "$sitecode{0:0###}" -f ($next+1)
  51.         }
  52.  
  53.         write-host "Available Mobile: $nextopen"
  54.  
  55. }
clone this paste RAW Paste Data