JScript to query scheduled tasks

June 9th, 2011 by exhuma.twn

Soon, we will need to send out notifications as soon something bad happens with a scheduled task on Windows. The following JScript file runs natively on Windows and is capable of just that. It uses the command line tool “schtasks” to query the information and wraps the result into a list of usable object instances.

It’s possible to use this list to react to important events in the job executions. For example, you could loop through the list and send emails to the appropriate people if the variable “lastResult” is non-zero.

/**
* Naive CSV splitter
*
* This splitter is *very* simplistic and may result in errors when parsing
* unknown CSV sources. This works well in the current problem domain.
*
* As we have well defined data, with no escaped quotes inside the fields, we
* can sefaly assume that this will work.
*/
function simpleCsvSplit(lineText){
var columns = [];
var inside_quote = false;
var current_data = "";
var i=0;
for(i=0; i<lineText.length; i++){
var chr = lineText.substring(i, i+1);
if (chr === "\""){
inside_quote = !inside_quote;
continue;
} else if (chr === "," && !inside_quote){
columns[columns.length] = current_data;
current_data = "";
}
if (inside_quote){
current_data += chr;
}
}
columns[columns.length] = current_data;
return columns;
}

/**
* A container object for the task metadata
* This makes further processing with the data a lot more expressive.
*
* @param lineText A string taken from the CSV output representing one line
*/
var Task = function(lineText){
// parse the CSV data
columns = simpleCsvSplit(lineText);

// put everything into explicitly named variables
this.hostName = columns[0];
this.name = columns[1];
this.nextRun = columns[2];
this.status = columns[3];
this.lastRun = columns[4];
this.lastResult = columns[5];
this.creator = columns[6];
this.schedule = columns[7];
this.command = columns[8];
this.startIn = columns[9];
this.comment = columns[10];
this.scheduledState = columns[11];
this.scheduledType = columns[12];
this.startTime = columns[13];
this.startDate = columns[14];
this.endDate = columns[15];
this.days = columns[16];
this.months = columns[17];
this.runAs = columns[18];
this.deleteIfNotRescheduled = columns[19];
this.stopIf = columns[20];
this.repeatEvery = columns[21];
this.repeatUntilTime = columns[22];
this.repeatUntilDuration = columns[23];
this.repeatStopIfRunning =columns[24];
this.idleTime = columns[25];
this.powerManagement = columns[26];
};

// execute the shell command to retrieve the scheduled tasks
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("schtasks /Query /FO CSV /V");

// retrieve the lines from stdout and save them as tasks
var tasks = [];
while( !oExec.StdOut.AtEndOfStream){
   tasks[tasks.length] = oExec.StdOut.ReadLine();
}

// now do something with the tasks
for(var i=2; i<tasks.length; i++){
var tmp = new Task(tasks[i]);
WScript.Echo(tmp.lastResult + " - " + tmp.status);
}

Posted in Coding Voodoo | No Comments »

Windows script to remove old files

June 8th, 2011 by exhuma.twn

Simple script… still, I thought I’d share…

/**
* Remove all files and folders that are older than a set number of days.
*
* @param rootURI The URI of the root folder. All old files and folders in this
* folder are removed.
* @param days Files older than this number of days are deleted
*/
function purgeFiles(rootURI, days) {

  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var rootFolder = fso.GetFolder(rootURI);
  var subFolders = new Enumerator(rootFolder.SubFolders);

  // create a date before which files are considered "old"
  var threshold_date = new Date();
  threshold_date.setDate(threshold_date.getDate()-days);

  // loop over each file
  subFolders.moveFirst();
  for(;!subFolders.atEnd(); subFolders.moveNext()){
     var f = subFolders.item();
     if(f.DateCreated < threshold_date ){
        //WScript.Echo("Deleting "+f.Name+" last accessed on: "+f.DateCreated);
        f.Delete(true);
     }
  }

}

purgeFiles("C:/path/to/folder", 300);

view raw purgeFiles.js This Gist brought to you by GitHub.

Posted in Coding Voodoo | No Comments »

Change JPA EntityManager connection properties at Runtime

