kwizcom banner advertisement

How to Remotely Execute SharePoint 2010 PowerShell Cmdlets?

PowerShell allows us to run cmdlets remotely from client machines. We can run SharePoint 2010 cmdlets on the SharePoint Server, remotely connecting from client machines by following these two steps:

Step 1: To execute SharePoint PowerShell remotely, you have to enable PowerShell Remoting on the Server first! By default PowerShell Remoting is disabled. Log-On to the server you want to access remotely, enable remote PowerShell for running SharePoint 2010 cmdlets

Enable-PSRemoting
execute sharepoint 2010 powershell remotely
This command enables remote PowerShell for SharePoint 2010 by enabling WinRM service and configures windows firewall to allow incoming sessions. 
Its a one time activity to configure remote PowerShell in SharePoint 2010.

Step 2:
Now, We can either directly invoke PowerShell Cmdlets or establish a session and then execute PowerShell cmdlets. E.g. To get the total no. of site collections, I'm using the below code:

 Option 1: Invoke PowerShell remotely to run SharePoint 2010 cmdlets:
 To Invoke SharePoint 2010 PowerShell on remote server:
 Invoke-Command -ComputerName SPSWFE01 -ScriptBlock { 
 Add-PSSnapin Microsoft.SharePoint.PowerShell; 
 $webApp = Get-SPWebApplication "http://sharepoint.crescent.com"; 
 write-host "Total No. of sites in the Web Application: $($webApp.Sites.Count)" 
 } -credential (Get-Credential)

Option 2: Establish a Remote Session and then execute PowerShell cmdlets:
We've to connect to remote SharePoint server using PowerShell. In the Client machine, enter:
Enter-PSSession "SharePoint Server Name" -Credential (Get-Credential) 

This cmdlet prompts for credentials and opens PowerShell remote session from the remote SharePoint farm .Once done, we can run PowerShell commands. Here is an example to establish and execute PowerShell commands
Add-PSSnapin Microsoft.SharePoint.PowerShell
$webApp = Get-SPWebApplication "http://sharepoint.crescent.com"
write-host "Total No. of sites in the Web Application: $($webApp.Sites.Count)"

Don't forget to exit the PowerShell session, once you are done with PowerShell:
Exit-PSSession
How to Remotely Execute SharePoint 2010 PowerShell Cmdlets? How to Remotely Execute SharePoint 2010 PowerShell Cmdlets? Reviewed by Salaudeen Rajack on June 09, 2013 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.