All posts by Thiyagu

Powershell : How to Compare Two Files, and List Differences

What is Compare-Object cmdlet do?

The Compare-Object cmdlet compares two sets of objects. One set of objects is the “reference set,” and the other set is the “difference set”. The below format will be out of the comparison.

<=This indicates that property value appears only in the -ReferenceObject set.
=>Indicates that property value appears only in the -differenceObject.
==Indicates that both property value appears the same.

Example:

In the Compare-Object cmdlet, there are two required parameters -referenceObject and -differenceObject. For example, -referenceObject controls the master content, the file which holds all the information, while -differenceObject has the secondary file.

Demo

Let we create two files, the first (Master & Child list) is for reference and the second for comparison. Now we are ready to execute this Compare-Object script.

MasterList.txt

WaterMelon
Grapes
Mango
Oranges
Bananas
Cucumber
Guva

ChildList.txt

Mango
Apple
Oranges
Bananas
Cucumber
WaterMelon
Grapes

Example 1: To Compare Two Files, and List Their Differences

This command compares the contents of two text files. Here Apple is present in the second file (-differenceObject set) not in the first file (-ReferenceObject set)  so the output will be => and Guva is present in the first file and not in the second file, so the output will be <=.

Clear-Host
$strReference = Get-Content "D:\BLOG\Power Shell\Examples\Masterlist.txt"
$strDifference = Get-Content "D:\BLOG\Power Shell\Examples\ChildList.txt"
Compare-Object $strReference $strDifference

Example 2: To Compare Two Files, with -Parameters

Clear-Host
$strReference = Get-Content "D:\BLOG\Power Shell\Examples\Masterlist.txt"
$strDifference = Get-Content "D:\BLOG\Power Shell\Examples\ChildList.txt"
Compare-Object $strReference $strDifference -IncludeEqual
Optional Parameters

Optional Parameters

-CaseSensitiveThis indicates that comparisons should be case-sensitive.
-CultureSpecifies the culture to use for comparisons.
-PropertySpecifies an array of properties of the reference and difference objects to compare.
-ExcludeDifferentIndicates that this cmdlet displays only the characteristics of compared objects that are equal
-SyncWindowSpecifies the number of adjacent objects that this cmdlet inspects while looking for a match in a collection of objects.
-IncludeEqualIndicates that this cmdlet displays characteristics of compared objects that are equal. By default, only characteristics that differ between the reference and different objects are displayed.
-PassThruReturns an object representing the item with which you are working. By default, this cmdlet does not generate any output.

 

NOTE: If the reference set or the difference set is null ($null), Compare-Object generates a terminating error.

 

What do you think?

I hope you have got an idea about how to use PowerShell Compare-Object cmdlet. I would like to have feedback from the readers of my post. Your valuable feedback, question, or comments about this article are always welcome.

PowerShell : Continue Keyword

Continue Keyword in PowerShell

One of the things that is a bit confusing for powershell beginner is the Continue statement. This is partially because it does not act like it might in some other languages. Next question will arise, then what does the Continue statement do in Windows PowerShell? the answer is it returns flow to the top of the innermost loop that is controlled by a WhileFor, or Foreach statement. 

Example:

In the following example, program flow returns to the top of the While loop if value of $myValue variable is equal to 6. As a result, all the numbers between 1 and 10 are displayed except for 5:

while ($myValue -lt 10)
{
$myValue += 1
if ($myValue -eq 6) {Continue}
Write-Host $myValue 
}

Output:

1
2
3
4
5
7
8
9
10

The Continue keyword supports labels. A label is a name you assign to a statement in a script, so the continue will skip in the current loop and execute form the  assigned label location. Let we discuss more about the labels in upcoming articles.

ASP.NET Core Real Power: Features And Benefits

Introduction

ASP.NET Core 1.0 is not a continuation of ASP.NET 4.6. It is a whole new framework, a side-by-side project which happily lives alongside everything else we know. It is an actual re-write of the current ASP.NET 4.6 framework, but much smaller and a lot much more modular. There is no System. Web namespace anymore and everything which came with it.

It is the framework you want to use for web development with .NET. ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. If you have any experience with MVC or Web API over the last few years, you will notice some familiar features. ASP.NET Core 1.0 is a 1.0 release that includes Web API and MVC but doesn’t have SignalR or Web Pages. It doesn’t yet support VB or F#.

If you have any experience with MVC or Web API over the last few years, you will notice some familiar features.This sequence of tutorials, will help you write an application using ASP.NET Core that can create, edit, and view data from a database.

Advantages of ASP.NET Core

Open Source:

ASP.NET Core is open source and available on GitHub,Which help to reduce the cost of hosting.NET application.

Platform Independent (Cross Platform Support):

ASP.NET Core 1.0 is a big fundamental change to the ASP.NET landscape and it is open source. ASP.NET Core is supported on Windows, Mac and Linux OSX

Built from scratch:

ASP.NET Core is not like an enhancement to ASP.NET 4.5 but it has been built from scratch. A lot of new features have been added to it and some of the existing features has been dropped also. To support Cross Platform and improve performance some existing features have been removed.

Modular Web Framework:

Asp.Net core everything is managed using nugget package, so it’s easy to upgrade new framework without releasing new .net framework version. In ASP.NET Core, a community can just release new NuGet package version and you will get latest changes by updating your packages. The .NET developer has the ability to pick and choose which ASP.NET features to include in their solutions. This opt-in model allows developers to be more deliberate with regard to which libraries are included in their projects

Dependency Injection:

Dependency injection is integrated into the framework for achieving loose coupling between objects and their collaborators.Third party products such as Autofac should still be available as an alternative to the build-in ASP.NET DI functionality.

Combination of MVC and WEB.API

In previous versions of ASP.NET, MVC and WEB.API were based on different versions of the framework ie., System.Web.Mvc and System.Web.Http/System.Net.Http, respectively. This has been removed and its been integrated with ASP.NET CORE 1.0 MVC 6. There is now a single set of objects within a single namespace (Microsoft.AspNet.Mvc). This standardized approach simplifies developing both MVC and WEB.API endpoints.

What do you think?

I hope you have basic about ASP .NET Core and its advantage . I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

Introduction to ASP.NET Core

Introduction

ASP.NET Core is the new web framework from Microsoft. It is the framework you want to use for web development with .NET. ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. If you have any experience with MVC or Web API over the last few years, you will notice some familiar features.

ASP.NET Core 1.0 is a 1.0 release that includes Web API and MVC but doesn’t have SignalR or Web Pages.
It doesn’t yet support VB or F#.

What is ASP.NET Core

ASP.NET Core is an open source and cloud-optimized web framework for developing modern web applications that can be developed and run on across the many platforms like Windows, Linux and the Mac. It includes the MVC framework, which now combines the features of MVC and Web API into a single web programming framework.

What is New in ASP.NET Core?

ASP.NET Core ships entirely as NuGet packages. This allows you to optimize your application to include only the necessary NuGet packages rather than having all in application.

  • Open-source and community-focused.
  • A lightweight, high-performance, and modular HTTP request pipeline.
  • Ability to build and run on Windows, macOS, and Linux.
  • Ability to host on IIS, Nginx, Apache, Docker, or self-host in your own process.
  • ASP.NET Core apps can run on .NET Core or on the full .NET Framework.

What do you think?

I hope you have basic introduction and overview about ASP .NET Core. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

 

Power Shell – Containment Operators

Power Shell Containment Operators

The containment operators (-contains and -notcontains) are similar to the equal operators. However, the containment operators always return a Boolean value, even when the input is a collection. The conditional operator -Contains is similar to -eq, except it returns ‘True’ or ‘False’. -Contains is designed for situations where you wish to test for one particular item in a collection, array or a hash table.

OperatorsDescription
-containsReturns true when reference value contained in a collection
-notcontainsReturns true when reference value not contained in a collection
-inReturns true when test value contained in a collection
-notinReturns true when test value not contained in a collection

contains

Always returns a Boolean value. Returns TRUE only when the test value exactly matches at least one of the reference values. When the test value is a collection, the Contains operator uses reference equality. It returns TRUE only when one of the reference values is the same instance of the test value object.

Syntax: <Reference-values> -contains <Test-value>

Example: 1

Contains checks each item between the commas

$Name = "John", "Ray Matt", "Anthony" 
$Name -Contains "Ray Matt"

OUTPUT

True

Example: 2

-Contains requires exact equality

$Name = "John", "Ray Matt", "Anthony" 
$Name -Contains "Ray"

OUTPUT

False

-notcontains

The negative -NotContains is not as useful as -NotMatch. However, from what we have already learned the syntax is predictable.

Syntax: <Reference-values> -notcontains <Test-value>

Example:

$Name = "John", "Ray Matt", "Anthony" 
$Name -notContains "Ray"

OUTPUT

True

-in Operator

In operator return the boolean value if the test value appears in a collection of reference values. Always return as Boolean value. Returns TRUE only when the test value exactly matches at least one of the reference values else it will returns FALSE.

Syntax: <Test-value> -in <Reference-values>

Example: 1

$Name = "John", "Ray Matt", "Anthony" 
"Ray" -in $Name

Output:

False

Example: 2

$Name = "John", "Ray Matt", "Anthony" 
"Ray Matt" -in $Name

Output:

True

-notin Operator

In operator return the boolean value if the test value appears in a collection of reference values. Always return as Boolean value. Returns TRUE only when the test value not a exactly matches at least one of the reference values else it will returns FALSE.

Syntax: <Test-value> -notin \<Reference-values>

Example: 1

$Name = "John", "Ray Matt", "Anthony" 
"Ray" -in $Name

Output:

True

Example: 2

$Name = "John", "Ray Matt", "Anthony" 
"Ray Matt" -in $Name

Output:

False

PowerShell : Do-While & Do-Until Loop

Do-While & Do-Until Loop in PowerShell

Loops are an essential construct in any programming language as they enable a block of code to be repeatedly executed based on the condition. This can be useful for iterating through items in an object or repeatedly performing an operation. PowerShell supports involves setting a condition which either allows the loop to process as long as the condition is true or until it is met. 

  • The Do keyword works with the While keyword or the Until keyword to run the statements in a script block, up to the condition. The script block in a Do loop always runs at least once.
  • In a Do-While loop, the condition is evaluated after the script block has run. Then the while loop repeat the script until as long as the condition became false.
  • In a Do-Until loop always runs at least once before the condition is evaluated. However, the script block runs as long as the condition is false.

Do-While

Syntax : do {<statement list>} while (<condition>)

Example:

In below example, the loop will stop executing if condition became false that is if loop_index value reach 0 then condition became false.

$myArray = 1,2,3,0,4
do
{
$loop_index++;
}
while ($myArray[$loop_index] -ne 0)
$loop_index

Output:

3

Do-Until

Syntax : do {<statement list>} until (<condition>)

Example:

In below example, Do…Until loop is used when we want to repeat a set of statements as long as the condition is false.

$myArray = 1,2,3,0,4
do
{
$loop_index++;
}
until($myArray[$loop_index] -ne 0)
$loop_index

Output:

4

 

Power Shell – Type comparison

What is comparison Operator?

Comparison operators let you specify conditions for comparing values and finding values that match specified patterns. To use a comparison operator, specify the values that you want to compare together with an operator that separates these values.

Type comparison

The type comparison operators (-is and -isnot) are used to determine if an object is a specific type.

Operators Description
-isReturns true if both object are the same type
-isnotReturns true if the objects are not the same type

-is Operator

Syntax: <object> -is <type reference>

Example:

PS C:\>
$stringMsg1 = "dotnet-helpers"
$stringMsg2 = 100
$stringMsg1 -is $stringMsg2.GetType()
$stringMsg2 -is [int]

OUTPUT

False
True

-isnot Operator

Syntax: <object> -is <type reference>

Example:

PS C:\>
$stringMsg1 = "dotnet-helpers"
$stringMsg2 = 100
$stringMsg1 -isnot $stringMsg2.GetType()
$stringMsg2 -isnot [int]

OUTPUT

True
False

PowerShell – Replacement Operator

Replacement Operator

PowerShell provides several options to help you replace text in a string with other text. The -replace operator replaces all or part of a value with the specified value using regular expressions. It replaces every occurrence of the exact string you specify with the exact replacement string that you provide. The -replaceoperator provides much more flexibility because its arguments are regular expressions that can match and replace complex patterns.

Operators Description
-replacereplace a string pattern


Syntax:

<input> <operator> <original> <substitute>

The syntax of the -replace operator is as follows, where the <original> placeholder represents the characters to be replaced, and the <substitute> placeholder represents the characters that going to replace.

Example:

PS C:\> 
$stringMsg = "Hi Welcome to dotnet-helpers.com"
$stringMsg -replace 'Hi' , "You are"

OUTPUT

You are Welcome to dotnet-helpers.com

By default, the -replace operator is case-insensitive. To make it case sensitive, you need to introduce ‘c’ as prefix like  -creplace. To make it explicitly case-insensitive, you need to prefix ‘it’ like -ireplace.

Example:

Here the output will be same text beacuse we maked a replace operator as case sensitive, so ‘HI’ does’t macth with the string.

PS C:\>
$stringMsg = "Hi Welcome to dotnet-helpers.com"
$stringMsg -creplace 'HI' , "You are"

OUTPUT

Hi Welcome to dotnet-helpers.com

PowerShell – Matching Operators

-like and -notlike Operators

The like operators find elements that match or do not match a specified pattern using wildcard expressions.

How its handle single & Collection values?

If we input scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values. If there are no matches found in a collection then the comparison operators do not return anything.

The syntax is:

<string[]> -like <wildcard-expression>
<string[]> -notlike <wildcard-expression>

Example:1

PS C:\>
$singleValue = "Aadharsh"
$singleValue = $singleValue -like "Aadharsh"
Write-Host ""
Write-Host "Is varible contain 'Aadharsh' : " $singleValue

OUTPUT:

Is varible contain 'Aadharsh' : True

Example:2

$singleValue = "Aadharsh" , "Rakshu"
$singleValue = $singleValue -like "Aadharsh"
Write-Host ""
Write-Host "The matched value is : " $singleValue

OUTPUT:

The matched value is : Aadharsh

-match and -notmatch Operators

The match operators find elements that match or do not match a specified pattern using regular expressions. It search only in strings and they can’t search in arrays of integers or other objects.

The syntax:

<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>

Example:1

PS C:\>
$singleValue = "Aadharsh"
$singleValue = $singleValue -match "Aadharsh"
Write-Host ""
Write-Host "Is variable contain 'Aadharsh' : " $singleValue

OUTPUT:

Is variable contain 'Aadharsh' : True

Example:2

$singleValue = "Aadharsh" , "Rakshu"
$singleValue = $singleValue -like "Aadharsh"
Write-Host ""
Write-Host "The matched value is : " $singleValue

OUTPUT:

The matched value is : Aadharsh

Note:

There is automatic variable called “matches” element which return the match elements. You can use $matches variable to get the matched value.

What is difference between -Like & -Match?

The most technical distinction is -Match is a regular expression, whereas -Like is just a wildcard comparison, a subset of -Match. If you need a wildcard to find this item’, then start with -Like. However, if you are sure that most of the letters in the value that you are looking for, then you are better you can go with -Match. -match operator is quite faster than -like operator.

PowerShell – Equal Operators

Equal Operators:

The equal operators return a value of TRUE if its identical or return a value of FALSE if it is not identical. The entire pattern must match an entire value. By default, all comparison operators are case-insensitive. To make a comparison operator case-sensitive, precede the operator name with a “c”. For example, the case-sensitive version of -eq is -ceq. To make the case-insensitivity explicit, precede the operator with an “i”

Operators Description
-eqequals
-nenot equals
-gtgreater than
-gegreater than or equal
-ltless than
-leless than or equal

 

Example:

$userName = 'Rakshu'
$userName -eq 'rakshu'
$userName = 'Aadharsh'
$userName -ne 'rakshu'
$number1 = 100
$number1 -gt 50
$userName = 100
$userName -le 50

OUTPUT: