adding Sharepoint snapin into a custom dscmodule

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Bjørn Roalkvam Bjørn Roalkvam 2 months, 1 week ago.

  • Author
    Posts
  • #60439
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    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

  • #60582
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #61729
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    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

  • #61734
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #61771
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    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

You must be logged in to reply to this topic.