Powershell Help Desk tasks

This script will help automating some help desk tasks. For example in my current environment we put user’s name in the “Description” field. When user calls in helpdesk for a support, we need to connect to their machine. In a huge network with dynamic IPs it’s hard to maintain the list of what computer this user is using. This when you can query AD to pull required information. This script will help with the following tasks:

– query AD to pull description field for given computer account
– query all computer accounts to get list of computer, which contain requested user name
– query domain controllers and pull last connection date to AD. It uses LastLogon date, which is not replicated across all Domein Controllers. You would have to query all DCs and get the most resent once. In my example I query only two, as those are the only one used.

Script

# Created: Naz Snidanko
# Date created: October 31, 2012
# Version: 2.0
# Description: Help Desk Script
# Changes:
# version 1.0 - Oct 31, 2012 - Naz Snidanko - basic code
# version 2.0 - Dec 05, 2012 - Naz Snidanko - added menu
# version 2.1 - Dec 27, 2012 - Naz Snidanko - added LastLogon check
# import module
Import-Module ActiveDirectory
# functions
# credit: http://jdhitsolutions.com
Function Show-Menu {

Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter your menu text")]
[ValidateNotNullOrEmpty()]
[string]$Menu,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]$Title="Menu",
[switch]$ClearScreen
)

if ($ClearScreen) {Clear-Host}

#build the menu prompt
$menuPrompt=$title
#add a return
$menuprompt+="`n"
#add an underline
$menuprompt+="-"*$title.Length
$menuprompt+="`n"
#add the menu
$menuPrompt+=$menu

Read-Host -Prompt $menuprompt

} #end function

#define a menu here string
$menu=@"
1 Match user to computer
2 Match computer to user
3 Find last connection date for computer to AD
4 Reserved for future use.
Q Quit

Select a task by number or Q to quit
"@

#Keep looping and running the menu until the user selects Q (or q).
Do {
    #use a Switch construct to take action depending on what menu choice
    #is selected.
    Switch (Show-Menu $menu "My Help Desk Tasks" -clear) {
     "1" {Write-Host "Match user to computer" -ForegroundColor Green
         $const = "*"
         $strUser = read-host "Please enter user's name or part of the name:"
         $variable = $const + $strUser + $const
         echo "The following computers match results:"
         get-adcomputer -SearchBase "OU=My Computers,DC=domain,DC=local" -Filter {Description -like $variable} -Property Description | Select-Object Name, Description
         
         #pause
         Write-Host "Press any key to continue ..."
         $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
         
         }
     "2" {Write-Host "Match computer to user" -ForegroundColor Green
          $strComp = read-host "Please enter computer name to get the owner:"
          echo "The following computers match results:"
          get-adcomputer -SearchBase "OU=My Computers,DC=domain,DC=local" -Filter {Name -eq $strComp} -Property Description | Select-Object Name, Description
          
          #pause
          Write-Host "Press any key to continue ..."
          $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
          
          }
     "3" {Write-Host "Find last connection date for computer to AD" -ForegroundColor Green
         
          $strComp = read-host "Please enter computer name to get last connection date"
          
          if (get-adcomputer -Identity $strComp)
          {
          #query DCs
          [long]$strLastLogonDC2 = (Get-ADComputer -Identity $strComp -Server Companydc02.domain.local -properties *).LastLogon
          [long]$strLastLogonDC1 = (Get-ADComputer -Identity $strComp -Server Companydc01.domain.local -properties *).LastLogon

          #logic to compare dates and display result
          if ( $strLastLogonDC1 -gt $strLastLogonDC2)
          { 
            echo "Domain Controller: Companydc1"
            echo ([datetime]::FromFileTime($strLastLogonDC1))
          }
          elseif ( $strLastLogonDC2 -gt $strLastLogonDC1)
          { 
            echo "Domain Controller: Companydc02"
            echo ([datetime]::FromFileTime($strLastLogonDC2))
          }
          else { echo "Error." }
          }
          
          #pause
          Write-Host "Press any key to continue ..."
          $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
         }
     "4" {Write-Host "Reserved for future use" -ForegroundColor Magenta
         sleep -seconds 2
         }
     "Q" {Write-Host "Goodbye" -ForegroundColor Cyan
         Return
         }
     Default {Write-Warning "Invalid Choice. Try again."
              sleep -milliseconds 750}
    } #switch
} While ($True)

Cheers!

This entry was posted in Powershell and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *