Author Posts

November 13, 2014 at 9:02 am

Hi,
I've decided to use DSC to configure our sharepoint environment.
But I'm having problems loading the SharePoint cmdlets from the second module in the configuration. It always throws an error. Could you please have a look and help me how to proceed?

I've created a sample module and configuration file for reference. The module given below and the second one have the same content.

function Get-TargetResource
{
	[CmdletBinding()]
	[OutputType([System.Collections.Hashtable])]
	param
	(
		[parameter(Mandatory = $true)]
		[System.String]
		$Site1
	)
}


function Set-TargetResource
{
	[CmdletBinding()]
	param
	(
		[parameter(Mandatory = $true)]
		[System.String]
		$Site1
	)
}


function Test-TargetResource
{
	[CmdletBinding()]
	[OutputType([System.Boolean])]
	param
	(
		[parameter(Mandatory = $true)]
		[System.String]
		$Site1
	)

	 If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null){
        Write-Verbose "Loading SharePoint Powershell Snapin..."
        Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
     }  
     if ((Get-SPSite -Limit All | ? {$_.Url -eq $Site1})-eq $null){
        Write-Verbose "$Site1 not found"
        return $false
     }
     else{
        return $true
     }
}


Export-ModuleMember -Function *-TargetResource


My configuration file looks like

Configuration SPSiteConfig {
    Import-DscResource -ModuleName xDSCSPSite1
    Import-DscResource -ModuleName xDSCSPSite2
    Node SER-XXX{

        xDSCSPSite1 Site1{
            Site1 = "http://Site1-d"
        }
        

        xDSCSPSite2 Site2 {            
            Site2 = "http://Site2-d"
        }
    }
}
SPSiteConfig

Start-DscConfiguration -Wait -Verbose -Path .\SPSiteConfig

But when I run this configration I get the error as

PS E:\DSC> .\SPSiteConfig.ps1


    Directory: E:\DSC\SPSiteConfig


Mode                LastWriteTime     Length Name                                                                                                         
----                -------------     ------ ----                                                                                                         
-a---        13-11-2014     17:51       1488 SLO-702.mof                                                                                                  
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationMan
ager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer SLO-702 with user sid S-1-5-21-1229272821-1801674531-839522115-133922.
VERBOSE: [SLO-702]: LCM:  [ Start  Set      ]
VERBOSE: [SLO-702]: LCM:  [ Start  Resource ]  [[xDSCSPSite1]Site1]
VERBOSE: [SLO-702]: LCM:  [ Start  Test     ]  [[xDSCSPSite1]Site1]
VERBOSE: [SLO-702]:                            [[xDSCSPSite1]Site1] Loading SharePoint Powershell Snapin...
VERBOSE: [SLO-702]:                            [[xDSCSPSite1]Site1] Leaving BeginProcessing Method of Get-SPSite.
VERBOSE: [SLO-702]:                            [[xDSCSPSite1]Site1] Leaving ProcessRecord Method of Get-SPSite.
VERBOSE: [SLO-702]:                            [[xDSCSPSite1]Site1] Leaving EndProcessing Method of Get-SPSite.
VERBOSE: [SLO-702]:                            [[xDSCSPSite1]Site1] http://Site1-d not found
VERBOSE: [SLO-702]: LCM:  [ End    Test     ]  [[xDSCSPSite1]Site1]  in 5.9720 seconds.
VERBOSE: [SLO-702]: LCM:  [ Start  Set      ]  [[xDSCSPSite1]Site1]
VERBOSE: [SLO-702]: LCM:  [ End    Set      ]  [[xDSCSPSite1]Site1]  in 0.0060 seconds.
VERBOSE: [SLO-702]: LCM:  [ End    Resource ]  [[xDSCSPSite1]Site1]
VERBOSE: [SLO-702]: LCM:  [ Start  Resource ]  [[xDSCSPSite2]Site2]
VERBOSE: [SLO-702]: LCM:  [ Start  Test     ]  [[xDSCSPSite2]Site2]
VERBOSE: [SLO-702]:                            [[xDSCSPSite2]Site2] Loading SharePoint Powershell Snapin...
The term 'Get-SPSite' 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 again.
    + CategoryInfo          : ObjectNotFound: (Get-SPSite:) [], CimException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : SLO-702
 
VERBOSE: [SLO-702]: LCM:  [ End    Test     ]  [[xDSCSPSite2]Site2]  in 0.3880 seconds.
The PowerShell provider xDSCSPSite2 threw one or more non-terminating errors while running the Test-TargetResource 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        : SLO-702
 
VERBOSE: [SLO-702]: LCM:  [ End    Set      ]
The SendConfigurationApply function did not succeed.
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 1
    + PSComputerName        : SLO-702
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 7.578 seconds

I've attached the module and mof files for reference.

November 18, 2014 at 10:06 am

If you run the -TargetResource functions manually, just from a PowerShell prompt, do they do anything different?

November 19, 2014 at 7:59 am

Hi Jones,
It works fine. Even the first module in the configuration works without an issue. Its the second module that has the issue with the snapin.

Anyway,
I figured out a way to get this thing resolved. I dispose the snapin at the end of every function in the module.

 finally{
        If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-ne $null){
            Write-Verbose "Removing the PowerShell snapin"
            Remove-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
        } 
    }