Powershell and SQL: Alert for users stuck in Winscribe database

I’m slowly learning about SQL, and I’ve found that there are some things in our databases that I would like to check on automatically with Powershell. The lowest hanging fruit was to check for users that are stuck in Winscribe’s database. Because we have concurrent user licensing, stuck users cause us headaches. If a zombie user is considered “online” to Winscribe’s licensing, it’s preventing someone else from working when we hit our upper limit.

I’m not really sure what causes users to get stuck, but my theory is that the program (Winscribe Client) may have been improperly shut down and didn’t get a chance to release the license. This could go unnoticed until we start getting complaints that users can’t get in the system due to depleted licenses. The stuck user may not even be able to log in until their account is released, but it’s not a problem to them if they infrequently use the program.

The best solution is to be proactive with a little Powershell script that alerts you after doing a simple SQL query and parsing the results:

Read more »

Automating Avaya: Live Web Reporting via Powershell

Avaya Web Report

Avaya Web ReportPowershell is not only great for working with CSV files, it can also be used to output HTML. If you have a large number of users that need to look at the same live report, you can share this over an IIS web page instead of maxing out the number of simultaneous users in Avaya Site Administration. Using the same virtual machine I use for the e-mailed CSV reports, I scheduled additional tasks in Avaya Site Administration every 30 seconds to pull a report. The reports are not CSV standard and need to be processed and then formatted with HTML tables. Most of the information on using HTML in Powershell I pulled from the examples here on Microsoft’s site. You will need two scheduled tasks in Windows to process the report, with one using a 30 second delay.
Read more »

Scheduling a PowerShell Script

I have received various questions and comments on here (and elsewhere) asking about some PowerShell basics. This post is meant to help with one of those questions. I’ll have more posts like this as new things come to mind. Much of this information is available elsewhere on the internet, but if you’re already coming here I’ll give you some of the ones that were most important to me.

Scheduling A PowerShell Script

Note: This example uses Windows 2008 R2 as the operating system.

  • Start -> Administrative Tools -> Task Scheduler
  • Select “Task Scheduler Library”.
  • On the right hand side of the MMC, click “Create Task…”
  • You will now see the General tab:
  • Name your task something clear, like “VMware – Snapshot Alert”, also add a description if helpful.
  • I strongly suggest creating a service account to run scripts (helps prevent everything breaking when you change your password) – note that you’ll need to ensure this service account has all the necessary permissions to run all of the command in the script. For a script like the Snapshot Alert, you need to make sure the account has access to vCenter.
  • Click “Run whether user is logged on or not” and “Run with highest privileges”.
  • Select the “Triggers” tab. Click “New”.
  • Most likely you’ll want the script running on a daily schedule (but totally subjective to the script’s function). Choose “Daily” then set the time you’d like it to run each day. If you want to run this script only on weekdays, choose “Weekly” and check the days that you want. If you have an alert script like this you may not care to receive things on Saturday and Sunday.
  • As a safety measure I tell the task to stop if it runs longer than 1 hour. The script only takes a couple of minutes to run so this should never happen.
  • Make sure “Enabled” is checked (it should be, by default), then click OK.
  • Select the “Actions” tab. Here’s where we fire off the script. Click “New”.
  • The Action is “Start a Program”. Go ahead and put in the full path to the PowerShell Executable, not the script. On most systems this should be “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” (even if you have 2.0).
  • Under “Add Arguments” we’ll put the path to the script. In my case it was this: “C:\ScriptCenter\VMware\SnapshotAlert\SnapShotAlert.ps1″. NOTE: I have had numerous problems when there is a space in the path of a script, even with quotation marks. Try to store scripts in a path without spaces, especially in the script name itself!
  • Unless you script needs to run in a different directory, I suggest making “Start in” the same path as your “Add Arguments” field, minus the .ps1 file obviously.
  • Click OK.
  • At this point you can click OK again and you should be done. You script should now be scheduled and ready.
  • If you want to test it out, right-click and select “Run” (I hope you tested this script before hand!)
  • You’ll need to keep refreshing the view, but once the script completes, you should receive “The operation completed successfully. (0×0)”. Anything other than that indicates an error you will need to investigate.
That’s all for today!


Back from the Dead

So things have finally calmed down enough at work that I can give some more attention to the blog again. I’ve been writing quite a number of knick knack scripts as of late so I’ll start sharing those on the previous Monday/Wednesday schedule (with Joseph usually rounding out the week on Friday).

One especially cool thing I’ve been working on lately has been Nagios. If you aren’t familiar with Nagios, it’s an open source monitoring tool (Linux-based) that can monitor practically anything. Put more simply – if you can write a script to retrieve data from something, Nagios can query that script and monitor, trend, and alert on it. I bet you can see why it might be a worthwhile topic to include on this blog!

Anyway, this Wednesday I’ll share some of my general PowerShell how-to tips that have been prompted by comments in various other posts. Hopefully I can compile them in one “go to” post for anyone’s future reference.

