programming4us
programming4us
WEBSITE

PowerShell for Microsoft SharePoint 2010 : Flow Control and Object Disposal - Conditional Statements

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Windows PowerShell supports the conditional statement if/elseif/else, which branches execution based on a condition, and the switch statement, which can handle multiple complex conditions.

The if/elseif/else Statement

The if/elseif/else statement allows you to execute a block of code if a specified condition is met. However, the statement can also execute a block of code if a condition is not met. This statement uses comparison operators to test the condition.

The following is a simple if/elseif/else statement.

PS > $url = "http://SPServer01"
PS > if((Get-SPSiteAdministration $url).DiskUsed -gt 20MB) {
>> "Disk space used is more than 20 MB"
>> } else {
>> "Disk space used is less than 20 MB"
>> }
>>
Disk space used is less than 20 MB

In this example, we use the Get-SPSiteAdministration cmdlet to retrieve the size (the amount of disk space used) of a site collection. We then use an if/else statement to check if this size exceeds 20MB. If so, we return “Disk space used is more than 20MB.” If the condition is not met, we return “Disk space used is less than 20MB.”

The next example demonstrates the use of the elseif keyword within the if/elseif/else statement.

PS > $url = "http://SPServer01"
PS > if((Get-SPSiteAdministration $url).DiskUsed -gt 20MB) {
>> "Disk space used is more than 20 MB"
>> } elseif((Get-SPSiteAdministration $url).DiskUsed -gt 10MB) {
>> "Disk space used is more than 10 MB"
>> } else {

>> "Disk space used is less than 10 MB"
>> }
>>
Disk space used is more than 10 MB

The elseif keyword lets you introduce another condition and a corresponding additional execution branch. You can add any number of elseif keywords to an if/elseif/else statement.

The switch Statement

The switch statement is a series of if statements and is used to evaluate a condition against a number of potential matches. The switch statement matches the expression with each of the conditions, and if a match is found, an action associated with the condition is performed. If more than one condition applies, the switch statement will execute each of the applicable conditions.

The following is a simple switch statement.

PS > $a = 1
PS > switch($a) {
>> 1 { "contains one" }
>> 2 { "contains two" }
>> }
>>
contains one

In this example, we choose an action based on the value in parentheses after the switch keyword. The value is matched with each of the conditions. If a match is found, the action associated with that condition is performed.

The default comparison operator used by the switch statement is the -eq operator. It is possible to use other operators when using the switch statement, as the next example demonstrates.

PS > $url = "http://SPServer01"
PS > switch((Get-SPSiteAdministration $url).DiskUsed) {
>> {$_ -gt 20MB} { "Disk space used is more than 20 MB"; Break }
>> {$_ -gt 10MB} { "Disk space used is more than 10 MB"; Break }
>> {$_ -gt 5MB} { "Disk space used is more than 5 MB"; Break }
>> Default { "Disk space used is less than 5 MB" }
>> }
>>
Disk space used is more than 10 MB

In this example, the values in parentheses are the amount of disk space used. The value is then matched against the patterns in each clause, and if a match is found, the corresponding clause is executed. Since a Break is used at the end of each condition, the switch stops as soon as a match is made. The Default clause is used to perform an action if none of the switch values match the pattern. Notice how we use the $_ variable to reference the input object.

The switch statement supports a couple of options that you can use to control the pattern matching. By default, the switch statement is case-insensitive. You can perform a case-sensitive pattern match with the -Casesensitive option.

PS > $url = "http://SPServer01"
PS > switch -CaseSensitive ($url) {
>> "http://SPServer01" {"matches http://SPServer01"; Break }
>> "http://SPSERVER01" {"matches http://SPSERVER01"; Break }
>> default {"no match found"}
>> }
>>
matches http://SPServer01

We can also use wildcard pattern matching with the switch statement. Here is an example:

PS > $url = "http://SPServer01"
PS > switch -WildCard -CaseSensitive ($url) {
>> "http*[S]*" {"Starts with 'http' and contains a upper-case S"; Break }
>> "http*[s]*" {"Starts with 'http' and contains a lower-case s"; Break }
>> }
>>
Starts with 'http' and contains a upper-case S

In this example, first we check if the value of the variable $url starts with a 'http' and contains an uppercase S. Then we check if the value of the variable $url starts with 'http' and contains a lowercase s. Since the first pattern matches the variable, the corresponding clause is executed.

The switch statement also supports regular expressions, which let you create complex pattern matches. Here is an example:

PS > $url = "http://SPServer01"

PS > switch -regex ($url) {

>> "^(http|https):/{2}" {"match found"}

>> }

>>

match found

In this example, we test if the value of the variable $url starts with 'http' or 'https' followed by the : character and two / characters.

Other  
  •  PowerShell for Microsoft SharePoint 2010 : Variables, Arrays, and Hashtables - Hashtables in Windows PowerShell
  •  PowerShell for Microsoft SharePoint 2010 : Variables, Arrays, and Hashtables - Arrays in Windows PowerShell
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 12) - Digital Asset Management
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 11) - eDiscovery and Hold , Retention
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 10) - In-Place Records Management
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 9) - Improved Records Center Site
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 8) - Content Type Syndication
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 7) - Term Store and Term Sets
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 6) - Managed Metadata Fields
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 5) - Content Types and Columns
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us