Posts

Showing posts from 2016

Touch command in powershell

The following power shell program is the similar to the Touch Command in Unix. It has the following features. create new file modify time stamp <#Read Me: Developed By: Name:Vaneeswaran N www.vaneeswaran.com #> $Function_Exit_Status = $false #Variable to store the exit status of the function function Touch_File ( $filename ) { $global:Function_Exit_Status = $false if (! [System.IO.File] ::Exists( $filename )) { echo "" > $filename $global:Function_Exit_Status = $true } else { $update = get-date try { Set-ItemProperty -Path $filename -Name LastWriteTime -Value $update $global:Function_Exit_Status = $true } catch { Log_Exception $( $_ .Exception.GetType().FullName) $( $_ .Exception.Message) $global:Function_Exit_Status = $false

Logger in Powershell

The below functions can be used as Logger and Exception Logger in power shell. The Logger function can be used to log the message in a file The Log_Exception function can be used to log the exception thrown at any try{} .. catch{} statements. The Test_Exception function is a driver function  < #Read Me: Developed By: Name:Vaneeswaran N www.vaneeswaran.com #> $Function_Exit_Status = $false #Variable to store the exit status of the function function Logger ($Log_Message , $Log_File) { try { $log_str = "$(Get-Date -format " yyyy MMM d hh:mm:ss ") $Log_Message " echo $log_str >> $Log_File } catch { write-host “ Caught an exception: ” -ForegroundColor Red write-host “ Exception Type: $ ($_.Exception. GetType ().FullName) ” -ForegroundColor Red write-host “ Exception Message: $ ($_.Exception.Message) ”