To load additional modules and snap-ins use the commands:
Import-Module
and Add-PSSnapIn
.If you try to load the same snap-in more in the same session an error occurs. One way around is to ignore the error.
Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
Great care must be taken when using variables. A simple typo can lead to a lot of confusion. It is also worthwhile to read up on variable scope.
help about_scope
PowerShell allows you to create methods or functions within a script. The basic syntax is
function { script block }
A quick example of creating a function to add two numbers together and calling the function.
function Add ([int] $x, [int] $y) {
$results = $x + $y;
Write-Host "Results $results";
}
Add 3 4
In the above example, I am doing a few small things that are more personal preference than anything else. There are multiple ways to create parameters. I like to create parameters on the same line as the function name. I also like to add the datatypes. This just gives a me a bit of clue as to what the function would like to have for data. I am also using semi-colons at the end of each line. This comes from my history of working with C, C++, Java, JavaScript, and C#.
You will notice in calling the function, "Add 3 4", parameter are separated by spaces.
Single line comments start with the # sign.
#A simple one line comment
Block comments are encased in <# #>
<#
This is a multiple line comment
or block comment
#>
Accessing the help system within PowerShell is straight-forward. There are multiple key words that can be used.
Get-Help
, Help
, or Man
Calling the help system on the cmdlet Get-Content.
man Get-Content
The help system also comes with a set of switches for showing the full listing.
man Get-Content -full
No comments:
Post a Comment