gygon

PowerShell Ping Test

Posted by gygon on Wednesday, November 16th, 2011

Used this little script the other day for a basic network test to an access point I was testing. It just pings the device every 10 seconds (or whatever you set it at) and only outputs when there is an error and the time it occurred. Made for an easy way to leave this running over [...]

continue reading

Using PowerShell to monitor a web server

Posted by gygon on Monday, November 14th, 2011

A couple years ago I posted about a PowerShell script that would read in a web page. I used that to setup a basic web site test to go through a list of web sites/servers and make sure they were up and running and email an alert if they were down or the size of [...]

continue reading

Monitoring the Event Log with PowerShell

Posted by gygon on Saturday, March 21st, 2009

One of my goals with learning more about PowerShell is to be able to monitor the event logs on servers and notify me via email when certain events happen. The system I’m looking to monitor are not part of a domain, are in remote locations on isolated networks. Some of the main things I’m looking [...]

continue reading

QuickBooks Enterprise Business Planner Unable to Print

Friday, December 2nd, 2011 - Tech, Tips

Recently had someone try using the Business Planner tool in QuickBooks (Enterprise v11) and after entering all of the info, ran into an issue with the preview button being greyed out and unable to view the report. After following the instructions on the Intuit knowledge base article, the preview button was now visable but when clicking it, a Printer Not Activated error -30 would appear.

Several phone calls with Intuit support later, and numerous printer re-installs, they determined this is a bug. Version 12 was recently released but we haven’t upgraded yet…so I installed it on a test system and tried again, same issue.

So if you’re having issues trying to view the business planner…save you time and skip it for now.

PowerShell Ping Test

Wednesday, November 16th, 2011 - PowerShell, Tips

Used this little script the other day for a basic network test to an access point I was testing. It just pings the device every 10 seconds (or whatever you set it at) and only outputs when there is an error and the time it occurred. Made for an easy way to leave this running over the weekend and see any time that the device went offline.

PingTest.PS1

function Test-Ping
 {
 param($ip)
 trap {$false; continue}
 $timeout = 1000
 $object = New-Object system.Net.NetworkInformation.Ping
 (($object.Send($ip, $timeout)).Status -eq 'Success')
 }

$killswitch=1
 Write-Host "Running ping test to 192.168.10.9 every 10 seconds. Logs errros to screen. Press <CTRL> C to stop." -Fo Cyan
while ($killswitch -ne 0) {

If (!(Test-Ping 192.168.111.5)) {
 Write-Host 'Lost connectivity at: ' $(Get-Date -format "dd-MM-yyyy @ hh:mm:ss") -Fo Red
 }
 Else {
 Start-Sleep 10
 }
}

Using PowerShell to monitor a web server

Monday, November 14th, 2011 - Featured, PowerShell, Tech

A couple years ago I posted about a PowerShell script that would read in a web page. I used that to setup a basic web site test to go through a list of web sites/servers and make sure they were up and running and email an alert if they were down or the size of the returned page had changed indicating something had changed. It came up recently and figured it would be easier to post here for future reference.

This can definitely be cleaned up and made more versatile but it does what I need it to do and the only changes I’ve made over the last two years has been to adjust the size checks of the websites as changes were made.

#-----------------------------------------------------------
#
# WebSiteCheck.ps1
#
# Checks websites to ensure they are within the size range for the specified page.
# If site size is out of range, email alert is sent.
#
# Usage: ./WebSiteCheck.ps1 (-debug)
#   ex:  ./websitecheck.ps1
#   ex:  ./websitecheck.ps1 -debug
#
#-----------------------------------------------------------

#------------------------------------------------------
Param([switch]$debug)

if ($debug) { $DebugPreference = " continue" }
write-debug "Starting website check now..."
#------------------------------------------------------

function Get-Web($url,
    [switch]$self,
    $credential,
    $toFile,
    [switch]$bytes)
{
    #.Synopsis
    #    Downloads a file from the web
    #.Description
    #    Uses System.Net.Webclient (not the browser) to download data
    #    from the web.
    #.Parameter self
    #    Uses the default credentials when downloading that page (for downloading intranet pages)
    #.Parameter credential
    #    The credentials to use to download the web data
    #.Parameter url
    #    The page to download (e.g. www.msn.com)
    #.Parameter toFile
    #    The file to save the web data to
    #.Parameter bytes
    #    Download the data as bytes
    #.Example
    #    # Downloads www.live.com and outputs it as a string
    #    Get-Web http://www.live.com/
    #.Example
    #    # Downloads www.live.com and saves it to a file
    #    Get-Web http://wwww.msn.com/ -toFile www.msn.com.html
    $webclient = New-Object Net.Webclient
    if ($credential) {
        $webClient.Credential = $credential
    }
    if ($self) {
        $webClient.UseDefaultCredentials = $true
    }
    if ($toFile) {
        if (-not "$toFile".Contains(":")) {
            $toFile = Join-Path $pwd $toFile
        }
        $webClient.DownloadFile($url, $toFile)
    } else {
        if ($bytes) {
            $webClient.DownloadData($url)
        } else {
            $webClient.DownloadString($url)
        }
    }
}

function SendEmail($SendTo,$SendSubject,$SendMessage)
{
	$SmtpClient = new-object system.net.mail.smtpClient
	$MailMessage = New-Object system.net.mail.mailmessage
	$SmtpClient.Host = "SMTP.YOURDOMAIN.COM"
	$FromAddress = new-object System.Net.Mail.MailAddress("MyEmail@YOURDOMAIN.com", "YOUR NAME")
	$mailmessage.sender = $FromAddress
	$mailmessage.from = $FromAddress
	$mailmessage.To.add($SendTo)

	$mailmessage.Subject = $SendSubject
	$mailmessage.IsBodyHtml = 1

	$mailmessage.Body = $SendMessage

	$Credentials = new-object System.Net.networkCredential
	$Credentials.UserName = "USERNAME@YOURDOMAIN.com"
	$Credentials.Password = "YOURPASSWORD"
	$SMTPClient.Credentials = $Credentials
	$SMTPClient.Port = 25
	$smtpclient.Send($mailmessage)
}

# Set who the email goes to and what the subject will be.
$sendtoemail = 'support@YOURDOMAIN.com'
$BaseSubject = 'Website Check: '

# Uncomment the line below to output the retrieved web page to the screen
# write $webpagecall.length
# write $webpagecall

########### Site 1 ##################
write-debug 'Checking Site 1'
$webpagecall = Get-Web 'https://www.site1.com/subfolder/ASP/pagetocheck.asp'
write-debug $webpagecall.length
if (($webpagecall.length -lt 15948) -or ($webpagecall.length -gt 15948)) {
   write-debug 'out of range'
   write-debug $webpagecall.length
   $SendSubject = $BaseSubject + 'SITE 1'
   SendEmail $sendtoemail $SendSubject $webpagecall
 }

########### Site 2 ##################
write-debug 'Checking Site 2'
$webpagecall = Get-Web 'https://www.site2.com'
write-debug $webpagecall.length
if (($webpagecall.length -lt 12428) -or ($webpagecall.length -gt 13455)) {
   write-debug 'out of range'
   write-debug $webpagecall.length
   $SendSubject = $BaseSubject + 'SITE 2'
   SendEmail $sendtoemail $SendSubject $webpagecall
 }

########### Site 3 ##################
write-debug 'Checking Site 3'
$webpagecall = Get-Web 'https://www.site3.com'
write-debug $webpagecall.length
if (($webpagecall.length -lt 11000) -or ($webpagecall.length -gt 11005)) {
   write-debug 'out of range'
   write-debug $webpagecall.length
   $SendSubject = $BaseSubject + 'SITE 3'
   SendEmail $sendtoemail $SendSubject $webpagecall
 }