December 30th, 2010 by exhuma.twn

There are many times you want to be able to use different connection options for a JPA EntityManager. The most obvious is different user-credentials (think of a user login-screen and re-using these credentials to connect to the DB), or to make the distinction between development/testing/production environment.

However, if you let Netbeans create the persistence configuration, it will hardcode all connection parameters into the persistence.xml file. When retrieving an EntityManager instance, it will use this information to connect.

If, instead you would like to do this at runtime, you can do the following:

  1. Remove the “properties” tag from the persistence.xml. This may not be necessary, but this will make it clear, that the properties are set inside the code.
  2. Create a “Map<String, String>” which will contain the properties. A list of standard properties can be found in the specs in section 8.2.1.10.
  3. Use this map to create an EntityManagerFactory and use this to create your EntityManager

An example persistence.xml without properties

<?xml version="1.0" encoding="UTF-8"?>                                          
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLo
 <persistence-unit name="myTestPU" transaction-type="RESOURCE_LOCAL">    
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>            
    <class>my.package.Entity1</class>                    
    <class>my.package.Entity2</class>                    
  </persistence-unit>                                                              
</persistence>

In case Netbeans created a method “getEntityManager”, you can safely replace this. Here is an example I currently have in use. The “appConf” instance is a singleton I use to store configuration data in the user’s home folder, and yes, the password is stored in plain-text, but for this test-case I did not need to go any further:

    private EntityManager getEntityManager() {                                  
                                                                               
        Map<String, String> dbProps = new HashMap<String, String>();            
                                                                               
        dbProps.put("eclipselink.logging.level",                                
                appConf.get("eclipselink.logging.level", "INFO").toString());  
                                                                               
        // On linux, the GSSAPI is not available. Use a default user/password  
        // pair to connect                                                      
        if ("Linux".equals(System.getProperty("os.name"))) {                    
            dbProps.put("javax.persistence.jdbc.url",                          
                    String.format("jdbc:jtds:sqlserver://%s/%s",                
                    appConf.get("db.host", "my-default-host"),                          
                    appConf.get("db.database", "my-default-db")));                
            dbProps.put("javax.persistence.jdbc.driver",                        
                    "net.sourceforge.jtds.jdbc.Driver");                        
            dbProps.put("javax.persistence.jdbc.password",                      
                    appConf.get("db.password").toString());                    
            dbProps.put("javax.persistence.jdbc.user",                          
                    appConf.get("db.user", "my-default-username").toString());          
        } else {                                                                
            dbProps.put("javax.persistence.jdbc.url",                          
                    String.format("jdbc:sqlserver://%s;databaseName=%s;integratedSecurity=true",
                    appConf.get("db.host", "my-default-host"),                          
                    appConf.get("db.database", "my-default-db")));                
            dbProps.put("javax.persistence.jdbc.driver",                        
                    "com.microsoft.sqlserver.jdbc.SQLServerDriver");            
        }                                                                      
        appConf.flush();                                                        
                                                                               
        EntityManagerFactory fact = Persistence.createEntityManagerFactory("myTestPU", dbProps);
        return fact.createEntityManager();                                      
   }

This example uses eclipselink. All available properties can be found it the EclipseLink Wiki

Posted in Coding Voodoo | No Comments »

SPSS, MS-SQL2008 & bigint

March 31st, 2010 by exhuma.twn

There seems to be an issue with SPSS while reading data from an MS-SQL-Server instance. Notably with the SQL datatype “bigint”. Assume the following SPSS syntax:

GET DATA
   /TYPE=ODBC
   /CONNECT='DSN=my_dsn;SERVER=server_name;Trusted_Connection=yes;DATABASE=db_name'
   /SQL = 'SELECT year FROM  my_table'
.
EXECUTE.

If the field in question (in this case: “year”) is of SQL-type “bigint” then SPSS will show these values in the majority of the cases as “MISSING”. Sporadically some values appear, but they are completely wrong.

