Entering a Remote Session
You can start a remote session against the target computer with the Enter-PSSession cmdlet:
PS > Enter-PSSession -ComputerName SPServer01.powershell.nu `
>> -Authentication CredSSP -Credential powershell\administrator
This starts an interactive session with a remote
computer using CredSSP authentication. During the session, all commands
that you type run on the remote host. You can stop the remote session
with the Exit-PSSession cmdlet:
To run commands against multiple remote computers, combine the New-PSSession and Invoke-Command cmdlets. The New-PSSession cmdlet creates persistent connections to remote computers. The Invoke-Command cmdlet runs a script block on the computers specified in the New-PSSession cmdlet. Here’s an example:
PS > $Session = New-PSSession '
>> -ComputerName SPServer01.powershell.nu, Server1.powershell.nu `
>> -Authentication CredSSP -Credential powershell\administrator
PS > Invoke-Command -Session $Session -ScriptBlock {
>> "Running Remote commands on: $($Env:COMPUTERNAME)"
>> $regKey = "hklm:software\microsoft\shared tools\web server extensions\14.0"
>> if(Test-Path -Path $regKey) {
>> if(get-itemProperty -Path $regKey |
>> Where-Object { $_.SharePoint -eq "Installed" }) {
>> "$($Env:COMPUTERNAME) is running SharePoint 2010"
>> } else {
>> "$($Env:COMPUTERNAME) is not running SharePoint 2010"
>> }
>> } else {
>> "$($Env:COMPUTERNAME) is not running SharePoint 2010"
>> }
>> }
>>
Running Remote commands on: SERVER1
SERVER1 is not running SharePoint 2010
Running Remote commands on: SPSERVER01
SPSERVER01 is running SharePoint 2010
In the script block in this example, we check if the remote computer has the registry key HKLM\Software\Microsoft\Shared Tools\Web Server Extensions\14.0 using the Test-Path cmdlet. If the key exists, we check if the SharePoint string value is equal to Installed.
Depending on the outcome of condition evaluation, we then output an
informational message stating whether SharePoint is installed on that
particular server.
Note
Checking the registry for keys and values is a quick and simple way to verify that SharePoint 2010 is installed on the server.
Running SharePoint 2010 Cmdlets Remotely
Windows PowerShell includes the Add-PSSnapin
cmdlet, which is used to add registered snap-ins to the current
session. After a snap-in is added, you can use cmdlets and providers
that the snap-in supports. When SharePoint 2010 is installed, it also
installs a Windows PowerShell snap-in for SharePoint. You can use the snap-in to run cmdlets included in SharePoint 2010 remotely.
PS > Enter-PSSession -ComputerName SPServer01.powershell.nu `
>> -Authentication CredSSP -Credential powershell\administrator
[SPServer01.powershell.nu]: PS > Get-PSSnapin -Registered
Name : Microsoft.SharePoint.PowerShell
PSVersion : 1.0
Description : Register all administration Cmdlets for Microsoft SharePoint Server
[SPServer01.powershell.nu]: PS > Add-PSSnapin Microsoft.SharePoint.PowerShell
In this example, we use the Enter-PSSession cmdlet to start an interactive session on the server SPServer01. We then use the Get-PSSnapin cmdlet to retrieve all registered snap-ins. Since the server has SharePoint 2010 installed, Microsoft.SharePoint.PowerShell is returned. We can add the snap-in with the Add-PSSnapin
cmdlet and invoke all the cmdlets available in SharePoint 2010
remotely. In a standard remote Session each pipeline, function, or
script is run on its own thread. To start a remote session that runs
pipelines, functions, and scripts on the same thread, you have to
create a custom session configuration on every server in the farm that
you want to manage remotely. This example demonstrates how to create a
custom session configuration.
PS > Register-PSSessionConfiguration -Name SharePoint -ThreadOptions ReuseThread
When starting a remote session against a server in
the farm that contains a custom session configuration, you can simply
use the ConfigurationName parameter as demonstrated in this example:
PS > Enter-PSSession -ComputerName SPServer01.powershell.nu `>> -Authentication
CredSSP -Credential powershell\administrator `>> -ConfigurationName SharePoint