Powershell Intro/Basics

1

Category :

Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of .NET Framework.

Perform administrative tasks on both local and remote Windows systems.

PowerShell output is always a .NET object, basically it could be any .NET object whose assembly is loaded into PowerShell including your own .NET objects.

Discoverability

Windows PowerShell makes it easy to discover its features. For example, to find a list of cmdlets that view and change Windows services, type:
Get-Command *-Service
After discovering which cmdlet accomplishes a task, you can learn more about the cmdlet by using the Get-Help cmdlet. For example, to display help about the Get-Service cmdlet, type:
Get-Help Get-Service
Most cmdlets return objects which can be manipulated and then rendered into text for display. To fully understand the output of that cmdlet, pipe its output to the Get-Member cmdlet. For example, the following command displays information about the members of the object output by the Get-Service cmdlet.
Get-Service | Get-Member

Online Help You can get online help for any command using the
Get-Help {command-name} -Online
Get-Help | Get-Service -Online

PowerShell syntax

semi colons

Not needed to terminate statements. You can however use them to separate statements on the command line.

escape character

The backtick (grave accent) represented as the character ` is the escape character within PowerShell. You can use this character to for example print tabs `t or escape characters with a special meaning like the $ character as in `$ or escaping quotes like `”.
You can also use the backtick character to span your statements over multiple lines, which can sometimes come in handy.

variables

Variables in PowerShell have a ‘$’ sign in front of them, like
$myvar = 'some value'
$myothervar = 42

single & double quotes

There actually is a distinction when using these. The single quote represents everything literally, the double quotes interprets what’s inside it.
$dummyvalue = 'dummy'
write 'test$dummyvalue'
write "test$dummyvalue"
The first one will print ‘test$dummyvalue’ the second one will print ‘testdummy’. And like most scripting language you can use the quotes to encapsulate each other like “soo he said ‘wow’, jus he did”.

brackets and colons/periods

The round brackets, parenthesis, ‘(‘ and ‘)’ can be used to specify order and to pass arguments to .NET methods. When calling function you defined in PowerShell you pass the arguments SPACE separated and without parenthesis since they could cause an error.

The square brackets can be used to access list/array members like most languages: The below will produce the output ‘1’ since it’s accessing the first members in the array.
$test = 1,2,3,4,5
write $test[0]
The other use case for these brackets is to define types and accessing .NET classes as in: will print the current date time. Like you can see here you can use a double colon to access properties of a class,
$test = [DateTime]
write $test::now
to access methods you can use the period character
$test = New-Object System.datetime
write $test
write $test.AddYears(2015)

Functions

So there is an important distinctions between calling functions and calling methods. Functions in PowerShell use spaces for the parameters and not brackets, which you might confuse in the beginning when you mix in calls to methods.

CmdLet v Script v Function

Script

A file containing a collection of commands which are executed. A script can contain functions.

Function

Is also a collection of commands which are executed but it must be present in the session to be executed.
Function Say-Hello {
  <#
    .Synopsis
      A brief summary of the function
    .Description
      A full description of the function
    .Parameter Name
      The name parameter
  #>

  Param(
    [String]$Name = "everyone"
  )

  Return "Hello $Name"
}

Cmdlet

A cmdlet is .NET code which performs a specific task without you having to write it out yourself.

my thanks to:

some links to good resources below:
https://diablohorn.com/2016/02/06/powershell-overview-and-introduction/

blog series referenced in previous link above:
https://www.darkoperator.com/powershellbasics/

https://docs.microsoft.com/en-us/powershell/scripting/powershell-scripting?view=powershell-6

Factory Method

0

Category : ,

The Factory completely hides/removes the process of creating objects from the client/caller.

Example

Lets say we have an eCommerce application and we have 2 payment gateways integrated with our application. Lets call these payment gateways BankOne and BankTwo.

Interface

We need a "Payment Gateway" interface to set the template/functionality that all payment gateways need to provide.

interface IPaymentGateway
{
    void MakePayment(Product product);        
}

Concrete implementations

