PowerShell and Unix crontables

Recently, I working on ways of documenting crontables we had on the various Unix servers we had a responsibility for. In the end I opted for a PowerShell solution to create an XML file for each of the crontables.

The program looks as follows:

# Creates an XML file from the output of the Unix command
# $ crontab -l > cron.txt.
# This program is also an example of creating XML files from PowerShell.

function Write-Dataline($xwriter, $cronbits, $id) {

  $xwriter.WriteAttributeString("ID", "$id");




write-output ("error thing = $($cronbits.ErrorRedirect)");
  $xwriter.WriteFullEndElement();     # <-- Close element ''Line''.

} #end function Write-Dataline

function Close-XmlFile($xwriter) {

  Write-Host "Closing the xml file";
  # The closing bits of the XML file
  $xwriter.WriteEndElement();     # <-- Close root element
  $xwriter.WriteEndDocument();    # <-- Close the document

  # Finish The Document

} #end function Close-XmlFile

function Get-Lineparts([String]$str) {
  begin {
    $props = [ordered]@{
       DateTime = "";
       Executable = "";
       Logfile = "";
       ErrorRedirect = "";
    $cronbits = New-Object psobject -Property $props;

    $props = [ordered]@{
       Start = 0;
       End = 0;
    $pos = New-Object psobject -Property $props;
  } #end begin

  process {

    # Get the date/time part of the cron line.

    # Get the executable, i.e. the shell script bit.
    $pos.End=$str.IndexOf('>', $pos.End);

    # Get the logfile name.
    $pos.Start=$str.IndexOf('/', $pos.End);
    $pos.End=$str.IndexOf('2>&1', $pos.End);

    # Get the error redirect.

  } #end process

  end {
    return $cronbits;
  } #end end

} #end function Get-Lineparts

function Set-XmlWriterSettings {

  $xsettings = New-Object -TypeName System.Xml.XmlWriterSettings;
  $xsettings.CloseOutput = $true;
  $xsettings.Encoding = [System.Text.Encoding]::UTF8;
  $xsettings.Indent = $true;
  $xsettings.IndentChars = "    "; # 4 spaces
  $xsettings.WriteEndDocumentOnClose = $true;
  $xsettings.CheckCharacters = $true;
  #$xsettings.NewLineChars = "0xoa";
  $xsettings.NewLineOnAttributes = $true;
  $xsettings.OmitXmlDeclaration = $false;
  $xsettings.ConformanceLevel = [System.Xml.ConformanceLevel]::Auto;

  return $xsettings;

} #end function Set-XmlWriterSettings

function Set-XmlFile {

  # Do some initialising
  $filePath = "C:\ian\PowerShell\good03.xml";
  $xset = Set-XmlWriterSettings;
  [System.Xml.XmlWriter]$xwriter = [System.Xml.XmlWriter]::Create($filePath,$xset);

  # Write the header elements to our XML file
  $xwriter.WriteStartDocument();               # <-- Start the document
  $xwriter.WriteStartElement("CronTable");     # <-- Important root element





  return $xwriter;

} #end function Set-XmlFile

# ------------------------
# Main routine starts here
# ------------------------

$input = New-Object -TypeName System.IO.StreamReader -ArgumentList $infile;

#Create and write the header to the XmlFile.

$inrec = $input.ReadLine();
while ($inrec -ne $null) {
   Write-Output ($inrec);
   $cronbits=Get-Lineparts $inrec;
   Write-Dataline $xwriter $cronbits $counter;
   $cronbits | Format-list;

   $inrec = $input.ReadLine();



Write-Host "`nRecords read: $counter";
Write-Host "The date is $ymd";

Input and output files are hard coded so the program could be more flexible.

Input file – is at line 148.

Output file – is at line 113.

The input file is created on the Unix server with the command:

$ crontab -l > inputfile.txt

The man page will cover this command. The input file I used was:

15,30,45,0 * * * * /u01/Acer/MorrisGarages/exec/GreenBank.sh > /u01/Acer/MorrisGarages/GreenBank.log 2>&1
45 19 * * * /u04/Acer/Minolta/exec/Hartung–Boothroyd.sh > /u04/Acer/Minolta/logs/Hartung–Boothroyd.log 2>&1
25 04 * * * /u04/Boeing/Digital/Jodrell-Bank.sh > /u04/Boeing/Digital/Jodrell-Bank.log 2>&1

The output XML file will look like this:

<?xml version="1.0" encoding="utf-8"?>
            <CronTime>15,30,45,0 * * * *</CronTime>
            <CronTime>45 19 * * *</CronTime>
            <CronTime>25 04 * * *</CronTime>

The XML tag CronLines contain details of each of the lines in my example input file. The ID attribute indicating which line in particular. So the tag <Line ID=”3″> gives the detail for line 3 of my input file, Jodrell-Bank.sh.

Lines 156-165 is the main processing loop in which each line is read in from the input file.

Line 160 invokes function Get-Lineparts passing to it, an input record.

Funtion Set-XmlWriterSettings specifies a set of features to support on the XmlWriter object. See XmlWriterSettings Class for further details. This is really initialising my XML object. How many character spaces to indent and so on.

Function Write-Dataline writes data to the tags CronTime, Executable, Logfile and ErrorRedirect.

Function Get-Lineparts tokenizes the input string record into its constituent parts.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s