随着你的LYNC环境用户不断增多,你可能为用户分配了很多的电话号码。突然有一天,你发现要找一个可用的号码来分配变得有些困难,而且你还没有一个列表来看全部已经被使用了的号码。那么今天下面这个脚本就可以帮你大忙了。我们先来看看执行它会有什么效果。

寻找可用的电话号码_职场

首先它提示你输入你的分机开始号码,也就是你的第一个分机的号码。

寻找可用的电话号码_电话号码_02

然后输入你想找几个可用的号码。完毕之后点击OK。

寻找可用的电话号码_休闲_03

系统就会在屏幕上输出可用的10个号码,非常方便吧。更方便的是它会在脚本文件所在的目录生成一个tel_list.txt文件,里面记录了所有用户的名称和他们对应的号码。

 

下面就是脚本内容,存成一个ps1文件执行就可以,如果不能执行脚本,你可能需要执行(set-executionpolicy RemoteSigned)

# 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 = “可用分机号码计算”
$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 = “请输入开始的分机号码:”
$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 = “分机号码数量”
$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 = “请输入你需要的分机号码数目:”
$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 -encoding "unicode"
    
     $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++
   }
 
     }
    }
 

 

脚本内容来自:http://ocsguy.com/2010/01/08/im-no-scripting-guy-but/,我个人做了简单的修改,使得它显示为中文,并且导出的文件可以正常显示中文名称。