Windows Powershell I

Some of my posts have been laced with sarcasm but I could not help it. This is about the lack of interest in programming skills amongst my offshore colleagues. It is widely known that the offshore industry is driven my monetary goals. Business is the driving force here.
So we do not generally have to work through books like the ‘Structure and Interpretation of Computer Programs’ or algorithms to find employment. The interview process here favors UI frameworks and tools like Portals and ESB’s. The fundamentals are lost forever.
That is the prelude to the following drab Windows PowerShell program that I had to write recently. Not out of compulsion but out of curiosity. The PowerShell seems to pack a wealth of tricks and features and I discarded my original DOS shell script.

The shell script follows.


$script:utilScript = Get-Script | split-path -leaf -resolve

function isExists( $file ){

   
    try{
        $exists = test-Path $file
        
        if( $exists ){
        
            #log "$file exists"
            
        }else{
        
            #log "$file does not exist"
        
        }
    }catch {
    
        logException( $error )
        
    }
    return $exists
}


   
function filterFiles( $patterns, $source ){


        if( ( $patterns -eq $null ) -or ( $source -eq $null ) ){
        
            log "$utilScript The filter pattern or directory is null"
            
            return
            
        }else{
            
            $epfiles = Get-Childitem "C:\target\filter" | where {$_  -match $patterns}
            
        }
        return $epfiles

}


   
function deleteFiles( $files, $source ){

    try{

        if( ( $files -eq $null ) -or ( $source -eq $null ) ){
        
            log "$utilScript The files to be deleted or the directory is null"
            return
            
        }
        
        $files | foreach-object -process {

            if( isExists "$source\$_" ){

                log "$utilScript Deleting the file $source\$_"
                
                Remove-Item $source\$_
              
                log "$utilScript Deleted the file $source\$_"
            
            }else{
            
                log "$utilScript $source\$_ does not exist. $_ cannot be deleted"
            }
        }
        
    }catch {
    
        logException( $error )
        
    }

}



   

   
function moveFile( $files, $source, $target ){

    try{
    
            
            if( ( $files -eq $null ) -or ( $source -eq $null ) -or ( $target -eq $null ) ){
            
                log "$utilScript moveFile The file, source or target is null"
                return
                
            }
            
            foreach( $file in $files ) {
            
                if( -not ( $null -eq $file ) ){#Redundant

                    log "$utilScript Trying to move the file $source\$file to the target $target\$file"

                    if( ( isExists "$source\$file" ) -and ( -not ( isExists "$target\$file" ) ) ){

                        move-item -path $source\$file -destination $target\$file
                      
                        log "$utilScript Moved the file $source\$file to the target $target\$file"
                    
                    }else{
                    
                        log "$utilScript $source\$file does not exist or $target\$file is already present . $file cannot be moved to $target"
                    }
                }
            }
             
    }catch {
    
        logException( $error )
    }
}



   
function copyFile( $files, $source, $target ){

    try{
    
            
            if( ( $files -eq $null ) -or ( $source -eq $null ) -or ( $target -eq $null ) ){
            
                log "$utilScript The file, source or target is null"
                return
                
            }
            $files | foreach-object -process {

                if( isExists "$source\$_" ){

                    log "$utilScript $source\$_ exists in the file system"

                    log "$utilScript Copying the file $source\$_ to the target $target\$_"
                    
                    Copy-Item -path $source\$_ -destination $target\$_
                  
                    log "$utilScript Copied the file $source\$_ to the target $target\$_"
                
                }else{
                
                    log "$utilScript $source\$_ does not exist. So it cannot be copied to $target"
                }
            }
             
    }catch {
    
        logException( $error )
    }
}


function createDirectory( $directories, $target ){

    try{

            
            if( ( $directories -eq $null ) -or ( $target -eq $null ) ){
            
                log "$utilScript The directory, or target is null"
                return
                
            }

            $directories | foreach-object -process {

                if( -not ( isExists "$target\$_" ) ){

            
                    log "$utilScript Creating $directories in $target"
                    
                    new-item -path $target -name $_ -itemtype directory
                    
                    log "$utilScript Created $directories in $target"
                
                }else{
                
                    log "$utilScript $target\$_ already exists. So it cannot be created again"
                }

            }

    }catch {
    
        logException( $error )
    }

}


   
function searchFiles( $files, $characters ){

    $isSuccess = $FALSE

    try{

            if( ( $files -eq $null ) -or ( $characters -eq $null ) ){
            
                return $isSuccess
                
            }
            
            foreach( $file in $files ) {

                    log "$editScript Searching $file for $characters"

                    $reader = new-object System.IO.StreamReader -ArgumentList $file
                    
                    
                
                    do {
                        if ( -not ( $line.length -eq 0  ) ){
                        
                                if ($line -match $characters)  {
                                
                                    $isSuccess = $TRUE

                                    log "$editScript The search for $characters in the line $line in $file succeeds "
                        
                                    break

                                }     

                        }
                   }while( ( [String] $line = $reader.readline()) -ne $null  )
                    
                   $reader.Close()
            }

    }catch {
    
        //Log Exception
    }
    
    return $isSuccess

}

I picked up this function from an example and it helps me log the name of executing script


function Get-Script{

if($myInvocation.ScriptName) { $myInvocation.ScriptName }

else { $myInvocation.MyCommand.Definition }

}

Exploring the PowerShell scripting language will help you keep in touch with the duller side of the IT industry 🙂

Update : The functions to move a file and search for patterns in a file are rewritten

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: