Thursday, December 13, 2012

Powershell script to Refresh Excel Data

The following sript opends Excel files one by one, refreshes the data, saves the file back and closes the file.


 Function RefreshExcelData($Excel){
  $App = new-object -comobject excel.application
  $App.Workbooks.Open($Excel)
  $App.Visible = $true
  $App.ActiveWorkbook.Refreshall()
  $App.Save()
  $App.Quit()
  $username = $env:username
  Remove-Item "C:\Users\$username\Documents\RESUME.XLW"
 }

 CLEAR-HOST
 $Host.UI.RawUI.ForegroundColor = "yellow"
 $Excel = 'R:\folder\file1.xlsx'
 RefreshExcelData $Excel
 $Excel = 'R:\folder\file2.xlsx'
 RefreshExcelData $Excel
 $Excel = 'R:\folder\file3.xlsx'
 RefreshExcelData $Excel
 $Host.UI.RawUI.ForegroundColor = "white"

If you find this script useful please donate generously.

Thursday, December 6, 2012

Powershell script to ftp MySQL backup file and restore it locally.

The following Powershell script can be customised and scheduled (

 powershell.exe -executionpolicy bypass -file E:\scripts\dbrestore.ps1
) on your server:

 <#########################################################################
  Purpose:  FTP down MySQL backup file and restore it locally.
  Requirements: 1) Source backup file automatically created daily.
       The name is generated as YYYYMMDD_dbname.sql
       and located in /dbbackup folder.
      2) Indy.Sockets.dll exits on the destination system.
       Lets presume that it is located in e:\scripts
 #########################################################################>
 function Open-FTPConnection($ftphost, $username, $password) {
  [void][Reflection.Assembly]::LoadFrom("e:\scripts\Indy.Sockets.dll");
   $ftp = new-object Indy.Sockets.FTP;
   $ftp.Disconnect();
   $ftp.Host = $ftphost;
   $ftp.Username = $username;
   $ftp.Password = $password;
   $ftp.Connect();
   $ftp.Passive=$true;
   return $ftp;
  }

 function Close-FTPConnection($ftp) {
  $ftp.Disconnect();
  }

 function Draw-DirectoryContents($ftp) {
  "Listing directory contents ..";
  $ls = new-object System.Collections.Specialized.StringCollection;
  $ftp.List($ls, "", $true);
  foreach($file in $ls){
   $file;
   }
   Get-FTPCurrentLocation $ftp
  "Complete";
  }

 function Get-FTPCurrentLocation($ftp) {
  return $ftp.RetrieveCurrentDir();
  }

 function Set-ParentLocation($ftp) {
  "Changing to Parent Directory ..";
  $ftp.ChangeDirUp();
  Draw-DirectoryContents;
  }

 function Download-FTPFile($ftp, $sourceFileName, $targetDir) {
  "Downloading {0} into {1}.." -f $sourceFileName, $targetDir;
  $ftp.Get($sourceFileName, ($targetDir + $sourceFileName), $true, $false);
  "Downloading of {0} into {1} is complete" -f $sourceFileName,
  [Environment]::CurrentDirectory;
  }
  
 function Upload-FTPFile($ftp, $sourceFileName, $targetDir) {
  "Uploading {0} into {1}.." -f $sourceFileName, $targetDir;
  $ftp.Put($sourceFileName, ($targetDir + $sourceFileName), $false);
  "Uploading of {0} into {1} is complete" -f $sourceFileName, $targetDir;
  }
 
 function Process-FTPItem($ftp, [string]$sel) {
   [string[]]$fields=[Regex]::Split($sel, " +");
   $startField=8; #the file/directory name starts after 8 fields
   [string]$name="";
  
  for ($field=$startField; $field -lt $fields.Length; $field++) {
    if ($field -eq $startField) {
    $temp = "";
    } else {
    $temp = " ";
    }
    $name += $temp + $fields[$field];
    #add aditional space for name split into multiple fields
   }
  
  if ($sel[0] -eq 'd') { #directory
    "Changing directoy to {0} .." -f $name;
    $ftp.ChangeDir($name);
    Draw-DirectoryContents $ftp #redraw contents after changing directory
  } else {
    if ($sel[0] -eq '-') { #plain file has '-' as first character
    return $name; #return the name of file to download
    }
  }
  }
 
 function datetime{
  $year = (Get-Date).Year;
  $month = Get-Date -format MM;
  $day = Get-Date -format dd;
  $hour = Get-Date -format hh;
  $minute = Get-Date -format mm;
  $second = Get-Date -format ss;
  return "$day/$month/$year $hour`:$minute`:$second";
 }

 $year = (Get-Date).Year;
 $month = Get-Date -format MM;
 $day = Get-Date -format dd;
 $hour = Get-Date -format hh;
 $minute = Get-Date -format mm;
 $second = Get-Date -format ss;
 CLEAR-HOST
 $Host.UI.RawUI.ForegroundColor = "yellow"
 [void][reflection.assembly]::LoadWithPartialName("microsoft.visualbasic")
 $date = datetime;
 "** Start process $date **";
 write-host "`r";
 $theDate = "$year$month$day";
 $word_to_delete = "_dbname.sql";
 $file = "/dbbackup/$theDate$word_to_delete";
 $dir = "e:\db_backup"; #Destination folder
 $from = "$dir\dbbackup";
 $to = "$dir\$theDate`_$hour$minute$second";
 $out = "$to\$theDate$word_to_delete";
 $procedures = "/dbbackup/curls_procedures_$year-$month-$day.sql"
 if (!(Test-Path -path "$dir\$theDate*")){
  if ((Test-Path -path "$dir\dbbackup")){
   Remove-Item "$dir\dbbackup";
  }
  New-Item "$dir\dbbackup" -type directory;
  $f = Open-FTPConnection "www.sitename.com" "ftpUserName" "ftpPassword";
  Download-FTPFile $f $file ($dir);
  Download-FTPFile $f $procedures ($dir);
  Close-FTPConnection $f;
  Rename-Item $from $to;
  #Global replace of the keyword
  (Get-Content $out) | Foreach-Object {$_ -replace "sourceDBname", "destinationDBname"} | Set-Content $out;
  Write-Host $to;
  Write-Host $from;
 } else {
  "File already downloaded!";
  "Processing local restore ...";
  $items = Get-ChildItem -Path "$dir\$theDate*"
  foreach ($item in $items){
   $val = $item.Attributes
     if ($item.Attributes -Like "Directory*"){
    $theDir = $item.Name
     }
  }
  $theDir = "$dir\$theDir"
  $fileEntries = [IO.Directory]::GetFiles($theDir); 
  foreach($filename in $fileEntries){ 
   $file = $filename;
  }
 }

 $mysql = "mysql -u dbUserName --password=dbPassword destinationDBname --execute=""SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(TABLE_NAME),';') AS stmt FROM information_schema.TABLES WHERE  TABLE_SCHEMA = 'destinationDBname' INTO OUTFILE 'e:\\db_backup\\dropAllTables.sql'"""
 Write-Host $mysql;
 & cmd /c $mysql;
 $mysql = "mysql -u dbUserName --password=dbPassword destinationDBname < e:\db_backup\dropAllTables.sql"
 Write-Host $mysql;
 & cmd /c $mysql;
 $mysql = "mysql -u dbUserName --password=dbPassword destinationDBname < $out"
 Write-Host $mysql;
 write-host "`r";
 $date = datetime;
 "** End of process $date **";

