Active Directory Tefonnummernsuche

Eine zweite Eigenentwicklung. Es kann schon mal vorkommen das man in größeren Organisation einen Telefonnummer oder Durchwahl eines Mitarbeiters sucht. Outlook bietet an der stelle nur begrenzte Möglichkeiten.

Ich habe ein kleines Programm geschrieben das mit Hilfe des AD Power Shell Moduls eine Abfrage erstellt und die Nummern der Benutzer durchsucht. Das Script lässt sich einfach anpassen, sollten mehr Informationen benötigt werden. Zudem kann ein Unternehmenslogo mit eingebunden werden.

Ich habe die Version aktualisiert auf 1.0.0.1. Diese enthält einige Verbesserung der Benutzeroberfläche, so kann das Fenster jetzt minimiert werden, eine Benachrichtigung erscheint dann im Infobereich. Version 1.0.0.0 stelle ich nicht weiter zur Verfügung!

Mit dem Programm PS2EXE kann das Script inklusive Bilder o.ä. einfach in eine Ausführbare Datei konvertiert werden: http://ps2exe.codeplex.com/

#===========================================================#
# ActiveDirectory Telefonnummernsuche 1.0.0.1               #
# © Andres Sichel // blog.asichel.de // blog@asichel.de     #
# Sucht nach Telefon oder Mobilfunknnumern von AD Benutzern #
#                                                           #
#                                                           #        
#===========================================================#

#Ausgangsbreite festlegen, damit alles in die Ausgabe passt
$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (700, 25)

#Ausführungspfad wichtig für Bild/Logo und ICON, muss im Pfad vom Script liegen
$script:Scriptpath = Split-Path $script:MyInvocation.MyCommand.Path
$script:Recipients_Info_Array = New-Object System.Collections.ArrayList

#Systembibliotheken laden
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

#AD Modul laden
$modul=Get-Module ActiveDirectory
if(!$modul)
{
 Import-Module ActiveDirectory
}

#Hauptfensterfenster erstellen
$Icon = [system.drawing.icon]::ExtractAssociatedIcon("$Scriptpath\icon.ico")
$Font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Regular)
$Fenster1 = New-Object System.Windows.Forms.Form
$Fenster1.Size = New-Object System.Drawing.Size(900,700)
$Fenster1.Text = "Active Directory Telefonnummernsuche 1.0.0.1"
$Fenster1.StartPosition = "CenterScreen"
$Fenster1.BackColor = "white"
$Fenster1.Icon = $Icon
$Fenster1.Font = $Font
$Fenster1.WindowState = "Normal"
$Fenster1.add_SizeChanged({Minimieren})

#Kontextmenü für Icon erstellen
$Kontext = New-Object System.Windows.Forms.ContextMenu
$KontextExit = New-Object System.Windows.Forms.MenuItem
$KontextInfo = New-Object System.Windows.Forms.MenuItem

$KontextExit.Text = "Exit"
$KontextExit.Index = 1
$KontextExit.add_Click({$NotifyIcon.Visible = $False,$Fenster1.close()})

$KontextInfo.Text = "Info"
$KontextInfo.Index = 2
$KontextInfo.add_Click({ShowInfo})

# NotifyIcon erstellen, erscheint wenn Fenster minimiert worden ist
$NotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$NotifyIcon.Icon = $Icon
$NotifyIcon.Visible = $false
$NotifyIcon.add_Click({Iconclick})
$NotifyIcon.Text = "AD Telefonnummernsuche"
$NotifyIcon.BalloonTipTitle = "AD Telefonnummernsuche"
$NotifyIcon.BalloonTipText = "Suchfenster wurde minnimiert, klick zum öffnen"
$NotifyIcon.BalloonTipIcon = "Info"
$NotifyIcon.ContextMenu = $Kontext
$NotifyIcon.ContextMenu.MenuItems.AddRange($KontextInfo)
$NotifyIcon.ContextMenu.MenuItems.AddRange($KontextExit)