Once the cause is known (problem with the “biging” type), the solution is straight-forward: Cast the type to another appropriate type which is understood by SPSS. Which type you choose obviously depends on the values stored in the affected fields. Casting blindly to “int” may (I haven’t tested this!) resolve in strange results if the values lie outside of the “int” range (-2^31 to 2^31-1). In this case you may need to cast it to something alphanumeric like “varchar” and re-cast it in SPSS into “Numeric”. As said, I haven’t tested this but I thought it might be worth mentioning!

So, here’s the above query with the appropriate cast:

GET DATA
   /TYPE=ODBC
   /CONNECT='DSN=my_dsn;SERVER=server_name;Trusted_Connection=yes;DATABASE=db_name'
   /SQL = 'SELECT CONVERT(int, year) AS year FROM  my_table'
.
EXECUTE.

Note also that in this case you need to add an alias for the column ( “… AS year” ). Otherwise SPSS will return it as “VXXX” (where XXX is a sequential number).

I have tested this solution on all combinations of SPSS 11.5, SPSS 18, SQL-Server 2008 64bit, SQL-Server 2008 Express 32bit. And casting the value worked every time.

Depending on your use, it may be helpful to create views which do the casting. I have not yet tried this, but I don’t see a reason why it shouldn’t work. Additionally, it might be noteworthy that I have only encountered this problem with “bigint” so far. There may be problems with other types as well. I expect, casting them to something else should work there too.

Posted in Coding Voodoo | No Comments »

Dialog buttons not responding in Eclipse under KDE/GNOME

February 1st, 2010 by wickeddoc

In case you’re running into the same trouble as me, that dialog buttons are not “clickable” anymore under Eclipse, just add the following line to one of your linux startup scripts to fix the problem:

export GDK_NATIVE_WINDOWS=1

Posted in Coding Voodoo | No Comments »

Mayflower Zend Framework Cheatsheet

December 31st, 2009 by wickeddoc

Several weeks ago I was scouting the Internet for a Zend Framework cheatsheet and I found a blog entry somewhere about a Zend Framework Cheatsheet Poster, created by a German company called Mayflower.

On their blog they say, if you’re an eager Zend Framework developer and want to get your copy of their Cheatsheet poster, you’ll just have to send them an email and you’ll get this great Poster delivered to your office or home or whatever, free of charge. So that’s what I did and guess what, a week later I received this very useful poster in the mail.
So if you are a Zend Framework developer yourself and want to own this cool poster, don’t be shy, just send an email to Björn Schotte over at Mayflower.

Here’s a photo of the poster in our office @ Visual Online, Luxembourg

Mayflower Zend Framework Cheatsheet Poster

Posted in Zend Framework | No Comments »

Unable to easy_install psycopg2 on debian

October 29th, 2009 by exhuma.twn

Problem:

$ easy_install psycopg2
Searching for psycopg2                                                                                                                                                                                                                                                      
Reading http://pypi.python.org/simple/psycopg2/                                                                                                                                                                                                                              
Reading http://initd.org/projects/psycopg2                                                                                                                                                                                                                                  
Reading http://initd.org/pub/software/psycopg/                                                                                                                                                                                                                              
Best match: psycopg2 2.0.13                                                                                                                                                                                                                                                  
Downloading http://initd.org/pub/software/psycopg/psycopg2-2.0.13.tar.gz                                                                                                                                                                                                    
Processing psycopg2-2.0.13.tar.gz                                                                                                                                                                                                                                            
Running psycopg2-2.0.13/setup.py -q bdist_egg --dist-dir /tmp/easy_install-cHE0C_/psycopg2-2.0.13/egg-dist-tmp-x-CxRS                                                                                                                                                        
error: Setup script exited with error: No such file or directory

Solution:

This most likely indicates that you are missing the “libpq” headers:

sudo aptitude install libpq-dev

should solve the problem

Posted in Python | No Comments »

Zend Studio forgetting about your ZF Project

June 16th, 2009 by wickeddoc

as a php developer i’m using zend studio for eclipse on a daily basis. sometimes zend studio forgets about my zend framework projects, especially projects which are hosted on a SVN repository. i close my project, reopen it, and for no obvious reasons zend studio no longer recognizes it as a zend framework project. huch!?

until now i was unable to find a real solution to my problem, but here’s a little workaround which should get you up and running again, in case you’re running into the same problem.

close the project, then just open the .project file at the root of your project in your favourite text editor and check the ‘natures’ section, make sure it contains the following line:

<nature>org.zend.php.framework.ZendFrameworkNature</nature>

that should do the trick.

Posted in PHP, Zend Framework | 2 Comments »

Add creation and modification timestamps to an Excel worksheet

February 19th, 2009 by exhuma.twn

Please, for the love of $deity do not hit me….. This is going to be a post about excel!

Excel is a horrid solution for data entry, and even worse for data archival. And yet, it’s one of the most commonly used solutions. One of the most useful information in any given data-set is the information about when the information was created and when it was last modified. This is something that any decent developer in charge of a data collection (let’s just call it that for now) will add to each data record.

Alas, a lot of non-it people manage and store their data in excel worksheets. And that is OK with me as long as they pay attention to data archival. In it’s most simple form, data archival can be achieved by storing the data as a CSV file and including the following metadata:

  • Which column represents which value (the name of the variable)
  • The data type (number, text, date, …) of each column
  • If a column is “coded”, please also include the meaning of each code.
    For example a “Yes”, “No”, “Maybe” column might be stored as “1″, “2″ and “3″. Which means in it’s most basic nature it’s a numeric variable, but the different values have a meaning attached to them. So: Add this list in your metadata description.
  • If any computations or checks are performed on the values, please add them to the metadata document as well!

Even if the timestamp values might seem superflous at first, it will be of great help to anyone tracing errors in the data. Imagine that you would at some point need to fix some values that were entered/modified during a specific time period for whatever reason. Without this most basic bit of information you will be up for a treat. However, if it’s been rigurously implemented since the beginning, you’ll have the problem solved in no time.

Now, each halfway serious database system will offer you this kind of functionality out-of-the-box. But Excel is no database system (I intentionally left out the word “management” as this issue is a bit more general!). So it does not offer you a straight-forward way to solve this. But even if it’s not straight-forward, it’s simple enough for about anyone using Excel do add this bit of information.

Assuming that you use the first two columns (numbered 1 and 2 in excel) of your worksheet to add creation- and modification timestamps simply open up the Visual Basic editor (found in Tools->Macro or somesuch), next, in your project tree (in the top left of the screen) select your workbook (the .xls file), and in it’s sub-tree double-click the Worksheet that should have the timestamps set automatically.

Then copy/paste the following text into the just opened code editor and you’re done. I hope the comments will give some insight as to what happens. Note that in this case I will ignore the first row of the sheet, and obviously, the first two columns. If that does not suit your needs, feel free to change this script to your liking.

'
' Callback which is called when a cell in a workbook changes
' @param Target: The cell that changed it's value
'
Private Sub Worksheet_Change(ByVal Target As Range)
   ' We will ignore any changes in the first row, as it contains header labels
  If Target.Row = 1 Then Exit Sub
   
   ' As we set the values of column 1 and 2 we won't need to capture changes in these either
  If Target.Column = 1 Or Target.Column = 2 Then Exit Sub
   
   ' We will update the timestamp in column 2 *always* (last changed time)
  Cells(Target.Row, 2) = Now
   
   ' We will update the timestamp in column 1 only if it is empty (creation time)
  If IsEmpty(Cells(Target.Row, 1)) Then
     Cells(Target.Row, 1) = Now
   End If
End Sub

Posted in Coding Voodoo | No Comments »

Python startup (command completion & history)

August 21st, 2008 by exhuma.twn

If you want command completion and a history in your python shell, export the PYTHONSTARTUP env var (export PYTHONSTARTUP=$HOME/.pystartup) in your bashrc and create a file ~/.pystartup with the following contents:

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)

if os.path.exists(historyPath):
readline.read_history_file(historyPath)

readline.parse_and_bind('tab: complete')

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

Posted in Python | No Comments »

« Previous Entries

Pages

Recent Posts

Categories

Links


Archives

Meta