I’m no scripting guy….but

While working with Kevin Re, one of my clients at an OCS shop, he shared a very cool script with me and has agreed to allow me to post it online.  Kevin’s script queried for the next 5 available phone numbers in an OCS environment and output them on screen; this helped him streamline new user creation.  It also dumped all of the OCS users and their extensions into a TXT/CSV that can be used for other company phone number lists.  After seeing this script in action I thought it would be great to take this script and expanded on it just a bit. 

With Kevin’s permission I’m sharing his script with a few minor modifications we worked together on.  So let’s dive in:

Pre-Req’s

* Powershell is required  (servermanagercmd -i powershell – if you are running server 2008)

* Powershell must be modified to allow saved scripts to run (set-executionpolicy RemoteSigned)

* Run from your FE server

* Save the following code (between “Begin Code” and “End Code”) as a .ps1 file on your front end server  (you can create a directory like C:\FindFreePhoneNumbers)

Begin Code:

# Set up the vars
# Set up starting extension
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
$count
$find
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “Starting Extension”
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = “CenterScreen”

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
    {$count=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”
$OKButton.Add_Click({$count=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = “Please enter the starting extension:”
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

# Enter number of free extensions to find
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “Number of free extensions”
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = “CenterScreen”

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
    {$find=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”
$OKButton.Add_Click({$find=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = “Please enter the number of free extensions to find:”
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$find = [int]$find
$count = [int]$count
# Set up the vars
# Get the users and sort the info

   $a = get-wmiobject -class msft_sipesusersetting
   $a += get-wmiobject -class MSFT_SIPApplicationContactSetting
   $a = $a | Select-Object DisplayName, @{Name=”URI”; Expression = {if($_.LineURI -ne $null){[int]$_.LineURI.TrimStart(“tel:+”)}else{[int]0}}}
   $a = $a | Sort-Object -property URI
 
   $a | Export-Csv -path tel_list.txt
   
    $nomatch = 0
    ForEach ($user in $a)
    {

 
 if ($user.URI -ge 1)
 {

  if ($user.URI -ne $count)
  {
   While ($count -lt $user.URI)
   {
    $nomatch++
    $count
    $count++
    if ($nomatch -ge $find){break}
   }
   $count++
   if ($nomatch -ge $find){break}
  }
  else
  {
   $count++
  }

     } 
   }

 End Code

Now open power shell and execute your script, you will be prompted to enter your starting extension number and the number of free numbers you would like to find.  Once you enter the information and click “OK” you will see a list of free extensions and a txt file will be created in the directory you ran the script from containing all users and extensions.

Kevin and I plan on doing some more tweaking of the script and will likely add more functionality to it shortly.  Stay tuned, and if you have suggestions on functionality or changes feel free to post. 

Credit where credit is due:

More than 90% of the work involved in this script was performed by Kevin Re  (not me, another Kevin) and he was nice enough to share.

The input boxes were based on code from this link with a few minor modifications.

http://www.microsoft.com/technet/scriptcenter/resources/pstips/feb08/pstip0208.mspx

*Updated 1/8/2010 2:10 PM to include response group objects in the output file

Advertisements

About Kevin Peters

My name is Kevin Peters.
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s