If you find this script useful please donate generously.

Tuesday, June 5, 2012

Reading live XML file to the SQL Server database by combining consumed and unconsumed XML data.

Task: Query the XML (LicenseKeys.xml) file from MS SQL Server database.
XML file fragment:

<?xml version="1.0" encoding="utf-8"?>
<licenseBatch xmlns="http://site/schemas/licensekey">
                <license batchID="1" batchStatus="Active" expireDays="60" admin="WESTERN_SYDNEY\rsakhamuri" adminEmail="Ramya.Sakhamuri@site.com" requester="rsakhamuri" requesterEmail="Ramya.Sakhamuri@site.com" creationTimeStamp="2010-02-04T15:46:02.525392+11:00" activationType="OfflinePossible">
                                <note>test</note>
                                <licenseKey sequence="1" key="3fea9da9-28b1-47f1-82ff-4198c96330d7" owner="ramya" ownerEmail="" status="ReActivated" lastModified="2010-02-04T05:03:40.663936Z" noOfActivation="3" maxActivationCount="50">
                                                <activator IP="127.0.0.1" userName="WESTERN_SYDNEY\rsakhamuri" email="" activationTimeStamp="2010-02-04T15:48:57.9026368+11:00">rams</activator>
                                                <activator IP="127.0.0.1" userName="WESTERN_SYDNEY\rsakhamuri" email="" activationTimeStamp="2010-02-04T15:49:11.5434364+11:00">rams</activator>
                                                <activator IP="172.24.108.48" userName="WESTERN_SYDNEY\rsakhamuri" email="ramya.sakhamuri@site.com" activationTimeStamp="2010-02-04T16:03:40.663936+11:00">ramya</activator>
                                </licenseKey>
                                <licenseKey sequence="2" key="acbebeeb-af3e-40d1-a9ca-f03dd0a5cce4" owner="" ownerEmail="" status="InActive" lastModified="2010-02-04T15:46:02.525392+11:00" noOfActivation="0" maxActivationCount="50" />
                                <licenseKey sequence="3" key="8efcc7c4-c367-4316-b8b3-8f5c8cfc244c" owner="" ownerEmail="" status="InActive" lastModified="2010-02-04T15:46:02.525392+11:00" noOfActivation="0" maxActivationCount="50" />
                                <licenseKey sequence="4" key="8b6e7919-7107-4025-bddb-5a746e1b2691" owner="" ownerEmail="" status="InActive" lastModified="2010-02-04T15:46:02.525392+11:00" noOfActivation="0" maxActivationCount="50" />
                                <licenseKey sequence="5" key="c5cf95d8-4b5a-458b-8704-4e8e569e2e07" owner="" ownerEmail="" status="InActive" lastModified="2010-02-04T15:46:02.525392+11:00" noOfActivation="0" maxActivationCount="50" />
                </license>
                <license batchID="2" batchStatus="Active" expireDays="180" admin="WESTERN_SYDNEY\rsakhamuri" adminEmail="Ramya.Sakhamuri@site.com" requester="rsakhamuri" requesterEmail="Ramya.Sakhamuri@site.com" creationTimeStamp="2010-04-20T18:30:33.998494+10:00" activationType="OnlineOnly">
                                <note>for OTEN users 20-04-2010</note>
                                <licenseKey sequence="1" key="96af9492-0f01-4689-9443-2b098a75f08b" owner="Julie Esson" ownerEmail="ramya.sakhamuri@site.com" status="ReActivated" lastModified="2010-04-21T06:21:53.4190295Z" noOfActivation="2" maxActivationCount="50">
                                                <activator IP="172.24.108.190" userName="WESTERN_SYDNEY\rsakhamuri" email="ramya.sakhamuri@site.com" activationTimeStamp="2010-04-21T08:44:23.8095566+10:00">ramya sakhamuri</activator>
                                                <activator IP="172.24.111.148" userName="SYDNEY\JEsson" email="julie.esson@site.com" activationTimeStamp="2010-04-21T16:21:53.4190295+10:00">Julie Esson</activator>
                                </licenseKey>
                                <licenseKey sequence="2" key="9c5e56b7-8733-46df-9f2d-8b91a57fd805" owner="Kuan Sin" ownerEmail="kuan.sin@site.com" status="Active" lastModified="2010-04-21T06:33:50.4079932Z" noOfActivation="1" maxActivationCount="50">
                                                <activator IP="172.24.107.251" userName="WESTERN_SYDNEY\ksin" email="kuan.sin@site.com" activationTimeStamp="2010-04-21T16:33:50.4079932+10:00">Kuan Sin</activator>
                                </licenseKey>
                                <licenseKey sequence="3" key="a350aff9-78c0-4446-a88e-8967771211c2" owner="Carmen Vallis" ownerEmail="carmen.vallis@site.com" status="ReActivated" lastModified="2010-07-22T23:52:25.3306929Z" noOfActivation="2" maxActivationCount="50">
                                                <activator IP="172.24.108.67" userName="WESTERN_SYDNEY\cvallis2" email="carmen.vallis@site.com" activationTimeStamp="2010-04-21T16:41:42.0516367+10:00">Carmen Vallis</activator>
                                                <activator IP="172.24.111.148" userName="WESTERN_SYDNEY\cvallis2" email="carmen.vallis1@site.com" activationTimeStamp="2010-07-23T09:52:25.3306929+10:00">Carmen Vallis</activator>
                                </licenseKey>
                                <licenseKey sequence="4" key="67865ae2-036e-4be3-a601-a55d378d7694" owner="Janet Coller" ownerEmail="janet.coller@site.com" status="Active" lastModified="2010-04-21T22:21:08.9738945Z" noOfActivation="1" maxActivationCount="50">
                                                <activator IP="172.24.110.20" userName="WESTERN_SYDNEY\jcoller" email="janet.coller@site.com" activationTimeStamp="2010-04-22T08:21:08.9738945+10:00">Janet Coller</activator>
                                </licenseKey>
                                <licenseKey sequence="5" key="bd927ebb-a846-4af7-a3fe-00a827ca69fb" owner="Rob Wales" ownerEmail="robert.wales@site.com" status="Active" lastModified="2010-04-21T23:51:30.0881764Z" noOfActivation="1" maxActivationCount="50">
                                                <activator IP="172.24.109.153" userName="WESTERN_SYDNEY\rwales2" email="robert.wales@site.com" activationTimeStamp="2010-04-22T09:51:30.0881764+10:00">Rob Wales</activator>
                                </licenseKey>
                </license>
