adding Sharepoint snapin into a custom dscmodule

Hi

Using PowerShell 5 on server 2012R2 and a custom (class 5) dscmodule for doing SharePoint configurations.
How can I add a SharePoint snapin, if I need to set a spesific SharePoint related configuration using my set() method in a DSCresource?
I have tried with adding the snapin using:

if ((Get-PSSnapin -Registered -Name 'microsoft.sharepoint.powershell') -ne $null) 
        {
            try
            {
                Add-PSSnapin 'microsoft.sharepoint.powershell'
                Write-Debug 'Added microsoft.sharepoint.powershell snapin'
            } 
            catch 
                {
                    throw 'There was a problem loading snapin microsoft.sharepoint.powershell.'
                }
        } 
        else 
        {
            throw 'microsoft.sharepoint.powershell snapin is not installed on this system!'
        }  

Gives me:

PowerShell DSC resource SkaDscResourceSharePoint  failed to execute Test functionality with error message: There was a problem loading snapin microsoft.sharepoint.powershell. 
    + CategoryInfo          : InvalidOperation: (:) [], CimException
    + FullyQualifiedErrorId : ProviderOperationExecutionFailure
    + PSComputerName        : localhost

Any tips on how to proceed?

brgs

Bjørn

Is it safe to assume that the snap-in loads normally on the same machine if you do so manually?

Hi Don Jones,

That is correct.
Will each resource-block in a configuration run in its own runspace as default?
If this is the case, how would i go about properly adding/remvoving a snapin for a certain resource block?

brgs

Bjørn

Yes, each one runs independently, so each one will need to handle the snap in.

Ok thanks. I have now edited my dscresource (sharepoint operation) and added

Get-PSSnapin -Registered | ? {$_.name -eq "Microsoft.SharePoint.PowerShell"} | Add-PSSnapin
#SharePoint cmdlet: Get-SPCertificateAuthority
Get-PSSnapin -Registered | ? {$_.name -eq "Microsoft.SharePoint.PowerShell"} | Remove-PSSnapin

Running my mof again gives me:

The term 'Get-SPCertificateAuthority' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try aga
in.
    + CategoryInfo          : ObjectNotFound: (Get-SPCertificateAuthority:) [], CimException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : localhost

VERBOSE: [PSW0KOSS-APP11]: LCM:  [ End    Set      ]  [[CRLSiteSlownesFix]CRL]  in 1.0000 seconds.
The PowerShell DSC resource '[CRLSiteSlownesFix]CRL' with SourceInfo '\\shareserver\FrontEndServer.ps1::323::9::CRLSiteSlownesFix' threw one or more non-terminating erro
rs while running the Set functionality. These errors are logged to the ETW channel called Microsoft-Windows-DSC/Operational. Refer to this channel for more details.
    + CategoryInfo          : InvalidOperation: (:) [], CimException
    + FullyQualifiedErrorId : NonTerminatingErrorFromProvider
    + PSComputerName        : localhost

VERBOSE: [PSW0KOSS-APP11]:                            [] Consistency check completed.
The SendConfigurationApply function did not succeed. LCM failed to start desired state configuration manually.
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 1
    + PSComputerName        : localhost 

The resource-block is run with PsDscRunAsCredential user SharePoint setupuser who has full access to SharePoint farm and SharePoint snap in.

Any tips on what i am doing wrong?

brgs

Bjørn