Unable to load SharePoint snap-in in PowerShell

0

I'm currently doing a script in powershell to add some items to a SharePoint list and add some columns to the same list, but by doing the following:

param([Parameter(Mandatory=$true)][string] $urlSite, [Parameter(Mandatory=$true)][string] $urlList)

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) 
{ 
   Add-PSSnapin Microsoft.SharePoint.PowerShell 
} 

Write-Host "Url del Sitio" $urlSite -foregroundcolor yellow

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
   #Aquí algo lo que se requiere hacer....
}
)

I get the following Error:

  

Get-PSSnapin: No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found. Check   the pattern and then try the command again.

It's like you can not load the assembly Microsoft.SharePoint.PowerShell

Am I doing something wrong?

    
asked by Andres Felipe Williams Suarez 05.01.2016 в 22:12
source

2 answers

0

This is the code solved:

param([Parameter(Mandatory=$true)][string] $urlSite 
[Parameter(Mandatory=$true)][string] $urlList)

Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

Write-Host "Url del Sitio" $urlSite -foregroundcolor yellow

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
   #Aquí algo lo que se requiere hacer....
}
)

It used to modify the statement

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) 
{ 
   Add-PSSnapin Microsoft.SharePoint.PowerShell 
} 

By:

Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

This prevents me from launching an error when I am adding the SharePoint assemblies, it seems when they are already loaded it throws an error as if I had not loaded them.

    
answered by 27.01.2016 / 15:19
source
1

Well you have to follow several steps, the first thing is to make sure that the script execution policy is correct, and preferably execute this in a console with elevated privileges:

Set-ExecutionPolicy Unrestricted

In your case, there may be some other condition that generates errors, then the best is to load the snap of Sharepoint to the raw

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

And if everything goes well

Get-SPManagedAccount

Of course all this must be run on the Sharepoint server which is where the dll Microsoft.SharePoint.PowerShell is installed in the GAC.

If you're doing it locally, you should make sure you have that dll in the GAC.

    
answered by 05.01.2016 в 22:30