Automating Avaya: E-Mail Scheduled CSV Reports

This will be the first in a series of posts on how to save time with Avaya phone systems. I’ll be leveraging the Windows client, Avaya Site Administration, which merely gives a nice GUI to the terminal commands being used in the background. Thankfully this client program uses safe API calls that AutoIt can easily handle, which enabled me to build a web interface for users wanting to turn on and off their voicemail. But, there are also some things I’ve done with Powershell in conjunction with the built-in reporting feature, which I will be covering first after the jump.

Read more »

Using AutoIt: Scrape Website via IE and Send Alerts

Some of you out there may have experienced a stubborn vendor that uses a proprietary ticketing system without an RSS feed or proper email response system. Why not leverage AutoIt? It can check their web-based ticketing system for you, multiple times a day (via scheduled task) and email your users that they have an updated ticket waiting for a response.

Unless you’re also a customer of the same system, you can’t really use this directly (I’m not pointing any fingers)… but you might find this code useful as a starting point for doing something similar.

This is what part of the table looks like on their web based ticketing system:

Number  Contact  Subject  Status  Created Date 
1623501 Billy Jason Issue 4731 ASSIGNED 2/16/2012
4000699 John Doe Issue 5156 ASSIGNED 2/15/2012
2431126 John Doe Issue 9898 IN PROGRESS 2/1/2012
1868059 Bobby Bob Issue 3526 RESOLVED-WAITING ON CLIENT 2/16/2012
6457699 Bobby Bob Issue 2045 IN PROGRESS 2/16/2012
5116024 Mary Don Issue 8392 IN PROGRESS 2/16/2012
1391800 Debbie Jean Issue 1288 IN PROGRESS 2/2/2012

We have AutoIt login via POST and pull up the ticket list (only certain users have access to everyone’s tickets), then scrape the table into an array. The script looks for a line with a status of WAITING ON CLIENT and emails that ticket’s contact their ticket is awaiting a response:

#include <IE.au3> ; Required to interact with Internet Explorer
#include <Array.au3> ; Required for _ArrayDisplay() function if debugging needed
#include <INet.au3> ; Required for emailing

;Change to fit your environment
$s_SmtpServer = "Contoso-SMTPServer"
$s_FromName = "VendorTicketSys alert"
$s_FromAddress = "VendorTicketSys@contoso.com"
Dim $as_Body[1]

; Login via POST method
$loginIE = _IECreate('https://VendorSite.VendorWebsite.com/1234/login.asp?USERNAME=YYYYYY&PASSWORD=XXXXX')

; Open new window with info I need
$oIE = _IECreate("https://VendorSite.VendorWebsite.com/1234/5678/VendorTicketSys/VendorTicketSys.asp")

; 2nd table on page contains every ticket status... if not, then alert me something failed!
$oTable = _IETableGetCollection ($oIE, 2)
If @error Then
	$s_Subject = "VendorTicketSys alert failed"
	$s_ToAddress = "itguy@contoso.com"
	$as_Body[0] = "VendorTicketSys alarm failed to login to VendorSite to check ticket status. Please check for password expiration."
	$Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @computername, -1)
	$err = @error
	Exit
EndIf

; Dump table into 2D array
$aTableData = _IETableWriteToArray ($oTable, True)

; Debugging helper
;_ArrayDisplay($aTableData)

For $i = 1 to (UBound($aTableData) - 1)
	;ConsoleWrite("STATUS: " & $aTableData[$i][5] & @CRLF)
	If StringRight($aTableData[$i][5],17) = "WAITING ON CLIENT" Then
		;ConsoleWrite("Detected response for " & $aTableData[$i][1] & @CRLF)
		; Below could load from a text file if a lot of people want this alert...
		Select
			Case $aTableData[$i][1] = "John Doe"
				_EmailTicketSys($aTableData[$i][0],$aTableData[$i][1],$aTableData[$i][4],$aTableData[$i][5],"jdoe@contoso.com")
			Case $aTableData[$i][1] = "Bobby Bob"
				_EmailTicketSys($aTableData[$i][0],$aTableData[$i][1],$aTableData[$i][4],$aTableData[$i][5],"bobbybob@contoso.com")
		EndSelect
	EndIf
Next

; Sometimes closing the login window worked, but it's safer to leave it open while scraping table
_IEQuit($loginIE)
_IEQuit($oIE)

; Send email for detected reponse
Func _EmailTicketSys($ticketID,$who,$subject,$status,$sendto)
	$s_Subject = $ticketID & " - " & $subject & " - " & $status & " - VendorTicketSys alert"
	$s_ToAddress = $sendto
	$as_Body[0] = $who & " has received a response from VendorSite." & @CRLF & @CRLF & "ID: " & $ticketID & @CRLF & "Subject: " & _
	$subject & @CRLF & "Status: " & $status & @CRLF & @CRLF & "Please login to VendorTicketSys Web Only Site to respond to the ticket. This email alert service is provided by the Contoso IT Department, not by VendorSite."
	$Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @computername, -1)
	$err = @error
EndFun

Our users have been quite happy with the alerts they’re finally getting, which enables the issue to be resolved much quicker than before. It’s hard to balance this between notifying the end users too much and not enough. The task only runs twice a day, morning and afternoon, so if the user ignores the alert, they will get alerts until they finally respond to the ticket. Sometimes the vendor will respond after the morning alert, so we won’t notice until the end of the day. It would be nice if I could detect whether or not the user has been alerted about that particular ticket or not, so I could check more often without bugging the users. But, an alert about the response within 24 hours should be better than no alert at all.

Echo! Echo! Echo!

Wow, neither Joseph or I have updated since October! First off we are still alive and well. I know he has many articles yet to share, but I think he was waiting for me to get off of my butt and produce more content. I’ve been one of the main role players in a major disaster recovery project at work, so that has been chewing up most of my time. I have been writing plenty of scripts along the way, but many are so specific that I haven’t taken the time to sterilize them of company info and upload here. They are coming!

I’ve also receive some questions on a couple of PowerCLI scripts, I will be answering those in the comments section (here and here).

Using AutoIt: Hotkey for IT Phone Support (Help users help you)

Computer Name, IP Address, User Currently Logged In, VNC status, Connection to NetworkMany times when a user calls IT, they aren’t even sure where they are in a building, let alone the “Computer Name” we ask for every time. I wrote a program that reduces a game of 20+ questions to maybe 5. If you can get the user to press a keyboard combination, then hopefully they can read to you the information shown on the screen. Everything is color coded to indicate problems (green is good, red is bad).

Over the years, my little script been extremely helpful in identifying networking issues, problems with the remote assistance software not running, or simply finding out what their computer name or IP address is so I can connect and assist. It’s also extremely helpful if I’m not near a computer to assist them, but they can read to me basic information that I need to solve the problem. “What was that last part? Network connection failed? I think I know why you can’t log in to X.” Or maybe, “You’re logged in as X? You need to be logged in as Y to do that.” It’s a good “bird’s eye view” of the situation.

Source code after the jump:
Read more »

Quick Exchange script to export mail from one mailbox to another

The following script may seem really simple… and that’s because it is. However it still seems that some folks either don’t know how to do this (or at least how to script it to make it fast and easy), so I figured I’d post it in one more place on the internet for a future person in need to stumble upon.

Assumptions: For Exchange 2007, this script must be run on a server with PowerShell 2.0, Exchange Administration Tools 32-bit installed, and 32-bit Outlook. For Exchange 2010 it’s the opposite, all 64-bit, but I have not tested this with Exchange 2010 yet (if you do, let me know if it still works!)

Synopsis: This script will give the person running it full access to the source and destination mailbox, perform the export, then remove the full access permissions. You must already have Exchange administration privileges for it to work!

# Mailbox Export Script
# Authors: David Beasley
# Updated: 10/18/2011

###############
# VARIABLES #
###############

$mailboxToExport = "dviesel@domain.com" # Which mailbox are you exporting mail FROM?
$mailboxToReceive = "wpalker@domain.com" # Which mailbox are you sending the mail TO?
$startDate = "10/06/2011" # Mail on this date and AFTER will be exported
$targetFolder = "Mail_Import" # All mail will be exported to a new Top-Level folder in Outlook with this name

#############
# ACTIONS #
#############

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin,Microsoft.Exchange.Management.Powershell.Support -ErrorAction SilentlyContinue

Get-Mailbox $mailboxToExport | Add-MailboxPermission -User $env:username -AccessRights "FullAccess" | Out-Null
Get-Mailbox $mailboxToReceive | Add-MailboxPermission -User $env:username -AccessRights "FullAccess" | Out-Null

Export-Mailbox -Identity $mailboxToExport -TargetMailbox $mailboxToReceive -StartDate $startDate -TargetFolder $targetFolder -Confirm:$false

Get-Mailbox $mailboxToExport | Remove-MailboxPermission -User $env:username -AccessRights "FullAccess" -Confirm:$false | Out-Null
Get-Mailbox $mailboxToReceive | Remove-MailboxPermission -User $env:username -AccessRights "FullAccess" -Confirm:$false | Out-Null

Read-Host "Hit enter to Quit..."

Using AutoIt: Analyzing slow boot time in Windows XP

Our users were complaining of slow boot time, especially during “Applying Computer Settings”. I researched on what might be going on during this process, and found that you can turn up the level of information recorded to your userenv.log to see more of what the OS is “thinking” during startup. I looked for tools to analyze this output, but didn’t find anything immediately useful (or free). I started to analyze it with Excel after using Notepad++ to convert it to a CSV, but Excel didn’t seem like the right tool for the job.

I thought that replaying the events of the log file as if it were happening would help me better visualize the problem areas. So, I wrote an AutoIt script to display each line of the log, but also analyze the time between each event and pause artificially. The script will also highlight events pausing longer than 1 second.

Full source code after the jump:

Read more »