My blog has moved!

My blog has moved to http://www.lamber.info. Click on the redirect button below to be redirected to the blog post.

Please update your bookmarks and use the new url for getting new updates.

Pages

Wednesday, February 17, 2010

Visual Studio 2010 and .NET Framework 4 Release Candidate available

Hi to everyone,

the release candidate of Visual Studio 2010 and .NET Framework 4 are available for the public. Check it here. The RTM release date remains fixed for the 12 April, 2010.

Best regards,

Patrick

Saturday, February 13, 2010

Free clinics for SharePoint Server 2010

Microsoft provides two self-practiced clinics about SharePoint Server 2010 for free. One of these targets the IT Professionals and the second the developers. You find the links to the clinics and a short description taken directly from the Microsoft site below:

This two-hour clinic describes the various benefits that Microsoft SharePoint 2010 offers IT Pros. It describes improvements to the user interface, including the Ribbon and enhanced Central Administration console. It also describes features that help you monitor your SharePoint site, such as large list resource throttling, Unattached Content Database Recovery, and the SharePoint Best Practices Analyzer.

This two-hour clinic describes various new features and enhancements that Microsoft SharePoint 2010 provides developers. It describes how you can create and deploy SharePoint 2010 solutions using Microsoft Visual Studio 2010. It also describes how you can develop remote clients for SharePoint 2010 and develop SharePoint 2010 solutions that incorporate data from external line-of-business applications.

Enjoy

Patrick

Originally from Jerry’s Blogs

Tuesday, February 9, 2010

VS 2010 Release candidate available

Yesterday, there was an announcement of the availability of Visual Studio 2010 RC for the MSDN subscribers. Next week on Wednesday it will be released for the public. Read here to get more information.

Saturday, February 6, 2010

PowerShell 2.0: An introduction (overview)

I want to link the PowerShell basics blog entries that I wrote in the last weeks. I hope in this way you are able to find faster the information you need.

 

Hope this helps,

Patrick

PowerShell 2.0: An introduction (Part 6) - The different loops in PowerShell

Finally, I found time to continue the post series about PowerShell 2.0: An introduction. In this post we are going to consider another fundamental aspect of any programming language: loops.

Walkthrough

In PowerShell, you can use many loop variations for different situations. Today, w will handle the different loops and make some easy examples.

  • for
  • while
  • do while
  • do until
  • foreach

FOR loop

Let us start with the easiest of the loops: the for loop. The basic syntax of the loop is following:

for (variableInitialization; condition; operation) {
# execute your stuff here
}



The next example creates an array of names and loops over it.

$people = “Patrick”, “Martin”, “Stefan”, “Richard”

for ($i = 0; $i –lt $people.length; $i++) {
$people["$i]
}




Line 1 creates the array with 4 slots filled with names. The loop is executed as follows:



  1. $i = 0 is executed the first time. The variable $i is declared and initialized with 0
  2. $i –le 3: this is the condition that is checked. If the condition is true, the loop is executed. In this case $i = 0.
  3. The loop executes
  4. The statement $i++ is executed after the loop cycle. Then the condition is checked again. If the condition remains true, go to step 3. If the condition becomes false, the loop is terminated


Note: the operation part of the for loop does not have to be only $i++. You can use depending on your needs also different operations such as $i = $i + 2 (e.g., to jump always two slots of the array)


If for some case you need to change the loop logic during the execution, you can use either the statements break or continue. The first statement stops the loop execution and continues with the code after the loop. The second statement stops the loop cycle (not the loop itself) and goes back immediately to point 4.


The next sample shows how you can use the statements break and continue.

$people = "Patrick", "Martin", "Stefan", "Richard" 

# normal execution
for ($i = 0; $i -lt $people.length; $i++) {
$people[$i]
}

# Execution with a break statement – Only first two"
for ($i = 0; $i -lt $people.length; $i++) {
if ($people[$i] -eq "Stefan) {
break;
}

$people[$i]
}

# Execution with a continue statement - Stefan is skipped
for ($i = 0; $i -lt $people.length; $i++) {
if ($people[$i] -eq "Stefan) {
continue;
}
$people[$i]
}

The for loop first of all initializes a variable at the beginning. You can do it either in the brackets or outside of the loop.


The results look like in the next picture. The first loop goes over all items of the array. The second loop breaks the complete loop when the value “Stefan” in the array is reached. The third loop finds the value “Stefan” and skips the cycle execution. The loop itself is not stopped yet and continues its execution.


image





Note: you can use the break and continue statements in every loop that I am describing in this post.


While loop and DO While


Let us jump over to the next two loops in our list. These work in a similar way. However, there is a small but important difference. Let us dig deeper into this by analyzing the basic syntax of both loops:

while(condition) {
# execute your stuff here
}

do {
# execute your stuff here
} while (condition)



Both loops are executed until the condition in the brackets stays true. When it turns to false, the loop is skipped. At first glance, both loops seem to do the same. Nevertheless there is an important difference: the condition of the while loop is executed first. On the other hand, the condition of the do while loop is checked after. At the end that means that the do while loop is executed at least once. To understand it better look at the next sample code. It shows both loops with the same condition. The condition is 1 –lt 1. A condition that is false from the beginning:

while (1 –lt 1) {
“This code is not executed”
}
do {
“The do while is executed at least once.”
} while (1 –lt 1)


Line 1 checks the condition. The condition is false and the code jumps over to line 3 skipping the loop. The text in the loop is never executed. Line 5, 6 are executed and the code jumps over to line 7. The condition is checked now. Since the condition is false, the loop is stopped right now and the code continues. The content of the do while loop was executed at least once.


Do until loop


The do until loop works and looks like the while loop. The only difference lies on the condition logic that terminates the loop. The do until loop terminates the loop when the condition in brackets becomes true. The syntax looks as follows:


do {
#execute your stuff here
} while (condition)


The next short example shows this behavior.


$i = 0

do {
$i
$i++
} until ($i –gt 10)


Line 1 initializes the variable $i to 0. On line 3 the content of the variable is outputted to the console. It follows line 4 that increments the $i with 1. Line 5 is checked and executes the loop as long as $i is not greater than 10.


Foreach loop


Last but not least, we are talking about the foreach loop. This loop is marvelous when you want to loop over a collection of objects of any kind. The loop is executed until all the objects in the collection where executed at least once. Naturally, there is an exception when you are using the break statement. Let us look to a simple example of a foreach looping over an array.


$people = “Hannes”, “Patrick”, “Stefan”

foreach ($person in $people) {
$person
}


The foreach loops over each single slot of the variable and outputs them into the console.


Summary



We checked different loop types in PowerShell and saw some simple examples. I think that we can finish with the introduction of PowerShell basics and start over to a field that I like most: exploring the object model of SharePoint by using PowerShell and C#.


 


Hope this helps,


Patrick

Wednesday, February 3, 2010

PowerShell 2.0: An introduction (Part 5) - Switch statement

Last time we saw how to create the first PowerShell script and how to use the If-Statement. In this post of the post series PowerShell 2.0: An introduction, we are going to see how the switch statement works in PowerShell.

The basic syntax of the switch statement looks as follows:

switch(input) {
Value1 { # case for value 1 }
Value2 { # case for value 2 }
default { # default case }
}

Let’s start with a switch statement in action with our next example:

$variableToCheck = 100

switch ($variableToCheck) {
50 { "Value is 50" }
100 { "Value is 100" }
default { "Value is something else" }
}


Our local variable is initialized with the value 100. The switch statement checks the content of the variables and compares it with the values specified afterwards. In this case the statement identifies the correct value at line 5 and executes the statement in brackets. If the switch statement does not find the correct value, the output of the default statement is executed.


Until now, the switch statement behavior is similar to other languages. A cool feature of the switch statement in PowerShell is the ability to use wildcard or regular expressions to compare the values. You see the next code sample showing the usage of both statements. Please note that you have to add the –wildcard and -regex statements after the switch statement to be able to use them.

$content = "This is a longer sentence to check"

switch -wildcard ($content) {
"*is*" { "you can find 'is' in the sentence" }
"*check" { "'check' is the last word in the sentence" }
"is" { "without wildcard you are not able to make a match in this case" }
default { "We did not find a match" }
}

switch -regex ($content) {
"[a-1]" { "in the content there is one match belonging to the range a-1" }
"[z]" { "in the content there is a z" }
default { "no match" }
}


Line 3 of the above example shows you how to use wildcards in the switch statement. in this case line 4 and 5 are executed, because both wildcard searches match the string provided. please note to provide like in line 3 the –wildcard statement before making such checks. similar, line 10 shows you how to use regular expressions. you can use the regular expression syntax that you usually use in the .NET framework.


Summary


This post described shortly the switch statement in PowerShell and how powerful this statement is. The switch statement in PowerShell does not only use the usual simple way of checking values, but it provides to you a powerful set of string and pattern matching capabilities for your scripts.


 


Hope this helps,


Patrick

Tuesday, February 2, 2010

PowerShell 2.0: An introduction (Part 4) - Creating your first script; If statement

In the last posts of this post series, we used the PowerShell console and executed some useful queries. Most of the cases this is enough to execute some fast commands. However, the idea of PowerShell is to automate our system and solve in this way daily tasks. In this and next posts we are going to learn some PowerShell syntax basics. You will note many similarities if you are already familiar with C#. Our example today is a demonstration how to comment and use if statements in our code.

First of all, open the PowerShell console as an administrator and execute the command Set-Executionpolicy remotesigned and confirm your choice. You should see something like this:

image

With this command you enable the execution of scripts on your machine.  You need to do it only once. You don’t need to execute it again on this machine for scripting.

Before we begin we must know that there are many IDEs that we can use for creating our PowerShell scripts. We are going to use the ISE (integrated scripting environment) environment directly integrated in our PowerShell console.

Note: ISE is not the only PowerShell editor out there. If you are interested in trying something different, you can check the following products:

Now, you can close again the console and start a new session. Start   As and execute the command ise. A window opens after a while.

image

This console is a good starting point to write your scripts. In the upper part you can write down your script. In the middle part you can see directly the results of your PowerShell console. Start your scripts by using the image button.

Let’s go with our first code presenting simple code comments and an if statement. The basic syntax for an if statement is:

if (condition) {
# execute code here
}

The next code sample shows you how to use comments and some simple if statements.

# you can create line comments with the "#" sign

# you can define an output directly in this way
"Simple output demonstration"

# otherwise you can use the PowerShell command write-output
write-output "IF-Statements"

# define a variable called valueToCheck and initialize it with an integer
$valueToCheck = 4

# this if statement checks if the content of $valueToCheck is greaer than 5
if ($valueToCheck -gt 5) {
"The value is greater than 5"
}

# this if statement checks if the content is less than 5
if ($valueToCheck -lt 5) {
"The value is smaller than 5"
}

# this if statement checks if the content equals 5
if ($valueToCheck -eq 5) {
"The value is equal 5"
}


You can improve the code above by extending the code by changing it with else if and else statements.

# you can create line comments with the "#" sign

# you can define an output directly in this way
"Simple output demonstration"

# otherwise you can use the PowerShell command write-output
write-output "IF-Statements"

# define a variable called valueToCheck and initialize it with an integer
$valueToCheck = 4

# this if statement checks if the content of $valueToCheck is greaer than 5
if ($valueToCheck -gt 5) {
"The value is greater than 5"
# this if statement checks if the content is less than 5
} elseif ($valueToCheck -lt 5) {
"The value is smaller than 5"
# otherwise, it has to be equal
} else {
"The value is equal 5"
}

Let us finish this post by showing you some interesting conditional statements that you might use in your code.



  • eq: compares two values to be equal

  • gt: checks if the first value is greater than the second one

  • lt: checks if the first value is smaller than the second one

  • match: verifies if the string specified on the right is anywhere in the left side. You can also use regular expressions.

  • notmatch: the opposite of match

  • like: can be used similar as in SQL with wildcards.

  • notlike: opposite of like

  • contains: is an equals for an array. Checks if a value is in a collection or not


The next example shows some examples of the above conditional statements in action.

# store some variables and assign values
$name = "Rodriguez"

# match example
# this statement is executed
if ($name -match "od") {
"od is contained somewhere in Rodriquez"
}

# match example
# this statement is not executed
if ($name -match "odi") {
"odi is not contained in Rodriguez"
}

# like example
# this statement is not executed
if ($name -like "od") {
"od is contained in Rodriguez. However, there are missing wildcards to enable a correct match"
}

# this statement is executed
if ($name -like "*od*") {
"*od* is contained in Rodriguez. By using the wildcards you get a correct match in the like statement."
}

# contains example
# first of all define an array
$nameList = "Patrick", "Stefan", "Martina"

# this statement is executed
if ($nameList - contains "Patrick") {
"Patrick is in the array"
}

# this statement is not executed
if ($nameList -contains "patr") {
"You need a perfect match to find the item"
}# store some variables and assign values
$name = "Rodriguez"

# match example
# this statement is executed
if ($name -match "od") {
"od is contained somewhere in Rodriquez"
}

# match example
# this statement is not executed
if ($name -match "odi") {
"odi is not contained in Rodriguez"
}

# like example
# this statement is not executed
if ($name -like "od") {
"od is contained in Rodriguez. However, there are missing wildcards to enable a correct match"
}

# this statement is executed
if ($name -like "*od*") {
"*od* is contained in Rodriguez. By using the wildcards you get a correct match in the like statement."
}

# contains example
# first of all define an array
$nameList = "Patrick", "Stefan", "Martina"

# this statement is executed
if ($nameList - contains "Patrick") {
"Patrick is in the array"
}

# this statement is not executed
if ($nameList -contains "patr") {
"You need a perfect match to find the item"
}

Summary


In this post we created our first PowerShell script and saw how the If-Statements are working in PowerShell.


 


Hope this helps,


Patrick