Concrete implementations of Payment Gateways required:
public class BankOne : IPaymentGateway
{
    public void MakePayment(Product product)
    {
        // The bank specific API call to make the payment
        Console.WriteLine("Using bank1 to pay for {0}, amount {1}", product.Name, product.Price);
    }
}

public class BankTwo : IPaymentGateway
{
    public void MakePayment(Product product)
    {
        // The bank specific API call to make the payment
        Console.WriteLine("Using bank2 to pay for {0}, amount {1}", product.Name, product.Price);
    }
}
To be able to identify what payment mechanism has been selected, lets define a simple Enum PaymentMethod.
enum PaymentMethod
{
    BANK_ONE,
    BANK_TWO
}

Factory

Factory class to handle all the details of creating these objects.
public class PaymentGatewayFactory
{
    public virtual IPaymentGateway CreatePaymentGateway(PaymentMethod method, Product product)
    {
        IPaymentGateway gateway = null;

        switch(method)
        {
            case PaymentMethod.BANK_ONE:
                gateway = new BankOne();
                break;
            case PaymentMethod.BANK_TWO:
                gateway = new BankTwo();
                break;
        }

        return gateway;
    }
}
Our factory class accepts the selected payment gateway and then based on the selection it is creating the concrete payment gateway class. We have effectively abstracted out all these details from the client code. Otherwise every class that want to use a payment gateway would have to write all this logic.

Usage

Lets now look at how the client class can use this factory method to make the payment.
public class PaymentProcessor
{
    IPaymentGateway gateway = null;

    public void MakePayment(PaymentMethod method, Product product)
    {
        PaymentGatewayFactory factory = new PaymentGatewayFactory();
        this.gateway = factory.CreatePaymentGateway(method, product);

        this.gateway.MakePayment(product);
    }
}
Now our client class does not depend on the concrete payment gateway classes. It also does not have to worry about the creation logic of the concrete payment gateway classes. All this is nicely abstracted out in the factory class itself.


my thanks to the following article:
https://www.codeproject.com/Articles/874246/Understanding-and-Implementing-Factory-Pattern-i

Windows Containers and Docker

0

Category :

Windows Containers Fundamentals

  1. Containers wrap software up within in a complete file system that contains everything it needs to run: code, runtime, system tools and system libraries.
  2. Always run the same, regardless of the environment.
  3. Applications running in containers can’t interact or see other applications running in the host OS or in other containers.

Virtual Machines Vs Containers

Virtual machine

  1. standalone and has its own operating system, its own applications and its own resources.
  2. Each virtual machine uses its own OS, libraries, etc.
  3. occupy significant amounts of memory.

Containers

  1. do not contain any operating system
  2. take up fewer resources
  3. share the host operating system, including the kernel and libraries, so they don’t need to boot a full OS.

Windows Server Containers Vs Hyper-V Containers

Windows Server Container

  1. based on the Windows Server Core image.
  2. if we trust the code

Hyper-V Container

  1. based on the Windows Nano Server image.
  2. each container runs in a highly-optimized virtual machine, so that they provide a full secure isolation.
  3. kernel of the container host is not shared with other Hyper-V Containers.
  4. if we don’t trust the code

Docker

Windows Server 2016 can’t run Linux containers in Docker format but only Windows containers.

Docker Platform

  • Container Host: Physical or Virtual computer system configured with the Windows Container feature.
  • Container Image: A container image contains the base operating system, application, and all the application dependencies that are needed to quickly deploy a container.
  • Container OS Image: The container OS image is the operating system environment.
  • Container Registry: Container images are stored in a container registry, and can be downloaded on demand. It is a place where container images are published. A registry can be remote or on-premises.
  • Docker Engine: It is the core of the Docker platform. It is a lightweight container runtime that builds and runs your container.
  • Docker file: Docker files are used by developers to build and automate the creation of container images. With a Docker file, the Docker daemon can automatically build a container image.


my thanks to the following:
https://www.red-gate.com/simple-talk/sysadmin/virtualization/working-windows-containers-docker-basics/