Extremely Serious

Author: ron (Page 17 of 33)

Usefull HTML Entities for Tracking Tasks

Entity Display Description
☐ Ballot box
◻ Empty Box
◻ Empty Box
□ Square
□ Square
☑ Ballot box with check
✓ Check
✓ Check
☒ Ballot box with cross
⊠ Box with cross
⊠ Box with cross
✗ Cross
✗ Cross

Sample Show Balloon Notification for PowerShell

A sample implementation of windows balloon notification for powershell is as follows:

Add-Type -AssemblyName System.Windows.Forms

function FnShowBalloon {

    [CmdLetBinding()]
    param($title, 
           $message, 
           [string] $icon = 'info', 
           [int] $delay = 20000, 
           [int] $sleep=0)

    Switch($icon.ToString().ToLower()) {
        'warn' {$iconInstance = [System.Windows.Forms.ToolTipIcon]::Warning}
        'error' {$iconInstance = [System.Windows.Forms.ToolTipIcon]::Error}
        'info' {$iconInstance = [System.Windows.Forms.ToolTipIcon]::Info}
        default {$iconInstance = [System.Windows.Forms.ToolTipIcon]::None}
    }

    $notification = New-Object System.Windows.Forms.NotifyIcon
    $path = (Get-Process -id $pid).Path
    $notification.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
    $notification.BalloonTipIcon = $iconInstance
    $notification.BalloonTipTitle = $title
    $notification.BalloonTipText = $message
    $notification.Visible = $true
    $notification.ShowBalloonTip($delay)

    if ($sleep -gt 0) {
        Start-Sleep -s $sleep
        $notification.Dispose()
    }
}

The FnShowBalloon  function can be used as follows:

FnShowBalloon -title "Hello World" -message "This is a sample message" -icon info

Recommended Way to Redirect the Output to a Text File in PowerShell

Instead of using > to redirect the output into a file, pipe it to out-file cmdlet.

The out-file cmdlet allows some useful parameters as follows:

Parameter Argument Description
-Append Adds the output to the end of an existing file.
-Encoding Encoding Specifies the type of encoding for the target file. The default value is utf8NoBOM.

The acceptable values for this parameter are as follows:

ascii: Uses the encoding for the ASCII (7-bit) character set.
bigendianunicode: Encodes in UTF-16 format using the big-endian byte order.
oem: Uses the default encoding for MS-DOS and console programs.
unicode: Encodes in UTF-16 format using the little-endian byte order.
utf7: Encodes in UTF-7 format.
utf8: Encodes in UTF-8 format.
utf8BOM: Encodes in UTF-8 format with Byte Order Mark (BOM)
utf8NoBOM: Encodes in UTF-8 format without Byte Order Mark (BOM)
utf32: Encodes in UTF-32 format.

-FilePath Path Specifies the path to the output file.

To redirecting the output of the dir command to dir.txt file, use the following command:

dir | out-file -encoding ascii -filepath "dir.txt"

Java 6 to use TLS 1.2 by using BouncyCastle

  1. Download the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6. (i.e. if the link is already dead then download this instead jce_policy-6.)
  2. Extract the downloaded archive and follow the installation procedure found in the README.txt.
  3. Download the following Bouncy Castle libraries:
  4. Place the downloaded libraries into ${JAVA_HOME}/jre/lib/ext directory.
  5. Update the java.security (i.e. found in ${JAVA_HOME}/jre/lib/security directory.) file to have the following as the priority:
    security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider
    security.provider.2=org.bouncycastle.jsse.provider.BouncyCastleJsseProvider
    

    Adjust the other security.provider to start from 3 like the following:

    security.provider.3=sun.security.provider.Sun
    security.provider.4=sun.security.rsa.SunRsaSign
    security.provider.5=com.sun.net.ssl.internal.ssl.Provider
    security.provider.6=com.sun.crypto.provider.SunJCE
    security.provider.7=sun.security.jgss.SunProvider
    security.provider.8=com.sun.security.sasl.Provider
    security.provider.9=org.jcp.xml.dsig.internal.dom.XMLDSigRI
    security.provider.10=sun.security.smartcardio.SunPCSC
    security.provider.11=sun.security.mscapi.SunMSCAPI
    
  6. Try the following Java code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.ProtocolException;
    import java.net.URL;
    
    public class Main {
    
        public static void main(String[] args) {
            try {
                URL url = new URL("https://www.nist.gov/");
                System.out.println(url);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                System.out.println(connection.getResponseCode());
                StringBuilder response = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    
                try {
                    String line = reader.readLine();
                    while (line != null) {
                        response.append(line);
                        line = reader.readLine();
                    }
                } finally {
                    reader.close();
                }
    
                System.out.println(response.toString());
            }
            catch(ProtocolException exception) {
                exception.printStackTrace();
            }
            catch(IOException exception) {
                exception.printStackTrace();
            }
        }
    }

    You should be able to access it without any SSL handshake error.

    Alternatively, you can opt to use the official JDK 6u121 via the Java SE 6 Advanced and Java SE 6 Support if you have availed of it.

Extension Methods

The method that allows to add methods to existing type without creating a new derived type, recompiling, or otherwise modifying the original type.

A method signature template with a return value and an argument

public static <T_RETURN> <METHOD_NAME>(this <T_TYPE> <VAR_NAME>, <T_ARG1_TYPE> <ARG1_NAME>)
Token Description
T_RETURN The type of the return value.
METHOD_NAME The desired name of the method.
T_TYPE The existing type to extend.
VAR_NAME The holder of an instance of the T_TYPE
T_ARG1_TYPE Type of the first argument.
ARG1_NAME The name of the first argument.

This method is actually a static method but the first argument has the keyword this and the target type to extend. Also it holds the instance of the target type.

The T_RETURN and the argument part of the template are optional. Also the argument is not limited to just one, you can have as many as required by your extension.

Class Property

Description

A member of a class that provides flexibility for exposing private fields.

Sample Property Declaration

//backing private field
private string prop;

public string ReadWriteProp {
    get {
        return prop;
    }
    set {
        prop = value;
    }
}

public string ReadOnlyProp {
    get {
        return prop;
    }
}

public string WriteOnlyProp {
    set {
        prop = value;
    }
}

Sample Auto-Implemented Property Declaration

A more concise property declaration especially if theres no additional logic required for the accessors.

public string ReadWriteProp {
    get; set;
} 

public string ReadOnlyProp {
    get; private set;
} 

public string WriteOnlyProp {
    private get; set;
}
« Older posts Newer posts »