Monday, October 13, 2008

Powershell: Process contents of Directory

Hello!  I know it has been a while since I posted any thing.  I have been busy working on a lot of different projects and I picked up a second job so that has been keeping me busy as well.  

Todays post is mostly some powershell code to process Microsoft Document Imaging Files (.mdi or .tif) and converting them to PDF's using a program called MDI2PDF from Bugysoft.  

Part of our specific problem is these files are really Microsoft Document Imaging Files but they have the .tif file extenstion.  We wrote a PHP script to process all of our uploads from the one client that sends us these files and based on some tests we put the MDI Files into a specific directory for processing.

And now on to the Code.  Lines that start with a # are comments for explaining what each line / section does.

#Setting up variables for our use
         $input = "y:\allAuths\problemAuths\"
         $output = "y:\allAuths\processAuths\"
         $convert = "C:\Program Files\MDIConvertor\MDI2PDF.exe"

#Get the current number of errors present in the global $error variable.
     $olderrorcount = $error.count

#Check to see if MDI2PDF is running. If it is this script will fail, so we need to kill it.
       $proclist = get-process mdi2pdf -ea silentlycontinue

#If the number of errors is the same as is used to be then we can kill the running process(es).  Otherwise we have nothing to kill and continue on.
    if ($error.couint -eq $olderrorcount) {$proclist | stop-process}

#Here we get the contents of the input directory filtered for the type of file we want to convert.
      $filelist = get-childitem -Path $input -Filter *.tif

#For every file in our file list we need to run the following command in order from top to bottom.
     foreach ($file in $filelist)
     {
#Replaces the .tif with .pdf.
          $filename=$file -replace ($file.extension + "`$"),".pdf"

#Write something to the screen so the user knows something is happening.
       write-host "Converting $input$file"

#Look ma! Simple string concatenation!!!
#Roughly equal to &"C:\program files\mdiconvertor\mdi2pdf.exe export:y:\allauths\problemauths\.tif y:\allauths\processauths\.tif.pdf
         &$convert "export:$input$file $output$filename"

#get the process info for the currently running mdi2pdf process.
     $convertprocess = get-process mdi2pdf
#Wait for the process to exit before moving on to the next one. I used to use sleep 10 here but this is much better.
      $convertprocess.waitforexit()

#We need to check and make sure the converted file exists. If it does exist then we need to delete the original file.
    #variable to store the input filename. We will need this later to delete the file.
    $inputfile = $file.fullname
    #Variable to store the output filename and full path.  We use this later to check for its existance.
    $outputfile = $output + $filename
            #If the result of the path test is true we remove the file.
            if (test-path $outputfile)
            {remove-item $inputfile}
    }


Wednesday, September 17, 2008

Excel Convert Multiple Cells to URLs

This will convert the selected cell range to a URL.  It assumes that the selected cells are vaild URLs but will skip empty cells.  

Sub Convert2URL()
    For Each cell In Selection
        If cell.Value <> "" Then
            If Left(cell.Value, 7) = "http://" Then
                URL = cell.Value
            Else
                URL = "http://" + cell.Value
            End If
            ActiveSheet.Hyperlinks.Add Anchor:=cell, _
              Address:=URL, TextToDisplay:=cell.Value
        End If
    Next cell
End Sub

Now lets say you have a list of order numbers and you want to click a link in Excel and go right to your order management screen you could do:

Sub Convert2URL()
    For Each cell In Selection
        If cell.Value <> "" Then
            If Left(cell.Value, 44) = "http://myordermanager.net/ordermanager.php?" Then
                URL = cell.Value
            Else
                URL = "http://myordermanager.net/ordermanager.php?" + cell.Value
            End If
            ActiveSheet.Hyperlinks.Add Anchor:=cell, _
              Address:=URL, TextToDisplay:=cell.Value
        End If
    Next cell
End Sub

Yup, its neat-o and works like a charm!

Excel Time Difference Formula

Yeah, I know this is all round the great interweb but I don't care.  I want to post it here too so I KNOW where it is.  This blog is not just to help someone else after I found a solution.  I often come back here myself to find solutions I have used in the past.


=IF(D2>H2,B2+1-D2,H2-D2)

D2 = Start Time
H2 = End Time

Yeah, it look complicated but it works wonderfully.  It doesn't take into account dates, so it only works from one day to the next if going over night.

Wednesday, August 13, 2008

Firefox 3: places.sql to bookmarks.html conversion

Sometimes, just sometimes you jump the gun and upgrade too soon. Well as it turns out I did that when I first started my new job. Our home grown web app renders perfectly in Firefox 2 but looks odd in Firefox 3. Also one of the plugins we need to run is very unstable in Firefox 3 so I had to revert back.

If I had known this a month ago and not started accumulating bookmarks all over the place I would have been fine, but that was not the case. The main problem is that Firefox 3 uses a new places.sqlite SQL database to keep track of bookmarks instead of the tried and true bookmarks.html. I went online looking for some sort of conversion program, thought about making my own (which I still might do just as a proof of concept / fun project.)

So anyway, here is how to have Firefox 3 save a bookmarks.html file in addition to the places.sqlite. Open a new tab and go to about:config. Click "Ok" when it asks if you will be careful. Now browse down to "browser.bookmarks.autoExportHTML" and change this from the default of "false" to "true". Restart Firefox and you will have your bookmarks.html file updated as you add new bookmarks.

If I ever do create a conversion program I will post the link on this site.

yum install imagemagick

Today's blog is going to be quick.

For those linux users out there that don't have imagemagick already installed the correct syntax is:
yum install ImageMagick
NOT yum install imagemagick

To find out if you already have imagemagick just type convert -version from the command line.

As it turns out I was trying to install something I already had thanks to CentOS.

Tuesday, August 12, 2008

Clonezilla

I have seen Symantec Ghost used in the past. I have always wanted a scenario where using something like Ghost would make sense and today I have found it!

We have a great many machines here that are identical. They do the same function, have the same hardware, and need the same software. That being said I have finally found a reason to really need a solution like Symantec Ghost. Now being on a very tight budget I was looking for a different solution and thanks to the generous folks over on the Spiceworks fourms I found Clonezilla.

Now there is a server based version that I could use to Clonecast (I don't know if that is a real word or even something they use but I like it) images out to machines, but for now I am booting off of my 2GB USB key and saving / reading images from a seperate 160GB USB drive.

It takes about 15 to 20 minutes do drop an image from the USB-Hard drive to the client machine (for around 1GB of info). It takes just around as long do put the image on the USB-HDD which is very quick if you ask me. Most of the time is getting the computer to boot off of the USB key and configure the options you want.

One of my projects that I hope to do later is to install the NTFS 3g drivers for Linux so I can modify the file system once the image is installed. This will enable me to do things like rename the PC based on it's MAC address. Also I may get a Clonezilla server up and running. If I do you can be sure I will post something about it.

Monday, August 11, 2008

New Job

Well I haven't posted in a while for many reasons.
For one I recently took a new position with a new company. It was sort of a lateral move, but sort of a downgrade. I went from being THE Systems Administrator to being more of a help desk tech. It was a move up the pay scale, which is nice and with less responcibility it's an added bonus, but I do miss some of the Sys Admin tasks I used to preform.

Also the new company uses Linux primarly and uses hosted managed servers, so there really isn't that much for me to do on the Sys admin side of things. All of this information is helpful because it means I won't have as much to write about, except on the desktop side of things.

That being said I hope to have something to share here shortly.