</licenseBatch>
This file can be automatically imported into MS Excel and subsequently to MS SQL Server database. It can be used for solution verification.The issue with reading such XML file using OPENXML is that not all data can be extracted in one go.  The licenseKey tag may consist of multiple activator tags however OPENXML can read only one set at the time.  The paragraph "I. Specifying the xml data type in the WITH clause" of Using OPENXML describes extracting unconsumed data into OverFlow column.The solution below required enabling BULK insert for the database user and Ad Hoc Distributed Queries.  It can be achieved by running the following set of commands:sp_addsrvrolemember @loginame = N' YourDatabaseUser', @rolename = N'bulkadmin'gosp_configure 'show advanced options',1reconfigure with overridegosp_configure 'Ad Hoc Distributed Queries',1reconfigure with overridegoSolution (combines consumed and unconsumed data into the one recordset):
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop procedure dbo.udpLicenceKeys'
GOif exists (select * from dbo.sysobjects
 where id = object_id(N'dbo.udpLicenceKeys')
 and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure dbo.udpLicenceKeys
GO

PRINT 'CREATE PROC dbo.udpLicenceKeys'
GO/******************************************************************************
   NAME:              udpLicenceKeys
   PURPOSE:   REVISIONS:   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE PROCEDURE dbo.udpLicenceKeys
AS
   DECLARE  @LicenceKeys TABLE(XmlCol xml)                INSERT @LicenceKeys
                  SELECT CONVERT(xml, BulkColumn, 2) FROM
                                OPENROWSET(Bulk '\\servername\share\LicenseKeys.xml'
                                , SINGLE_BLOB) rowsetresults;                DECLARE @Data XML                SELECT @Data = XmlCol FROM @LicenceKeys                             DECLARE @idoc int
                DECLARE @doc NVARCHAR(max)
                SET @doc = (SELECT CONVERT(NVARCHAR(max),XmlCol) FROM @LicenceKeys)
                SET @doc = REPLACE(@doc,'<?xml version="1.0" encoding="utf-8"?>','')
                SET @doc = REPLACE(@doc,' xmlns="http://site.com/schemas/licensekey"','')                EXEC sp_xml_preparedocument @idoc OUTPUT, @doc                DECLARE @FindSubString VARCHAR(MAX)                SET @FindSubString = '<activator'                SELECT  T.*
                ,(CASE WHEN (LEN(convert(nvarchar(max),overflow)) -
                                                                LEN(REPLACE(convert(nvarchar(max),overflow), @FindSubString, '')))
                                                                /LEN(@FindSubString) > 0
                                                THEN REPLACE(convert(nvarchar(max),overflow)
                                                                ,'<licenseKey','<licenseKey batchID="'+CONVERT(NVARCHAR(10),BatchID)
                                                                +'" sequence="' + CONVERT(NVARCHAR(10),sequence) + '" ')
                  ELSE NULL END
                ) strXML                FROM (
                SELECT *
                FROM OPENXML (@idoc, '/licenseBatch/license/licenseKey',10)
                WITH (
                                batchID float '../@batchID'
                                ,batchStatus nvarchar(255) '../@batchStatus'
                                ,expireDays float '../@expireDays'
                                ,[admin] nvarchar(255) '../@admin'
                                ,adminEmail nvarchar(255) '../@adminEmail'
                                ,requester nvarchar(255) '../@requester'
                                ,requesterEmail nvarchar(255) '../@requesterEmail'
                                ,creationTimeStamp nvarchar(255) '../@creationTimeStamp'
                                ,activationType nvarchar(255) '../@activationType'
                                ,note nvarchar(255) '../note'
                                ,sequence float '@sequence'
                                ,[key] nvarchar(255) '@key'
                                ,[owner] nvarchar(255) '@owner'
                                ,ownerEmail nvarchar(255) '@ownerEmail'
                                ,[status] nvarchar(255) '@status'
                                ,lastModified nvarchar(255) '@lastModified'
                                ,noOfActivation float '@noOfActivation'
                                ,maxActivationCount float '@maxActivationCount'
                                ,activator nvarchar(255)
                                ,IP nvarchar(255) 'activator/@IP'
                                ,userName nvarchar(255) 'activator/@userName'
                                ,email nvarchar(255) 'activator/@email'
                                ,activationTimeStamp datetime 'activator/@activationTimeStamp'
                                ,OverFlow nvarchar(max) '@mp:xmltext')) TGO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO                       
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop view dbo.udvLicenceKeys'
GOif exists (select * from dbo.sysobjects
 where id = object_id(N'dbo.udvLicenceKeys')
 and OBJECTPROPERTY(id, N'IsView') = 1)
drop view dbo.udvLicenceKeys
GOPRINT 'CREATE VIEW dbo.udvLicenceKeys'
GO/******************************************************************************
   NAME:              dbo.udvLicenceKeys
   PURPOSE:   REVISIONS:$Log$   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE VIEW dbo.udvLicenceKeys
AS
SELECT  *
FROM    OPENROWSET( 'SQLNCLI'
                    ,'Server=servername;UID=YourUserName;PWD=YourPassword;'
                    ,'SET FMTONLY OFF; SET NOCOUNT ON; Execute YourDatabaseName..udpLicenceKeys')

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop function dbo.udfGet_Overflow_XML'
GOif exists (select * from sysobjects
where id = object_id(N'.dbo.udfGet_Overflow_XML')
 and xtype in (N'FN', N'IF', N'TF'))
drop function dbo.udfGet_Overflow_XML
GOPRINT 'CREATE FUNCTION dbo.udfGet_Overflow_XML'
GO/******************************************************************************
   NAME:              udvGet_Overflow_XML
   PURPOSE:   REVISIONS:$Log$   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE FUNCTION dbo.udfGet_Overflow_XML()
RETURNS NVARCHAR(MAX)
AS
                BEGIN
                                DECLARE  @strXML NVARCHAR(MAX)
                                                                ,@finalXML NVARCHAR(MAX)                                DECLARE crs CURSOR
                                                                FOR select strXML from dbo.udvLicenceKeys                                SET @finalXML = '<root>'                                IF @@error = 0 BEGIN
                                                OPEN crs
                                END                                FETCH NEXT FROM crs
                                INTO   @strXML                                WHILE @@FETCH_STATUS = 0 BEGIN
                                                IF @strXML IS NOT NULL BEGIN
                                                                SET @finalXML = @finalXML + @strXML
                                                END
                                                FETCH NEXT FROM crs
                                                INTO   @strXML
                                END
                                DEALLOCATE crs
                                SET @finalXML = @finalXML + '</root>'
                                RETURN (@finalXML)
                ENDGO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop procedure dbo.udpOverflow_Recordset'
GOif exists (select * from dbo.sysobjects
 where id = object_id(N'dbo.udpOverflow_Recordset')
 and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure dbo.udpOverflow_Recordset
GO

PRINT 'CREATE PROC dbo.udpOverflow_Recordset'
GO/******************************************************************************
   NAME:              udpOverflow_Recordset
   PURPOSE:   REVISIONS:   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE PROCEDURE dbo.udpOverflow_Recordset
AS                DECLARE @DocHandle int
                DECLARE @XmlDocument nvarchar(MAX)                                SET @XmlDocument = (select dbo.udfGet_Overflow_XML())
                                EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument                                SELECT *
                                FROM OPENXML (@DocHandle, '/root/licenseKey/activator',1)
                                                  WITH (batchID float '../@batchID'
                                                                ,sequence float '../@sequence'
                                                                ,IP nvarchar(255) '@IP'
                                                                ,userName nvarchar(255) '@userName'
                                                                ,email nvarchar(255) '@email'
                                                                ,activationTimeStamp datetime '@activationTimeStamp'
                                                                ,activator nvarchar(255) '.')               
                                EXEC sp_xml_removedocument @DocHandleGO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop view dbo.udvOverflow_Recordset'
GOif exists (select * from dbo.sysobjects
 where id = object_id(N'dbo.udvOverflow_Recordset')
 and OBJECTPROPERTY(id, N'IsView') = 1)
drop view dbo.udvOverflow_Recordset
GOPRINT 'CREATE VIEW dbo.udvOverflow_Recordset'
GO/******************************************************************************
   NAME:              dbo.udvOverflow_Recordset
   PURPOSE:   REVISIONS:$Log$   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE VIEW dbo.udvOverflow_Recordset
AS
SELECT  *
FROM    OPENROWSET( 'SQLNCLI'
                                                                                ,'Server=servername;UID=YourUserName;PWD=YourPass;'
                    ,'SET FMTONLY OFF; SET NOCOUNT ON; Execute DatabaseName..udpOverflow_Recordset')

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop view dbo.udvOverflow_Full_Recordset'
GOif exists (select * from dbo.sysobjects
 where id = object_id(N'dbo.udvOverflow_Full_Recordset')
 and OBJECTPROPERTY(id, N'IsView') = 1)
drop view dbo.udvOverflow_Full_Recordset
GOPRINT 'CREATE VIEW dbo.udvOverflow_Full_Recordset'
GO/******************************************************************************
   NAME:              dbo.udvOverflow_Full_Recordset
   PURPOSE:   REVISIONS:$Log$   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE VIEW dbo.udvOverflow_Full_Recordset
AS
                SELECT DISTINCT
                                                O.batchID
                                                ,L.batchStatus
                                                ,L.expireDays
                                                ,L.admin
                                                ,L.adminEmail
                                                ,L.requester
                                                ,L.requesterEmail
                                                ,L.creationTimeStamp
                                                ,L.activationType
                                                ,L.note
                                                ,L.sequence
                                                ,L.[key]
                                                ,L.owner
                                                ,L.ownerEmail
                                                ,L.status
                                                ,L.lastModified
                                                ,L.noOfActivation
                                                ,L.maxActivationCount
                                                ,O.activator
                                                ,O.IP
                                                ,O.userName
                                                ,O.email
                                                ,O.activationTimeStamp
                                                 from dbo.udvLicenceKeys L
                INNER JOIN dbo.udvOverflow_Recordset O
                 ON L.batchID = O.batchID
                 WHERE L.sequence = O.sequenceGO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOPRINT 'drop function dbo.udfLicenceKeys'
GOif exists (select * from sysobjects
where id = object_id(N'.dbo.udfLicenceKeys')
 and xtype in (N'FN', N'IF', N'TF'))
drop function dbo.udfLicenceKeys
GOPRINT 'CREATE FUNCTION dbo.udfLicenceKeys'
GO/******************************************************************************
   NAME:              udfLicenceKeys
   PURPOSE:   REVISIONS:$Log$   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
 ******************************************************************************/
CREATE FUNCTION dbo.udfLicenceKeys()
RETURNS @tempTBL TABLE (
                                                                 batchID float
                                                                ,batchStatus nvarchar(255)
                                                                ,expireDays float
                                                                ,[admin] nvarchar(255)
                                                                ,adminEmail nvarchar(255)
                                                                ,requester nvarchar(255)
                                                                ,requesterEmail nvarchar(255)
                                                                ,creationTimeStamp nvarchar(255)
                                                                ,activationType nvarchar(255)
                                                                ,note nvarchar(255)
                                                                ,sequence float
                                                                ,[key] nvarchar(255)
                                                                ,[owner] nvarchar(255)
                                                                ,ownerEmail nvarchar(255)
                                                                ,[status] nvarchar(255)
                                                                ,lastModified nvarchar(255)
                                                                ,noOfActivation float
                                                                ,maxActivationCount float
                                                                ,activator nvarchar(255)
                                                                ,IP nvarchar(255)
                                                                ,userName nvarchar(255)
                                                                ,email nvarchar(255)
                                                                ,activationTimeStamp datetime)
AS
                BEGIN
                                INSERT INTO @tempTBL
                                SELECT batchID
                                                ,batchStatus
                                                ,expireDays
                                                ,admin
                                                ,adminEmail
                                                ,requester
                                                ,requesterEmail
                                                ,creationTimeStamp
                                                ,activationType
                                                ,note
                                                ,sequence
                                                ,[key]
                                                ,owner
                                                ,ownerEmail
                                                ,status
                                                ,lastModified
                                                ,noOfActivation
                                                ,maxActivationCount
                                                ,activator
                                                ,IP
                                                ,userName
                                                ,email
                                                ,activationTimeStamp
                                FROM  dbo.udvLicenceKeys                                INSERT INTO @tempTBL
                                SELECT batchID
                                                ,batchStatus
                                                ,expireDays
                                                ,admin
                                                ,adminEmail
                                                ,requester
                                                ,requesterEmail
                                                ,creationTimeStamp
                                                ,activationType
                                                ,note
                                                ,sequence
                                                ,[key]
                                                ,owner
                                                ,ownerEmail
                                                ,status
                                                ,lastModified
                                                ,noOfActivation
                                                ,maxActivationCount
                                                ,activator
                                                ,IP
                                                ,userName
                                                ,email
                                                ,activationTimeStamp
                                FROM   dbo.udvOverflow_Full_Recordset
                                RETURN
                ENDGO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

 The final query:SELECT * FROM dbo.udfLicenceKeys()
 
If you find this script useful please donate generously.

Sunday, December 18, 2011

Archive IIS Log Files

The following PowerShell script is using 7Zip to compress IIS Log files.  It identifies IIS Log folder and parses through the contents to find the log files older than $days.  It zips them up, and delete the original log files. The resulting compressed archive will be about 4.5% of the size of the original log files.


<##################################################################################
 NAME:  Archive_IIS_Log_Files.ps1
  
 COMMENT:  The script parse through the IIS logs directory contents to find the log files 
   older than $days. Zip them up, and delete the original  
           log files. The resulting compressed archive will be about  
           4.5% of the size of the original log files. 
  
 REQUIRED: 7-Zip is required for this to work. By default this script  
           looks for the 7-Zip executable, 7za.exe, in C:\Program Files\7-Zip\ 
 
 Running Scripts Without Starting Windows PowerShell
   powershell.exe -noexit c:\MyScripts\Archive_IIS_Log_Files.ps1

 Scheduling Action: powershell.exe c:\MyScripts\Archive_IIS_Log_Files.ps1
##################################################################################>
  
Function Archive_IIS_Log_Files{
 $strComputer = "."
 $days = "7"
 $exe = 'C:\Program Files\7-Zip\7z.exe'
 $emailFrom = "noreply@server.com"
 $emailTo = "admin@server.com"
 $subject = "Scheduled job 'Archive IIS Log Files' failed"
 $body = "Error in : "
 $smtpServer = "smtp.mail.com"
 Write-host "`r` "
 Write-host "`r` "
 Write-host "`t` `t` Start process `t` `t` "  -foregroundcolor Black -backgroundcolor White
 Write-host "`r` "
 Write-host "`t` `t` Inluding .Net Web.Administration"  -foregroundcolor "Green"
 [System.Reflection.Assembly]::LoadFile( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll")
 $iis = new-object Microsoft.Web.Administration.ServerManager
 Write-host "`r` "
 Write-host "`t` `t` Achive logs for the following websites: `t` `t` "  -foregroundcolor DarkCyan
 $iis.sites | select-object Id, Name
 $TopFolder = $IIS.SiteDefaults.LogFile.Directory 
 $TopFolder = $TopFolder -replace "%SystemDrive%", "C:"
 Write-host "`t` `t` Folders to process:" -foregroundcolor DarkCyan
 Get-ChildItem -path $TopFolder | select pspath
 Write-Host "To cancel the process, Press ‘Ctrl + C’" -foregroundcolor Yellow
 Start-sleep -s 5
 foreach ($site in $iis.sites){
  $TargetFolder = "$TopFolder\W3SVC"
  $TargetFolder = $TargetFolder += $site.Id
  $zipName = Get-Date -format 'yyyy_MM_dd_HH_mm_ss'
  $zipName = "$TargetFolder\$zipName.logs.zip"
  Write-host "`r` "
  Write-Host "Zip file: " $zipName -foregroundcolor DarkCyan
  $SiteName = GetSiteNameForByLogFolder $TopFolder  $TargetFolder
  Write-host "Processing logs for: $SiteName" -foregroundcolor "Yellow"
  if (Test-Path $TargetFolder){
   #Warn you the targeted folder, so you can double check
   Write-host "`r` "
   Write-host "The Targeted Folder is:" $TargetFolder -foregroundcolor DarkCyan
   $Now = Get-Date
   # Notice the minus sign before $days
   $LastWrite = $Now.AddDays(-$days)
   $Files = get-childitem $TargetFolder -include *.log -recurse |Where {$_.LastWriteTime -le "$LastWrite"}
   If ($Files -eq $NULL){
    write-host "No files for processing." -foregroundcolor Green
    return
   }
   try{
    foreach ($File in $Files){
     Write-host "`r` "
     Write-host "Running: " $exe a $zipName $File -foregroundcolor Yellow
     & $exe a $zipName $File
     Write-host "`r` "
     write-host "Deleting File $File" -foregroundcolor "Red"
     #You can add -whatif to see the consequence – Remove-item $File -Whatif
     $File.Delete()
    }
    #throw $_.exception
   }
   catch{
    #Send error message
    $body = "$body$File"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($emailFrom, $emailTo, $subject, $body)
    Write-Error  $body
   }
  } Else {
   Write-host "`r` "
   Write-Host "The Folder $TargetFolder Does Not Exist!"
  }
 }
 Write-host "`r` "
 Write-host "`t` `t` End process `t` `t` "  -foregroundcolor Black -backgroundcolor White
}
Function GetSiteNameForByLogFolder($TopFolder , $TargetFolder){
 $TargetFolderLength = $TargetFolder.Length
 $TopFolderLength = "$TopFolder\W3SVC".Length
 $SiteID = $TargetFolder.substring($TopFolderLength,$TargetFolderLength - $TopFolderLength)
 Write-host "`r` "
 [System.Reflection.Assembly]::LoadFile( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll")
 $iis = new-object Microsoft.Web.Administration.ServerManager
 foreach ($site in $iis.sites){
  if  ($site.id -eq $SiteID){
   $SiteName = $Site.Name
   break
  }
 }
 return $SiteName
}
Archive_IIS_Log_Files
 
If you find this script useful please donate generously.

Tuesday, April 5, 2011

Autonomy Interwoven TeamSite -> TortoiseSVN integration process

The following Perl script synchronises the TeamSite ‘STAGING’ area with SVN repository.


#!/usr/bin/perl
####################################################################
            # Purpose:                   Teamsite -> SVN integration
            # Args:                        $iwwapath
####################################################################
use POSIX qw(getcwd strftime);
use Cwd;
    try {
      my $dir = $ARGV[0];
      ## Constants declaration
      my $odLogIndex = "E:\\Req_Sys_SW\\Interwoven\\OpenDeployNG\\log\\rcv. appsPROD_flist.RUNDEPOLY.branch.log";
      my $odLog = "E:\\Req_Sys_SW\\Interwoven\\OpenDeployNG\\log\\rcv.appsPROD_flist.RUNDEPOLY.SourceServer.to. branch.log";                          
      my $username = "SVNuser";  #User should have access to all SVN applications Trunk folders
      my $password = "****";
      my $drive = "f:";
      my $share = "teamsite_integration";
      my $appDir = "application";
      my $target = "checkout";
      my $webdev = "\\inetpub\\webdev\\";
      my $commitSVN = " --message \"Automatic Teamsite Update\"";
      ##
      my $workDir = $drive . "\\" . $share . "\\" . $appDir . "\\";
      my $scriptLog = $0;
      my $currentLocation = substr($scriptLog, 0, get_last_pos($scriptLog,"\\"));
      my @svn, @app, @devSite, @url, $line, $drive, $appVal, $svnVal, $tmpVal, $tmpApp;
      my $pos1, $pos2, $devSiteVal, $appIndex, $replace , $find, $i;
      my $chkDirExists, $chkFolders, $lid, @folders, $chkFolderExists;
      my $msg = "";
      my $cmd = "";
      my $tmpTarget = "";
      my $beginJob = 0;
      my $endJob = 0;
      my $count_files = 0;
     
                  $commitSVN = $commitSVN . " --username " . $username . " --password " . $password . " --non-interactive";
                  $conf::date_format = "%d/%m/%Y";
                  $conf::time_format = "%H:%M:%S";
                  $scriptLog =~ s/.pl/.log/g;
                  $app[0] = "app1";
                  $app[1] = "app2";
                  $app[2] = "app3";
                  $svn[0] = "svn://server/app1/Trunk/";
                  $svn[1] = "svn://server/app2/Trunk/";
                  $svn[2] = " svn://server/app3/Trunk/";
                  $devSite[0] = $drive . $webdev . "app1";
                  $devSite[1] = $drive . $webdev . "app2";
                  $devSite[2] = $drive . $webdev . "app3";
                  # Get Open Deploy Job ID
                              if(-e $odLogIndex && -r $odLogIndex) {
                                          open(IN, "<$odLogIndex") || die "Couldn't open $odLogIndex: $!";
                              }
                              while ($line = <IN>) {
                                          if ((index($line, "Elapsed Time=") > -1)) {
                                                                  $pos1 = index ($line, "Job ID=") + length("Job ID=");
                                                                  $pos2 = index ($line, "   Deployment leg=") - length("   Deployment leg=");
                                                                  $jobid = substr($line, $pos1, $pos2 - $pos1);
                                                      }
                              }
                              close(IN);
                              if (length($jobid) eq 0) {
                                          exit;
                              }       
                              $msg = "Step 1 of 9 : Get Open Deploy Job ID";
                              debug($scriptLog, $msg);
                              $msg = "Start: " . get_now_date() . " " . get_now_time();
                              debug($scriptLog, $msg);    
                              $msg = "Open Deploy Job ID: " . $jobid;
                              debug($scriptLog, $msg);
                              $msg = "Step 2 of 9 : Initialising - removing $target directory if exists";
                              debug($scriptLog, $msg);
                              $currentLocation = $currentLocation . "\\" . $target;
                              if (-d $currentLocation) {
                                $cmd = "rmdir " . $drive . "\\" . $share . "\\" . $target . " /S /Q" . " >> " . $scriptLog;
                                $msg = "Run: " . $cmd;
                                debug($scriptLog, $msg);
                               `$cmd`;
                              }
                              $tmpVal = "-- Processing file(";
                              $msg = "Step 3 of 9 : Extract all Received File/s to array";
                              debug($scriptLog, $msg);
                              if(-e $odLog && -r $odLog) {
                                          open(IN, "<$odLog") || die "Couldn't open $odLog: $!";
                              }
                              while ($line = <IN>) {
                                          if ($beginJob eq 0) {
                                                      if ((index($line, "Job ID=" . $jobid) > -1) && (index($line, "Elapsed Time=") <= -1)) {
                                                                  $beginJob = 1;
                                                      }
                                          }       
                                          if ($beginJob eq 1) {
                                                      if (index($line, $tmpVal) > -1) {
                                                                  $pos1 = index($line, $tmpVal) + length($tmpVal);
                                                                  $pos2 = index($line, ")", $pos1);
                                                                  $dir = substr($line, $pos1, $pos2 - $pos1);
                                                                  push(@flist, $dir);
                                                      }
                                          }
                                          if ((index($line, "Job ID=" . $jobid) > -1) && (index($line, "Elapsed Time=") > -1)) {
                                                      last;
                                          }       
                              }
                              close(IN);
                              $appIndex = 0;
                              foreach $tmpFile (@flist){
                                          $msg = "** Processing: " . $tmpFile . " **";
                                          debug($scriptLog, $msg);
                                          $count_files++;
                                          $i = 0; 
                                          foreach $tmpApp (@app){
                                                      if (index($tmpFile, $tmpApp) gt -1){
                                                                  $appVal = $app[$i];
                                                                  $svnVal = $svn[$i];
                                                                  $devSiteVal = $devSite[$i];
                                                                  last;
                                                      }       
                                                      $i++;
                                          }
                                          if ($appIndex eq 0) {
                                                      $msg = "Step 4 of 9 : Checking out";
                                                      debug($scriptLog, $msg);
                                                      $cmd = "SVN checkout " . $svnVal . " " . $drive . "\\" . $share . "\\" . $target . "\\" . $appVal . " --username \"Teamsite\" --non-interactive" . " >> " . $scriptLog;
                                                      $msg = "Run: " . $cmd;
                                                      debug($scriptLog, $msg);
                                                      `$cmd`;
                                                      $appIndex++;
                                          }
                                          $msg = "Step 5 of 9 : Updating $target from Teamsite deployment";
                                          debug($scriptLog, $msg);
                                          $tmpTarget = $tmpFile;
                                          $replace = "\\\\" . $appDir . "\\\\";
                                          $find = "\\" . $target . "\\";
                                          $tmpTarget =~ s/$replace/$find/g;
                                          $cmd = "md " . substr($tmpTarget, 0, get_last_pos($tmpTarget, "\\")) . " >> " . $scriptLog;
                                          $msg = "Run: " . $cmd;
                                          debug($scriptLog, $msg);
                                          `$cmd`;
                                          $tmpVal = $tmpFile;
                                          $replace = "\\\\" . $appDir . "\\\\" . $appVal . "\\\\";
                                          $find = "\\" . $appDir . "\\generic\\";
                                          $tmpVal =~ s/$replace/$find/g;
                                          #Update this file from the current sub-site
                                          if (-e $tmpFile ) {
                                                      $cmd = "COPY " . $tmpFile . " " . $tmpTarget . " /Y" . " >> " . $scriptLog;
                                                      $msg = "Run: " . $cmd;
                                                      debug($scriptLog, $msg);
                                                      `$cmd`;
                                          } else {
                                                      $msg = "Warning: File " . $tmpFile . " not exists.";
                                                      debug($scriptLog, $msg);
                                          }
                                          $tmpVal = substr($tmpFile, index($tmpFile, "\\" . $appVal . "\\") + length("\\" . $appVal . "\\"), length($tmpFile));
                                          $devSiteVal = $devSiteVal . "\\" . $tmpVal;
                                          $cmd = "md " . substr($devSiteVal, 0, get_last_pos($devSiteVal,"\\")) . " >> " . $scriptLog;
                                          $msg = "Run: " . $cmd;
                                          debug($scriptLog, $msg);
                                          `$cmd`;
                                          $msg = "Step 6 of 9 : Copying from " . $tmpTarget . " to " . $devSiteVal;
                                          debug($scriptLog, $msg);
                                          $cmd = "COPY " . $tmpTarget . " " . $devSiteVal . " /Y" . " >> " . $scriptLog;
                                          $msg = "Run: " . $cmd;
                                          debug($scriptLog, $msg);
                                          `$cmd`;
                                          $cmd = "DEL /A:H /F /S /Q " . $drive . "\\" . $share . "\\" . $target . " >> " . $scriptLog;
                                          $msg = "Delete all hidden files: " . $cmd;
                                          debug($scriptLog, $msg);
                                          `$cmd`;
                                          $msg = "Step 7 of 9 : Adding back to SVN";
                                          debug($scriptLog, $msg);                            
                                          $chkDirExists = substr($tmpTarget, 0, get_last_pos($tmpTarget, "\\")) . ".svn";
                                          $msg = "Directory " . $chkDirExists;
                                          if (-d $chkDirExists ) {
                                                      $msg = $msg . " exists.";
                                              debug($scriptLog, $msg);
                                              $cmd = "SVN add " . $tmpTarget . " --force" . " >> " . $scriptLog;         
                                              $msg = "Run: " . $cmd;
                                                      debug($scriptLog, $msg);
                                                      `$cmd`;
                                                      $msg = "Step 8 of 9 : Committing back to SVN";
                                                      debug($scriptLog, $msg);
                                                      $cmd = "SVN commit " . $tmpTarget . $commitSVN . " >> " . $scriptLog;
                                                      $msg = "Run: " . $cmd;
                                                      debug($scriptLog, $msg);
                                                      `$cmd`;
                                          } else {
                                              $msg = $msg . " not exists.";
                                                      debug($scriptLog, $msg);                                                                           
                                                      $lid = $drive . "\\" . $share . "\\" . $target . "\\" . $appVal;
                                                      $chkFolders = substr(substr($tmpTarget, 0, get_last_pos($tmpTarget, "\\")), length($lid) + 1, length(substr($tmpTarget, 0, get_last_pos($tmpTarget, "\\"))));
                                                      @folders = split /\\/, $chkFolders;
                                                      $i = scalar @folders;
                                                      if ($i gt 0) {
                                                                  $chkFolderExists = $lid;
                                                                  foreach $dir (@folders)
                                                                              {       
                                                                                          $chkFolderExists = $chkFolderExists . "\\" . $dir;
                                                                              $cmd = "SVN add " . $chkFolderExists . " --force" . " >> " . $scriptLog;
                                                                              $msg = "Run: " . $cmd;
                                                                              debug($scriptLog, $msg);
                                                                              `$cmd`;
                                                                              $msg = "Step 8 of 9 : Committing back to SVN";
                                                                                          debug($scriptLog, $msg);
                                                                                          $cmd = "SVN commit " . $chkFolderExists . $commitSVN . " >> " . $scriptLog;
                                                                                          $msg = "Run: " . $cmd;
                                                                                          debug($scriptLog, $msg);
                                                                                          `$cmd`;
                                                                              last;   
                                                                              }
                                                      }
                                          }                                                                   
                              }
                              $msg = "Step 9 of 9 : Finalising - removing $target directory";
                              debug($scriptLog, $msg);
                              $cmd = "rmdir " . $drive . "\\" . $share . "\\" . $target . " /S /Q" . " >> " . $scriptLog;
                              $msg = "Run: " . $cmd;
                              debug($scriptLog, $msg);
                              `$cmd`;
                              $msg = "End: " . get_now_date() . " " . get_now_time();    
                              debug($scriptLog, $msg);
                              $msg = "Processed: " . $count_files . " file/s.";        
                              debug($scriptLog, $msg);
                              $msg = "____________________________________________________________________";
                              debug($scriptLog, $msg);                                    sub debug {
                  open(OUT, ">>@_[0]") || return;
                  chmod(0664, "@_[0]");
                  print OUT "@_[1]\n";
                  close(OUT);
      }
     
      sub get_now_date{
                  return POSIX::strftime($conf::date_format, localtime(time()));
      }
     
      sub get_now_time{
                  return POSIX::strftime($conf::time_format, localtime(time()));
      }
     
      sub get_last_pos{
                  $pos = -1;
                  while (($pos = index(@_[0], @_[1], $pos)) > -1){
                              $pos++;
                              $pos_last = index(@_[0], @_[1], $pos);
                              if ($pos_last <= -1){
                                          $pos_last = $pos;
                              }
                  }
                  return $pos_last;       
      }                   
    } catch Error with {
           my $ex = shift;
           $msg = "** Error: " . $ex . " **";
           debug($scriptLog, $msg);
    };
All (dynamic) application files should be imported initially to the TeamSite branch.  The script above should be called by the workflow associated with the TeamSite branch after content approval phase.  The process consists of the following steps:
·         Find OpenDeploy Job ID from the index log file.
·         Extract all received file/s referenced by the Job ID from the OpenDeploy log file.
·         Remove the checkout folder from the integration target location
·         Checkout appropriate application files into checkout folder
·         Update checkout files from the TeamSite deployment
·         Promote updated file/s to the appropriate environment (eg. DEV)
·         Delete all hidden files as required by SVN framework
·         Add updated file/s to the appropriate SVN repository
·         Commit updated file/s to the SVN repository
·         Remove temporary created checkout directory
The TeamSite->SVN integration area (\\destinationServer\Teamsite_Integration shared folder on the destination server) shows below


If you find this script useful please donate generously.