Wednesday 27 July 2011

Uploading all files from Directory to FTP from PowerShell Script

Issue:
     To Upload multiple files from Directory which matches the criteria like '.txt' '.exe' MIME Type.

Solution:
     To read all file from the Directory and uploading one by one. The code will be,
             #we specify the directory where all files that we want to upload are contained
             $Dir=<Path of the Directory>
             #ftp server
             $ftp = <FTP Path>
             $user = <UName>
             $pass = <Passwd>
             $webclient = New-Object System.Net.WebClient
             $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
             #list every sql server trace file
             foreach($item in (dir $Dir "*.txt"))
            {
                   "Uploading $item..."
                   $uri = New-Object System.Uri($ftp+$item.Name)
                   $webclient.UploadFile($uri, $item.FullName) 
            }


Uploading a file to FTP from PowerShell Script

Issue: 
    To Upload a file to FTP with proper credentials.

Solution:
    Giving the Address and proper credentials, and code will be,
         
      $File=<File to Upload>
      #ftp server
      $ftp = <FTP Path + File Name>
      $user = <UName>
      $pass = <Passwd>
      $webclient = New-Object System.Net.WebClient
      $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
      "Uploading $item..."
      $uri = New-Object System.Uri($ftp)
      $webclient.UploadFile($uri, $File)

[Note:]
Files will get uploaded properly if and only if the FTP allows You with Access Permissions and Credentials (if having).

Execution of script disabled by the system in PowerShell


Issue:
     Execution of the Shell Script is blocked by the Rules of PowerShell.

Check:
     Start Powershell prompt as the Administrator, and type the following,
             Get-ExecutionPolicy
     If it shows as 'Restricted'.
     You have to reset the policy to allow You to execute by typing,
Syntax

Set-ExecutionPolicy [-ExecutionPolicy] {<Unrestricted> | 
<RemoteSigned> | <AllSigned> | <Restricted> | <Default> |
<Bypass> | <Undefined>} [[-Scope] {<Process> | <CurrentUser>
| <LocalMachine> | <UserPolicy> | <MachinePolicy>}] [-Force]
 [-Confirm] [-WhatIf] [<CommonParameters>]   
 
 Description
          The Set-ExecutionPolicy cmdlet changes the user preference for the Windows PowerShell execution policy. 


Parameters


-ExecutionPolicy <ExecutionPolicy>

Specifies the new execution policy. Valid values are:
-- Restricted: Does not load configuration files or run scripts. "Restricted" is the default execution policy.
-- AllSigned: Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.
-- RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
-- Unrestricted: Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
-- Bypass: Nothing is blocked and there are no warnings or prompts.
-- Undefined: Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.




 

Sending Mail thru gmail server in PowerShell

To Send the mail,
 
 $SmtpClient = new-object system.net.mail.smtpClient
 $smtpclient.Host = <Host> like smtp.gmail.com, smtp.live.com
 $smtpclient.Port = <Port No>
 $smtpclient.EnableSsl =<Boolean Value> like, $true or $false
 $smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID)
 $smtpClient.Send(<From Mail>,<ToMail>,<Subject>,<Body>)

You can run it with PowerShell Script or Direct Console itself.

Monday 25 July 2011

Using Configuration File with Windows PowerShell


Issue: 
    To use the config file for adding the lookup values for PS Scripting.

Solution:

SETTINGS.TXT
#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
[General]
MySetting1=value

[Locations]
InputFile="C:\Users.txt"
OutputFile="C:\output.log"

[Other]
WaitForTime=20
VerboseLogging=True

POWERSHELL COMMAND
#from http://tlingenf.spaces.live.com/blog/cns!B1B09F516B5BAEBF!213.entry
#
Get-Content "C:\settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }



Then,
After executing the code snippet, a variable ($h) will contain the values in a HashTable.
Name                           Value
------                              -----
MySetting1                     value
VerboseLogging                 True
WaitForTime                    20
OutputFile                     "C:\output.log"
InputFile                      "C:\Users.txt"


*To get an item from the table use the command $h.Get_Item("MySetting1").*


Saturday 23 July 2011

How to create Executable jar for an Applet


Creating an executable jar file for an applet

      To create an executable a jar file there should be a main class (a class with a main method) inside the jar to run the software. An applet has no main method since it is usually designed to work inside a web browser. To make the jar file executable I had to do two things:


First, I created a new class inside the jar file as a main class which I called StartClass. The job of this class is to call and host the applet; hence if the jar file is executed, the applet method is invoked. Check out the following code that I used supported by few comments to explain what it does.


package CaesarCodePackage;
 public class StartClass {

     public static void main(String [] args)
      {
         // create an object of type CaesarCode which is

         //the main applet class
         CaesarCode theApplet = new CaesarCode();
         theApplet.init();   // invoke the applet's init() method
         theApplet.start();  // starts the applet

         // Create a window (JFrame) and make applet

         //the content pane.
          javax.swing.JFrame window = new javax.swing.JFrame

          ("Caesar's Cipher");
          window.setContentPane(theApplet);
          window.setDefaultCloseOperation

          (javax.swing.JFrame.EXIT_ON_CLOSE);
          window.pack();              // Arrange the components.
          window.setVisible(true);    // Make the window visible.
        }

}

Second, I had to edit the MANIFEST.MF file and add the following line to define my main class correctly

Main-Class: CaesarCodePackage.StartClass

please note that ‘CaesarCodePackage’ is my package’s name and ‘StartClass’ is the name of my main class (the one with the main method that I used to call the applet). If this step is not performed correctly the following error is more likely to occur if you try to execute the jar file:
Failed to load Main-Class manifest attribute
A final note would be that, to successfully execute jar files on your system, you need to have JRE installed and the Jar files must be associated with javax.exe (javax.exe is located inside the bin directory of your JRE folder e.g. C:\Program Files\Java\jre6\bin)

Wednesday 20 July 2011

Avoid Flickering in Java Graphics Program

Issue:
 Flickering is a big problem while drawing something on screen in Java. Flickering normally occurs when drawing images one after another. Though flickering is a big problem people might think that it could be very complicated to avoid or remove it completely, but it is quite easy to that. By making some simple changes in your code you can completely remove flickering in your animations.

What causes flickering in Java? 
Before flickering problem can be solved, we should know what causes it? As we know the paint() method in Java first clears the screen and then paints a new frame on the window. So there are two operations takes place when we can the paint() or repaint() method. So some times screen gets updated between the clearing of window and painting of new frame and in that moment of time we can see the cleared window and that makes it appear to be flicker.

Remove Flickering using Double Buffering:
As now we know that the flickering occurs because of refreshing of screen between the clearing of window and painting of new frame, so the solution is to paint the window in such a way that screen doesn't get refreshed before window get completely painted.
This can be done using the Double Buffering. To implement Double Buffering a BufferedImage class. A BufferedImage object is like an Image object except it has some method that makes it easier to work with.

Code in which flickering problem occur

public void run(){
            try{
                Thread.sleep(50);
            }
             catch(Exception e){ }
            repaint();
}
public void paint(Graphics g){
         animation(g);          //this function performs all animation
}


Code for Declaration of Buffered Image object

 
BufferedImage  bf = new BufferedImage( this.getWidth(), this.getHeight(), 
BufferedImage.TYPE_INT_RGB);

Code for Remove flickering in paint method by using Buffered Image
public void run(){
           
            while(true){
            try{
                  Thread.sleep(50);
            }catch(Exception ex){
                 
            }
            paint(this.getGraphics());
            }
      }
public void paint(Graphics g){
           
            animation(bf.getGraphics()); //bf is the BufferedImage object
            g.drawImage(bf,0,0,null);
      }