write-debug 'Finished running checks.'

A few notes:

  • Be sure to change the email credentials in the SendEmail function.
  • The Site 1, 2, 3 can be reduced to a single site or have however many sites you want to check added on. Just edit the info for the site you want to check.
  • I left all the size checks as a range as some of the sites I check vary slightly (ex: current date/time) so this allows a range for them to still pass the check and not throw false alarms.
The way I use the script is on a server that has it scheduled to run every 10 minutes. This way if a site goes down I find out pretty quick.

Recover Offline Files and Folders for Vista / Win 7

Friday, November 4th, 2011 - Tech, Tips, Windows

After moving over several systems from an old SBS2003 domain to a new SBS2011 Essentials server, one user noticed that there were several files missing from their documents. To be more accurate, the files were there but they were very old versions of the file, not the current one they had been working on.

After checking the server and the workstation, no other versions of the file were found. Since the old domain used to redirect the “My Documents” folder to the server and use Offline Files, I figured the current version was probably in the offline files on the computer and had never re-synced with the server when the migration was done.

Getting access to this was easy using a Ubuntu live CD to boot up Ubuntu without installing on the workstation. This gives you access to the drive…you can then go to the \Windows\CSC folder and drill down to the domain & username you want to get the files for. Then just copy the files out to another folder on the drive. Reboot back into Windows and you have access to all the files from the Offline files cache.

Set default printer with batch file script

Thursday, November 3rd, 2011 - Tech, Tips, Windows

For some reason QuickBooks seems to like changing the default printer on some workstations. Haven’t found the cause yet but decided to put in this work-around to make everyone happy. Added this to a login batch file that runs on the system to reset the default printer for each local computer:


rundll32 printui.dll,PrintUIEntry /y /q /n “Printer name”

Note that the PrintUIEntry appears to be case sensitive as it was giving me errors when I had it all lower case. Obviously change Printer Name to the actual name of the printer.

QuickBooks Enterprise Scheduled Backups

Wednesday, April 20th, 2011 - Tech, Tips

Recently upgraded to QuickBooks Enterprise 2011 and after setting up the scheduled backups, noticed they weren’t running. Manually backups ran fine but scheduled backups would just create a file in the backup folder called QBTempBackup.tmp followed by the date. After doing some digging around online I found out there’s a QB Backup log file that is located in C:\Users\<username>\AppData\Local\Intuit\QuickBooks\Log\QBBackup.txt Looking in this log, the only error showed Bad Connection Handle used when it tried to run the scheduled backup.

After giving up trying to find a solution online, called support and after some poking around they found the issue. It’s a simple fix that would have saved a lot of time if it was documented somewhere. The local user QBDataServiceUser21 needed to be added to the Shares permissions for the folder with the QB company files. Once the user was added with permissions, the scheduled backups ran without problem.

Running app from Mapped Drive – Avoiding UAC Prompts

Wednesday, March 2nd, 2011 - Tech, Tips, Windows

While reinstalling a Windows Vista system the other day I ran into a problem with the UAC security prompt. The primary applicaiton that is run on this computer is not installed locally but run directly from a mapped drive. Normally the user just clicks on the shortcut and the app launches but after a clean install and all updates I was now getting prompted to enter credentials to allow the app to run. Since the user isn’t an admin and I don’t want them to have admin rights, this was an issue. I knew it can be set as I’d done it before but took a little bit to figure out the workaround I’d used. So for when I forget again, here’s the workaround used:

I setup a batch file (see below) that first checks that the drive is mapped, if not, it will map the drive. Then it will launch the program. This part I had done but was still getting the error. The trick I forgot was that the batch file needs to be placed into a “secure location” in order to run without the elevation request. The three locations that I know of that are considered secure are

  • \Program Files (including subfolders)
  • \Windows\system32
  • \Program Files (x86)\  (including subfolders for 64-bit versions of windows)

Once the file was setup here, I no longer get the security prompts. So the users are happy they don’t get the UAC prompts and I’m happy that I don’t need to give out the admin rights to run apps.

Here’s a copy of the basic batch file used:

If NOT exist R:\somefile.exe (
  net use R: /delete
  net use R: \\1.2.3.4\SharedFolder /user:USERNAME PASSWORD /persistent:no
)
Start /D R: somefile.exe
exit

A reference link on Microsoft Technet: http://technet.microsoft.com/en-us/library/dd835564(WS.10).aspx#BKMK_ElevateUIAccessApps

Exchange 440 Login Timeout

Friday, February 25th, 2011 - Tech, Windows

Had an issue with an old Exchange 2003 server the other day….in the middle of the day users were disconnected in Outlook (using RPC over HTTPS) and when trying to log into the Exchange web page to view mail, users received the error “440 Login Timeout”.

Did the generic solve-all of rebooting the server but that didn’t help…then after looking around I found this article and followed the steps which fixed the problem. http://support.microsoft.com/kb/917686

In my case it was step 2 that fixed the problem, updating the folder permissions in IIS. I’m still not sure how/why the permissions changed, especially since it did it in the middle of the day and not while making changes on the server.

Moving to Google Aps…testing

Thursday, December 30th, 2010 - Google, Tech

It’s about time to replace the aging Exchange 2003 box. Rather than running my own system I’ve been looking at making the change over to Google Aps. The main reason for this is to eliminate one more system that I have to deal with as well as eliminate the 3rd party mail filter and just have everything in one. Being frugal I’m looking at the free/standard edition so that means giving up Outlook…the only thing that I’ve been worried about. I set it up the other week as a trial and have been using both just to get a better idea of how it will work and if there’s any issues. Here’s what I’ve found so far…

I like it a lot. The spam filtering works great and after getting used to Labels vs Folders I like it a lot. The interface on my phone and new iPad are great as well. The one thing I’ve run into that I do now and then and need to look into is pasting images into emails. I tend to take snapshots of a report on my screen and email it out to several people. This is no problem in Outlook and it just shows up in the email…not sure I can do that in Gmail without attaching the image…have to look into that one some more.

There’s still a few things I need to test out, like the ScanSnap document scanner I use daily…currently it allows scanning and attaching a PDF directly to an email. Not sure I can set this up to work with Gmail…it would mean having to always save the file, then attach and send an email. Would work but it adds a few steps for something I do a lot.

Another quirk is vieiwing Excel attachments. I get these a lot also…when I use the link to view as HTML in GMail, none of the data shows up so that’s useless. I can view as a Google spreadsheet which works fine except the images/graphs don’t show up. So looks like there’s an extra step there to download then view. Again, not a major issue, just makes things a little slower.

On the plus side, opening Gmail in the browser definitly saves time compared to opening Outlook…usually took a while to open and sync new messages from Exchange so even though I’m losing some time with certain steps, I do gain some just opening mail every day!

Copy file and append date and time

Thursday, December 9th, 2010 - Tech, Tips, Windows

Recently set this up for someone and then had someone else looking to do the same thing about a week later. Figured it would be good to post it here for future reference.

The first example adds just the date:


@echo off
REM *****************************************************
REM **  DatedBackup.bat
REM **  This batch file will make a backup copy of
REM **  the file "example.xlsx" and append the current
REM **  date in YYYYMMDD format to the current directory
REM **
REM *****************************************************
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
copy "example.xls" example_%All%.xls
exit

The second example adds both the date and timestamp:


@echo off
REM *****************************************************
REM **  DatedBackup.bat
REM **  This batch file will make a backup copy of
REM **  the file "examplefile.pdf" and append the current
REM **  date (in YYYYMMDD format) and timestamp
REM **  to the current directory
REM **
REM *****************************************************
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
copy "examplefile.pdf" examplefile_%All%_%Allm%%AMPM%.pdf
exit

Some reference:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true

http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-usi