Posts

Showing posts with the label Powershell

How to send emails in outlook using power shell

Powershell is one of the power tool in windows through which we can perform alot of automation through COMObjects. In this example, we are using COMObject for Outlook App installed in the system to perform automation. Note : Outlook App should be installed in the system <#Read Me: Developed By: Name:Vaneeswaran N www.vaneeswaran.com ##Function Name : Send_Email ##Descrption : Used to Send Mail ##Argument : Subject and Body of the Mail #> function Send_Email( $To , $Subject , $Body ) { echo "Sending Email Started.... " try { $olFolderInbox = 6 $Outlook = New-Object -ComObject Outlook.Application $Mail = $Outlook .CreateItem(0) $Mail .To = $To $Mail .Subject = $Subject $Mail .Body = $Body if ( $Body .length -gt 10) #Check to mail body is more than 10 char { ...

How to keep desktop awake

The following script will keep your desktop awake,It basically sends caps lock signal to the system periodically. Just copy the code and save it as a power shell (.ps1) file and execute it. If you face any permission issue''s while executing this , then simply open " Windows PowerShell ISE " and copy the code. <#Read Me: Developed By: Name:Vaneeswaran N www.vaneeswaran.com #> $myshell = New-Object -com "Wscript.Shell" for ( ;; ) { Start-Sleep -Seconds 60 $myshell.sendkeys( "{NUMLOCK}" ) }

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) ” ...