#Informationsfenster anzeigen lassen, beim rechtsklick auf BenachrichtigungsIcon
function ShowInfo
{
$Fenster1.Hide()
$Fenster2 = New-Object System.Windows.Forms.Form
$Fenster2.Size = New-Object System.Drawing.Size(300,300)
$Fenster2.Text = "AD Telefonnummernsuche Info"
$Fenster2.StartPosition = "CenterScreen"
$Fenster2.BackColor = "white"
$Fenster2.Icon = $Icon
$Fenster2.Font = $Font
$Fenster2.WindowState = "Normal"

#Text1
$Text3 = New-Object System.Windows.Forms.Label
$Text3.Location = New-Object System.Drawing.Size(20,20)
$Text3.Size = New-Object System.Drawing.Size(240,150)
$Text3.Text = "AD Telefonnummernsuche

Erstellt von Andres Sichel

blog@asichel.de

Version 1.0.0.1"
$Text3.TextAlign = "MiddleCenter"
$Fenster2.Controls.Add($Text3)

$FontB = New-Object System.Drawing.Font("Arial",14,[System.Drawing.FontStyle]::Regular)
$CloseInfo = New-Object System.Windows.Forms.Button
$CloseInfo.Location = New-Object System.Drawing.Size(90,200)
$CloseInfo.Size = New-Object System.Drawing.Size(100,30)
$CloseInfo.Text = "OK"
$CloseInfo.BackColor = "#0064AF"
$CloseInfo.font = $FontB
$CloseInfo.foreColor = "White"
$CloseInfo.Add_Click({$Fenster2.Close()})
$Fenster2.Controls.Add($CloseInfo)

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

#Benachrichtigungsicon (NotifyIcon) anzeigen lassen wenn Fenster minimiert wird
function Minimieren
{
    if($Fenster1.WindowState -eq [Windows.Forms.FormWindowState]"Minimized")
    {
      $Fenster1.Hide()
      $NotifyIcon.Visible = $True      
      $NotifyIcon.ShowBalloonTip(5000)
    }       

}

#Hauptfenster anzeigen lassen wenn auf Benachrichtigungsicon geklickt wird
function IconClick
{
    if($Fenster1.Visible)
    {
        $Fenster1.Hide()
    }
    else
    {        
        $Fenster1.Show()        
        $Fenster1.WindowState = [Windows.Forms.FormWindowState]"Normal"
    }
}

#Enter drücken zulassen (nur für Benutzerabfragen)
$Fenster1.KeyPreview = $True
$Fenster1.Add_KeyDown({if ($_.KeyCode -eq "Enter"){Abfrage}})
#Mit Escape wird das Fenster wird geschlossen
$Fenster1.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Fenster1.Close()}})

# Funktion zum Abfragen erstellen
function Abfrage
{
        $Suche = $Name.Text                    
        if ($RadioNummer.Checked)
        {
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::WaitCursor 
              $Info = Get-Aduser -LDAPFilter "(telephoneNumber=*$Suche*)" -Properties telephoneNumber, department, office                          
              if ($Info -eq $null)
              {
              $Ausgabe1.text = "Nichts gefunden!"
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
              else
              {
              $Ausgabe1.text = $Info  | sort Name | Format-Table -Autosize Name, TelephoneNumber, Department, Office | Out-String
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
        }
            if (($RadioNummer.Checked) -and ($CHKMobile.Checked))
        {

              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::WaitCursor
              $Info = Get-Aduser -LDAPFilter "(|(telephoneNumber=*$Suche*)(mobile=*$Suche*))" -Properties telephoneNumber, department, office, mobile
              if ($Info -eq $null)
              {
              $Ausgabe1.text = "Nichts gefunden!"
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
              else
              {
              $Ausgabe1.text = $Info  | sort Name | Format-Table -Autosize Name, TelephoneNumber, Mobile, Department, Office | Out-String
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
        }
        if ($RadioNachname.Checked)
        {
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::WaitCursor
              $Info = Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(Sn=*$Suche*))" -Properties telephoneNumber, department, office
              if ($Info -eq $null)
              {
              $Ausgabe1.text = "Nichts gefunden!"
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
              else
              {
              $Ausgabe1.text = $Info  | sort Name | Format-Table -Autosize Name, TelephoneNumber, Department, Office | Out-String
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }   
        }
        if ($RadioVorname.Checked)
        {
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::WaitCursor
              $Info = Get-ADUser -LDAPFilter "(givenName=*$Suche*)" -Properties telephoneNumber, department, office
              if ($Info -eq $null)
              {
              $Ausgabe1.text = "Nichts gefunden!"
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
              else
              {
              $Ausgabe1.text = $Info  | sort Name | Format-Table -Autosize Name, TelephoneNumber, Department, Office | Out-String
              $Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow
              }
        }

} # Funktion Abfrage beenden

#Bild einbauen - Größe: 900 * 173
$Bild1 = New-Object System.Windows.Forms.PictureBox
$Bild1.Location = New-Object System.Drawing.Size(0,490)
$Bild1.ImageLocation = "$Scriptpath\logo.jpg"
$Bild1.SizeMode = "AutoSize"
$Fenster1.Controls.Add($Bild1)

#Text1 - Info-Text
$Text1 = New-Object System.Windows.Forms.Label
$Text1.Location = New-Object System.Drawing.Size(20,20)
$Text1.Size = New-Object System.Drawing.Size(380,40)
$Text1.Text = "Active Directory Telefonnummernsuche Version 1.0.0.1
© Andres Sichel // blog.asichel.de // blog@asichel.de" 
$Fenster1.Controls.Add($Text1)

#Text2 Eingabefeld
$Text2 = New-Object System.Windows.Forms.Label
$Text2.Location = New-Object System.Drawing.Size(20,80)
$Text2.Size = New-Object System.Drawing.Size(120,40)
$Text2.Text = "Eingabefeld:"
$Fenster1.Controls.Add($Text2)

# RadioButton für Nummernsuche
$RadioNummer = New-Object System.Windows.Forms.RadioButton
$RadioNummer.Location = New-Object System.Drawing.Size(20,120)
$RadioNummer.Size = New-Object System.Drawing.Size(350,20)
$RadioNummer.Checked = $true
$RadioNummer.Text = "Suche nach Durchwahl / Nummer"
$Fenster1.Controls.Add($RadioNummer)

#Abfrage Handy
$CHKMobile = New-Object System.Windows.Forms.CheckBox
$CHKMobile.Location = New-Object System.Drawing.Size(40,150)
$CHKMobile.Size = New-Object System.Drawing.Size(350,20)
$CHKMobile.Text = "Auch nach Mobilnummern suchen"
$Fenster1.Controls.Add($CHKMobile)

# RadioButton für Nachnamesuche
$RadioNachname = New-Object System.Windows.Forms.RadioButton
$RadioNachname.Location = New-Object System.Drawing.Size(20,180)
$RadioNachname.Size = New-Object System.Drawing.Size(350,20)
$RadioNachname.Text = "Suche nach Nachname"
$Fenster1.Controls.Add($RadioNachname)

#RadioButton für Vornamensuche
$RadioVorname = New-Object System.Windows.Forms.RadioButton
$RadioVorname.Location = New-Object System.Drawing.Size(20,210)
$RadioVorname.Size = New-Object System.Drawing.Size(350,20)
$RadioVorname.Text = "Suche nach Vorname"
$Fenster1.Controls.Add($RadioVorname)

#Eingabefeld für Namen bauen
$Name = New-Object System.Windows.Forms.Textbox
$Name.Location = New-Object System.Drawing.Size(150,80)
$Name.Size = New-Object System.Drawing.Size(300,20)
$Fenster1.Controls.Add($Name)

#Abfragebutton bauen
$FontB = New-Object System.Drawing.Font("Arial",14,[System.Drawing.FontStyle]::Regular)
$Abfrage1 = New-Object System.Windows.Forms.Button
$Abfrage1.Location = New-Object System.Drawing.Size(500,80)
$Abfrage1.Size = New-Object System.Drawing.Size(200,100)
$Abfrage1.Text = "Suchen !!!"
$Abfrage1.BackColor = "#0064AF"
$Abfrage1.font = $FontB
$Abfrage1.foreColor = "White"
$Abfrage1.Add_Click({Abfrage})
$Fenster1.Controls.Add($Abfrage1)

#Ausgabetextfeld bauen
$Ausgabe1 = New-Object System.Windows.Forms.Textbox
$Ausgabe1.Location = New-Object System.Drawing.Size(20,240)
$Ausgabe1.Size = New-Object System.Drawing.Size(840,250)
$Ausgabe1.MultiLine = $true
$Ausgabe1.ReadOnly = $true
$Ausgabe1.AutoSize = $true
$Ausgabe1.Font = "Lucida Console, 10 pt"
$Ausgabe1.ScrollBars = "Vertical", "Horizontal"
$Ausgabe1.WordWrap = $false
$Fenster1.Controls.Add($Ausgabe1)

#Fenster anzeigen lassen
[System.Windows.Forms.Application]::Run($Fenster1)
$notifyIcon.Dispose()
$Fenster1.Cursor = [System.Windows.Forms.Cursors]::Arrow