Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPSite: Microsoft.SharePoint.SPSite Constructur returns $null when run as Ressource #1442

Open
ChristophHannappel opened this issue Nov 21, 2024 · 7 comments · May be fixed by #1443
Open

SPSite: Microsoft.SharePoint.SPSite Constructur returns $null when run as Ressource #1442

ChristophHannappel opened this issue Nov 21, 2024 · 7 comments · May be fixed by #1443

Comments

@ChristophHannappel
Copy link
Contributor

ChristophHannappel commented Nov 21, 2024

Problem description

When the Get-TargetResource Function tries to load an existing site collection it always returns $null when run by the LCM:

try
{
$centralAdminWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
$centralAdminSite = Get-SPSite -Identity $centralAdminWebApp.Url -ErrorAction Stop
$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList @($params.Url, $centralAdminSite.SystemAccount.UserToken)
}
catch [System.Exception]
{
}

Verbose logs

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = ResourceGet,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer ServerName with user sid <SID>.
VERBOSE: [ServerName]:                            [[SPSite]DirectResourceAccess] Getting site collection https://sharepoint.contoso.com/sites/MySiteCollection
VERBOSE: [ServerName]:                            [[SPSite]DirectResourceAccess] Executing as the local run as user DOMAIN\DSCRunAsAccount
VERBOSE: [ServerName]:                            [[SPSite]DirectResourceAccess] Site Collection not found
VERBOSE: [ServerName]: LCM:  [ End    Get      ]  [[SPSite]DirectResourceAccess]  in 0.2780 seconds.
VERBOSE: [ServerName]: LCM:  [ End    Set      ]    in  0.3410 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.


ConfigurationName        : 
DependsOn                : 
ModuleName               : SharePointDSC
ModuleVersion            : 5.4.0
PsDscRunAsCredential     : 
ResourceId               : 
SourceInfo               : 
AdministrationSiteType   : 
CompatibilityLevel       : 
ContentDatabase          : 
CreateDefaultGroups      : 
Description              : 
HostHeaderWebApplication : 
Language                 : 
Name                     : 
OwnerAlias               : 
OwnerEmail               : 
QuotaTemplate            : 
SecondaryEmail           : 
SecondaryOwnerAlias      : 
Template                 : 
Url                      : https://sharepoint.contoso.com/sites/MySiteCollection
PSComputerName           : localhost

DSC configuration

SPSite SiteCollection
{
    PsDscRunAsCredential = $cred   
    Url = 'https://sharepoint.contoso.com/sites/MySiteCollection'
    OwnerAlias = 'Domain\Username'
}

Suggested solution

Switch to $site = Get-SPSite -Identity $params.Url -ErrorAction SilentlyContinue

SharePoint version and build

SharePoint Server Subscription Edition 16.0.17928.20238

Operating system the target node is running

OsName               : Microsoft Windows Server 2022 Datacenter
OsOperatingSystemSKU : DatacenterServerEdition
OsArchitecture       : 64-bit
WindowsVersion       : 2009
WindowsBuildLabEx    : 20348.1.amd64fre.fe_release.210507-1500
OsLanguage           : en-US
OsMuiLanguages       : {en-US}

PowerShell version and build the target node is running

Name                           Value                                                                                                      
----                           -----                                                                                                      
PSVersion                      5.1.20348.2760                                                                                             
PSEdition                      Desktop                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                    
BuildVersion                   10.0.20348.2760                                                                                            
CLRVersion                     4.0.30319.42000                                                                                            
WSManStackVersion              3.0                                                                                                        
PSRemotingProtocolVersion      2.3                                                                                                        
SerializationVersion           1.1.0.1

SharePointDsc version

Name          Version Path                                                                             
----          ------- ----                                                                             
SharePointDSC 5.4.0   C:\Program Files\WindowsPowerShell\Modules\SharePointDSC\5.4.0\SharePointDSC.psd1
@ChristophHannappel
Copy link
Contributor Author

@ykuijs do you know if there was a special use case to get the Site Collection with

$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList @($params.Url, $centralAdminSite.SystemAccount.UserToken)

and not Get-SPSite ?

Also if you run the current code inside a PowerShell with the DSCRunAsAccount it works, it just breaks when run by them LCM 😆

@ChristophHannappel
Copy link
Contributor Author

I think it was changed with #875 but i do not understand why🤔

@ChristophHannappel
Copy link
Contributor Author

I found the PR #904 which explains the reason. Since its from 2018 - did a patch change that behaviour?
I got the bug on two SP Subscription Edition Farms. I'll test on 2016 and 2019 if they have the same issue.

@ChristophHannappel
Copy link
Contributor Author

SharePoint 2016 and 2019 works fine.

@ykuijs
Copy link
Member

ykuijs commented Nov 21, 2024

Has been a while, so had to think hard what the reasoning was. Fortunately I documented this in PR 904 😉

Just to get things clear: The current code works on 2016 and 2019, but doesn't work properly on SPSE. Is that correct?

If so, we can add a check to see if SPSE is used and in that case use Get-SPSite. But when 2016 or 2019 is used, use the current code.

@ChristophHannappel
Copy link
Contributor Author

Has been a while, so had to think hard what the reasoning was. Fortunately I documented this in PR 904 😉

Indeed :)

Just to get things clear: The current code works on 2016 and 2019, but doesn't work properly on SPSE. Is that correct?

Yes, it works on 2016 and 2019, but not on SPSE - at least my environments.

If so, we can add a check to see if SPSE is used and in that case use Get-SPSite. But when 2016 or 2019 is used, use the current code.

Good Idea, i'll use the following if statement, from the SPDistributedCacheService Resource.

$productVersion = Get-SPDscInstalledProductVersion
if ($productVersion.FileMajorPart -eq 16 `
-and $productVersion.FileBuildPart -gt 13000)
{

@ykuijs
Copy link
Member

ykuijs commented Nov 22, 2024

That is exactly what you need indeed. We have used this logic on several occasions in SPDsc to make sure we can do specific actions for a specific version. The SPInstallPrereqs resource for example, where we have to install the prereqs for each specific version of SharePoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants