WEBSITE

Sharepoint 2013 : PowerShell Basics (part 2)

10/10/2013 4:24:35 AM

What Are Console Commands?

A handful of what I like to call console commands stem from the good ol’ DOS days. They have been available in command prompts since then, with the current PowerShell consoles included. These commands provide standard directory navigation, file handling, and screen handling.

Several common console commands follow:

cd: Changes the directory

cls: Clears the console screen

dir: Displays a listing of the current directory (folder)

type: Used with a text-based filename and displays the contents of a text file


Tip

These commands have been grouped within PowerShell as Aliases. To see all of the available aliases use Get-Alias.

Path Environment Variable


Scenario/Problem: You have scripts within a local folder which you want PowerShell to recognize such that you do not need to use the full path name.


Solution: Add the local directory path to the PSModulePath variable.

You add the local folder’s path to the PSModule environment variable by entering the following command in the PowerShell command prompt (substituting <folder path> with the local directory path), as shown in Listing 2.2.

Listing 2.2. Adding a Folder Path to the PSModulePath


$env:PSModulePath = $env:PSModulePath + ";c:\<folder path>"



Tip

Add the same exact command line into the PowerShell profile configuration file to always have your local folder added to the PSModulePath. 

Running Unsigned Scripts


Scenario/Problem: You need to be able to run unsigned scripts within PowerShell.


Solution: Use the Set-ExecutionPolicy command.

To run unsigned scripts, you must change the execution policy:

Set-ExecutionPolicy remotesigned

You are prompted with a confirmation. Enter Y and press Enter (or just press Enter—Y is the default).

In some cases, you must set the policy to Unrestricted:

Set-ExecutionPolicy unrestricted

Disabling the Confirmation Prompt


Scenario/Problem: You need to automatically confirm operations such that a script runs unattended.


Solution: Include the -Confirm parameter with a $false setting.

When running cmdlets , you might receive a confirmation message to confirm the action being processed. If you use these cmdlets in scripts, the script will not run straight through without prompting for the confirmation.

The cmdlets that have a -Confirm optional parameter can be called in unattended mode by passing in -Confirm:$false to the cmdlet, as shown in Listing 3.

Listing 3. Suppressing the Confirmation Prompt Example


Remove-item *.png -Confirm:$false



Note

Not all cmdlets have a –Confirm parameter. Use the help (-?) parameter to verify each cmdlet in question.

Generating Inline Credentials


Scenario/Problem: You need to provide cmdlets with credentials without prompting for them.


Solution: Create a new PSCredential object.

Various cmdlets require SharePoint and/or SQL Server account credentials for proper authentication when performing the desired operations. The examples include using (Get-Credential) as the parameter value, which prompts the user for credentials.

When running these cmdlets in scripts, the prompting for credentials pauses the execution. If you need the script to run straight through, instead of using Get-Credential, you can generate a new PSCredential object inline, as shown in Listing 4.

Listing 4. Generating Inline Credentials Example


(New-Object System.Management.Automation.PSCredential "domain\user",
(ConvertTo-SecureString "password" -AsPlainText -Force))


Therefore, simply substitute (Get-Credential) for the text shown in Listing 2.4 with the proper username and password when using a cmdlet with authentication requirements.

Referencing an Assembly


Scenario/Problem: You want to be able to call methods and/or use objects within a trusted assembly (DLL).


Solution: Use a reflection declaration, and then use a variable to instantiate an object.

You can reference an assembly in your PowerShell script and then use any objects available by assigning them to a variable. Then you can use any methods or properties within your script. No intellisense exists, so you need to know the object model beforehand. Listing 5 demonstrates an example assembly reference and usage.

Listing 5. Referencing and Using a SharePoint Assembly


[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.
SharePoint")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.
SharePoint.Administration")

$spFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$spFarmSolutions = $spFarm.Solutions



Tip

Instead of coding and compiling a console application, use a PowerShell script that references the SharePoint assemblies.


Other  
  •  SharePoint 2013 Management Shell : What Is the SharePoint 2013 Management Shell Loading?
  •  Watch Web TV Anywhere (Part 3)
  •  Watch Web TV Anywhere (Part 2)
  •  Watch Web TV Anywhere (Part 1)
  •  YouView brings The Most Extraordinary Way To Watch TV (Part 2)
  •  YouView brings The Most Extraordinary Way To Watch TV (Part 1)
  •  Installing and Configuring SharePoint 2013 : Creating Web Applications and More
  •  Installing and Configuring SharePoint 2013 : Creating the Farm (part 7) - Configuring the App Management Service - Configuring the User Profile Service
  •  Installing and Configuring SharePoint 2013 : Creating the Farm (part 6) - Configuring the App Management Service - Configuring PerformancePoint Services , Configuring the Search Service Application
  •  Installing and Configuring SharePoint 2013 : Creating the Farm (part 5) - Configuring the App Management Service - Configuring the Managed Metadata Service
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone
    Visit movie_stars's profile on Pinterest.