Sometimes you run into an issue that is difficult to resolve. Recently I’ve been running a PowerShell script that needs to create 6000 site collections and even more sub sites within each site collection. Each of these site collections and sites have small adjustments made to them as a few custom lists and libraries are added to each site and web parts are added to the pages. During this process I was hitting one issues that I just couldn’t get resolved for a long time.

To create the site collections the script creates site collections based on classic teams sites and then PnP PowerShell is used to apply a template with all the custom bits needed in the site collection. The sub sites  are also based on the team site template, however I’m using standard SharePoint packages created by the Save site as template as this performs better than PnP PowerShell.

Activating user solutions

To install and activate the template for all the sub sites you only need two PowerShell commands:

Add-SPUserSolution -LiteralPath "C:\install\SiteCreationScripts\MySubsite Site.wsp" -Site $Site
$MySolution = Install-SPUserSolution -Identity "MySubsite Site.wsp" -Site $Site

Error received

These two commands would however give me the following error message every now and then.

The ‘Hidden’ attribute is invalid – The value ‘$Resources:core,True_Unless_Jpn;’ is invalid according to its datatype ‘http://schemas.microsoft.com/sharepoint/:TRUEFALSEorResource’ – The Enumeration constraint failed.

 

Auto restarting my script

The silly thing is that I would get the above error and then when I restart my PowerShell it would succeed once, but on the next site collection it would fail again. Initially to make this work I built into my script that it would autorestart with the following code and sometimes it would just work for multiple times. In short there seem to be some external factors that affect the installation of user solution in SharePoint. I’m using SharePoint 2016 at the moment, but I believe that this problem has been around for a while.

if ($AutoRestart)
{

Remove-SPUserSolution -Identity $solution.Name -Site $Site -Confirm:false

Write-Host "Going to delete the site."
Remove-SPSite $site.Url -Confirm:$false

Write-Host "Restart this process"
Start-Process powershell c:\install\SitecreationScripts\CreateSites.ps1

Stop-Process -Name powershell_ise
}
Exit

As I prefer to run in the PowerShell ISE for debugging purposes I will assume that I’m running my script within the ISE. I will only ever have one ISE open therefore killing the ISE is perfectly fine sledge hammer approach.  However you can’t start the PowerShell ISE and run code immediately therefore the above code will start a new powershell window and kill off my PowerShell ISE. At least now my site collections are being created and my script continues to run and create my sites and site collections.

Solution Attempt 1 – Memory Leaks

But still I don’t like the above error about the Japanese language pack that is missing. I’ve seen people suggest to fiddle with the solution package or install language packs. But I don’t have a Japanese language packs in my solution and I don’t need these.  Also why would these solutions install perfectly fine for a number of times and then suddenly stop working.

At first I considered memory leak problems. Initially my scripts were leaking memory like a bucket with a hole.

Using Start-SPAssignment and Stop-SPAssignment however this resolved my memory leaks the problem of the JPN language pack error would still not disappear.

The code I used to create my site collections and looked something like this. I simplified the script here a little bit.

$Site = New-SPSite -Url $siteUrl -Name $Title -Template "STS#0" -OwnerAlias $siteOwner.User.DisplayName -ContentDatabase $preferredContentDB
Add-SPUserSolution -LiteralPath "C:\install\SiteCreationScripts\MySubsite Site.wsp" -Site $Site
$MySolution = Install-SPUserSolution -Identity "MySubsite Site.wsp" -Site $Site

Solution Attempt 2 –  Cleanup code

Then I decided to create a separate function for adding the solution to the site collection as this would make it easier to debug the problem.

<br />function Add-SolutionsToSite {
Param([Microsoft.SharePoint.SPSite]$Site)

Add-SPUserSolution -LiteralPath "C:\install\SiteCreationScripts\MySubsite Site.wsp" -Site $Site
$MySolution = Install-SPUserSolution -Identity "MySubsite Site.wsp" -Site $Site

}

$site = New-SPSite -Url $siteUrl -Name $Title -Template "STS#0" -OwnerAlias $siteOwner.User.DisplayName -ContentDatabase $preferredContentDB
Add-SolutionsToSite -Site $sitethere

This made the script suddenly work a bit better and it ran for more than one day creating over 100 site collections and then suddenly the same error appeared

The ‘Hidden’ attribute is invalid – The value ‘$Resources:core,True_Unless_Jpn;’ is invalid according to its datatype ‘http://schemas.microsoft.com/sharepoint/:TRUEFALSEorResource&#8217; – The Enumeration constraint failed.

Aaaahhhh!

Solution Attempt 3 – Site references rather than objects

The only way to get back to a working situation is to restart PowerShell either manually or in the scripted way as described above. However, I’m not happy with that. So I made a further change rather than passing in the Site I’m passing in the SiteUrl rather than a site collection object. Also when I install the solution I’m using the Site Url rather than a site object.

function Add-SolutionsToSite {
Param([string]$SiteUrl)

Add-SPUserSolution -LiteralPath "C:\install\SiteCreationScripts\MySubsite Site.wsp" -Site $SiteUrl
$MySolution = Install-SPUserSolution -Identity "MySubsite Site.wsp" -Site $SiteUrl

}

$site = New-SPSite -Url $siteUrl -Name $Title -Template "STS#0" -OwnerAlias $siteOwner.User.DisplayName -ContentDatabase $preferredContentDB

Add-SolutionsToSite -Site $siteUrl

$site = Get-SPSite $siteUrl

Solution Attempt 4 – Slow down

Still I’m seeing the same problem. And it worked in the weekend.

So what would be different in the weekend? I’ve got users on the system.

Maybe the problem appears when the New-SPSite and the Add-SPUserSolution Cmdlets run to close after each other. This would also explain why things would work after I restart my script.

Time to add a Start-Sleep between the two lines responsible for creating the site and adding the solutions.

function Add-SolutionsToSite {
Param([string]$SiteUrl)

Add-SPUserSolution -LiteralPath "C:\install\SiteCreationScripts\MySubsite Site.wsp" -Site $SiteUrl
$MySolution = Install-SPUserSolution -Identity "MySubsite Site.wsp" -Site $SiteUrl

}

$site = New-SPSite -Url $siteUrl -Name $Title -Template "STS#0" -OwnerAlias $siteOwner.User.DisplayName -ContentDatabase $preferredContentDB
Start-Sleep -Seconds 30
Add-SolutionsToSite -Site $siteUrl

$site = Get-SPSite $siteUrl

With 10 second delays I still got failures but then as I increased the sleep to 30 seconds all worked as expected. So it looks like some of the features that are aqctivated as part of the site collection deployment take up to 30 seconds when the system is busy.

Advertisements