This is how a Buffered Image object is use to prevent the flicker problem. When calling the animation() method a graphics object is extracted form Buffered Image. This Buffered Image graphics object is same as the normal(window) graphics object except this graphics object contains information about the Buffered Image instead of the actual window. That means the animation method paints the new frame on the Buffered Image instead of the actual window. So far the window still holds the old frame and the Buffered Image holds the new frame. Than a call is made to the drawImage() method of graphics object of the window. So now in only one operation the whole content of window is replace by  the content of Buffered Image. This is known as an atomic operation and the screen can not get refreshed while the window is partially painted. This is how the flickering problem can be solved and this code can be use for any type of application.

Now to understand why this code does not use repaint() method we need to understand the repaint() method.

Working of repaint() method:
Some people might think that repaint() method calls to the paint() method, eventually it does but it is not the whole story though. A repaint() method actually calls the update() method and the default update() method then calls to the paint() method. If you Java code did not override update(), the default implementation of update() clears the component's background and simply calls paint().
So if we want to use repaint() method we have to override update() method to avoid flickering.

public void run(){
           
            while(true){
            try{
                  Thread.sleep(50);
            }catch(Exception ex){
                 
            }
              repaint();
            }
      }

public void update(Graphics g){
       paint(g);
}

public void paint(Graphics g){
           
            animation(bf.getGraphics()); //bf is the BufferedImage object
            g.drawImage(bf,0,0,null);
}
 You can download code from,
http://javacodespot.110mb.com/NoFlickering.zip

Tuesday 19 July 2011

Some screenshots missing in Problem steps Recorder

Issues:
     Some of the first few screenshots missing while using 'Problems Steps Recorder' feature in Windows

Solution:
     Go to Start-->Run, type 'psr'.
     Opens Problem Steps Recorder, Navigate to Settings, Max. no of screenshots could take. You can
increase/decrease these number.

How to Check Oracle Version

Issue:
    To check the Oracle Version that You have installed in Your Machine.

Soln:
    Start SQL PLUS Editor, Give the query,
               select * from v$version;

Monday 18 July 2011

How to block images and load as Plain Text

Tips:
In Firefox, Go to Tools-->Options-->Contents. Unselect 'Load Images automatically'.

It will block all images from the Webpages and You could allow some favorite sites that You would like.

Wednesday 13 July 2011

Record Steps to Reproduce a Problem in Windows Server 2008, even in Remote Desktop

Issue:
     To Record the snapshot of events doing to Rollback if any problem occurs.

Solution:
     Start Windows-->Type Search Box 'Record Steps to Reproduce a Problem'--> Click on "Record Steps to Reproduce a Problem"
     Start Recording and minimize it. Wow! You have done.
     Stop Recording when You have finished the critical operations done in the System.

[Note: This feature is available built-in only on the Windows Version 2008,2008 R2, Windows 7]

Tuesday 12 July 2011

Network Error in Oracle Apps



Issue
FRM-92102 : A network error has occurred.The Forms Client has attempted to re-establish its connection to the Server 5 times without success.Please check the network connection and try again later.




Soln:
On trying 3-4 times, this error disappears and the Application gets started

Friday 8 July 2011

Editing Registry Contents

Note:
       See, Editing the Registry will cause serious errors in the System. The system may fall due to inconsistency.

       But, if You sure need to edit Registry, You can back-up registry by, Go to File-->Export.
       Later, You can go either Safe Mode or Last known Good Configuration and fix the Registry to the normal state.

Thursday 7 July 2011

Finding the Lookup Tables in Oracle 12.0.4

To find the Lookup Tables, Go to Functional Administrator-->Core Services
Give the Application Name (Here, Human Resources)and click Go.

Sunday 3 July 2011

About Updates in Firefox


Note:
     Some applications will not work, if Firefox has been either updated for security and increasing performance.

Tips:
     If You do not want any further updates from the Firefox web browser, You just disable them by,
Go to Tools-->Internet Options-->Advanced-->Update. There, You uncheck all checkboxes.

Issue:
    If it has been already updated, Firefox cannot rollback, just remove Firefox and re-install it.