dsccommunity / SharePointDsc

The SharePointDsc PowerShell module provides DSC resources that can be used to deploy and manage a SharePoint farm
MIT License
245 stars 107 forks source link

[SPFarm] Import-Module SharePointServer causes exception in SharePoint Subscription 23H1 #1424

Closed Yvand closed 1 year ago

Yvand commented 1 year ago

Problem description

Specifically in a VM with SharePoint Subscription + CU 23H1 installed, resource SPFarm throws an exception when creating the SharePoint farm. The exact same exception can be reproduced outside of SharePointDsc, by running the code below in a Windows PowerShell prompt:

Import-Module SharePointServer -Verbose:$false

The block below (set in in function Invoke-SPDscCommand) will cause the exception later, when running Invoke-Command:

{
        $baseScript = @"
            Import-Module SharePointServer -Verbose:`$false -WarningAction SilentlyContinue

"@
    }

If I comment the block above, resource SPFarm creates the farm successfully and with no error

Verbose logs

VERBOSE: [2023-03-20 10:28:47Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] Server not part of 
farm, creating or joining farm
VERBOSE: [2023-03-20 10:28:47Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] This node has never 
been connected to a farm
VERBOSE: [2023-03-20 10:28:47Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] Executing as the 
local run as user CONTOSO\spsetup
VERBOSE: [2023-03-20 10:28:49Z] [ERROR] You cannot call a method on a null-valued expression.
VERBOSE: [2023-03-20 10:28:51Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] Importing function 
'Add-SPDscConfigDBLock'.
VERBOSE: [2023-03-20 10:28:51Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] Importing function 
'Get-SPDscConfigDBStatus'.
VERBOSE: [2023-03-20 10:28:51Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] Importing function 
'Get-SPDscSQLInstanceStatus'.
VERBOSE: [2023-03-20 10:28:51Z] [VERBOSE] [SP]:                            [[SPFarm]CreateSPFarm] Importing function 
'Remove-SPDscConfigDBLock'.

DSC configuration

SPFarm CreateSPFarm
{
    DatabaseServer            = $SQLAlias
    FarmConfigDatabaseName    = $SPDBPrefix + "Config"
    Passphrase                = $SPPassphraseCreds
    FarmAccount               = $SPFarmCredsQualified
    PsDscRunAsCredential      = $SPSetupCredsQualified
    AdminContentDatabaseName  = $SPDBPrefix + "AdminContent"
    CentralAdministrationPort = $SharePointCentralAdminPort
    RunCentralAdmin           = $true
    IsSingleInstance          = "Yes"
    SkipRegisterAsDistributedCacheHost = $false
    Ensure                    = "Present"
}

Suggested solution

It looks like the whole PowerShell module SharePointServer was removed starting with SharePoint Subscription 23H1, but I did not hear about it so I cannot guarantee that this is the root cause

EDIT: I tested to never load module SharePointServer if it is SharePoint Subscription, and so far it worked with both SharePoint Subscription RTM and SharePoint Subscription 23H1, so it seems that an easy fix is to simply skip that for SharePoint Subscription. I'll submit a PR soon with this change and wait for your feedback on it

SharePoint version and build

SharePoint Subscription 23H1 (2023-03 CU) - 16.0.16130.20206

Operating system the target node is running

OsName               : Microsoft Windows Server 2022 Datacenter Azure Edition
OsOperatingSystemSKU : 407
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.1366
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.20348.1366
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

SharePointDsc version

Name          Version Path
----          ------- ----
SharePointDSC 5.3.0   C:\Users\yvand\Documents\WindowsPowerShell\Modules\SharePointDSC\5.3.0\SharePointDSC.psd1
ykuijs commented 1 year ago

What error/exception are you getting? I am unable to reproduce the issue when running Import-Module SharePointServer -Verbose:$false. I get the following warning, but no exception

PS C:\> Import-Module SharePointServer -Verbose:$false
WARNING: The names of some imported commands from the module 'SharePointServer' include unapproved verbs that might
make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.
Yvand commented 1 year ago

It really is the exception you see in the issue description:

[ERROR] You cannot call a method on a null-valued expression.

To reproduce it, you need to have a server with SharePoint Subscription 23H1, and that has never been joined to a farm (maybe it is enough that it is not member of a farm). I do not repro this error if the server is already joined to a farm.

ykuijs commented 1 year ago

Ah, check! That explains why I wasn't able to reproduce, I tested it on a farm joined to the farm 😉

My guess is that a change in the module has been implemented in 23H1 that tries to connect to the farm. Since the server hasn't been joined yet, it throws this error. By removing the explicit import, the module is only loaded on-demand further down the code and apparently the issue has been resolved.

Will merge the PR once the unit tests are done!