Advertisement
Guest User

Untitled

a guest
Nov 16th, 2012
1,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement