I want to create a PowerShell script that :
- add a farm solution that only contains a site template
- deploy the solution globally
- wait for the deploy job to finish
- create a site collection based on the site template
The first three steps are working as expected. The 4th does not. I get the following error :
New-SPSite : File or arguments not valid for site template 'myTemplate#0'.
Parameter name: WebTemplate
At D:\SCRIPT\install.ps1:73 char:11
+ New-SPSite <<<< $url -OwnerAlias "$user" -Name "my site name" -Template mytemplate#0" -Language 1036
+ CategoryInfo : InvalidData: (Microsoft.Share...SPCmdletNewSite:SPCmdletNewSite) [New-SPSite], ArgumentException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletNewSite
If I create the site collection from the central administration, it works perfectly.
I suspect there is a "cache" of available templates somewhere, but did not found neither the source, nor the solution.
Here is my full script :
Add-PSSnapin Microsoft.SharePoint.PowerShell
Start-SPAssignment -Global
function WaitForJobToFinish([string]$SolutionFileName)
{
$JobName = "*solution-deployment*$SolutionFileName*"
$job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
if ($job -eq $null) { Write-Host 'Timer job not found' }
else {
$JobFullName = $job.Name
Write-Host -NoNewLine "Waiting to finish job $JobFullName"
while ((Get-SPTimerJob $JobFullName) -ne $null) {
Write-Host -NoNewLine .
Start-Sleep -Seconds 2
}
Write-Host "Finished waiting for job.."
}
}
$user = (get-item env:userdomain).Value +'\' + (get-item env:username).Value
$webapp = 'http://myvirtualhost'
$url = 'http://myvirtualhost/sites/mysite'
$solutionName = 'mytemplate.wsp'
Add-SPSolution -LiteralPath (get-item $solutionName).Fullname
Install-SPSolution -Identity $solutionName -GACDeployment
WaitForJobToFinish $solutionName
remove-spsite $url -Confirm:$false
iisreset /noforce # trying to recycle the cache without success
New-SPSite $url -OwnerAlias "$user" -Name "my site name" -Template "mytemplate#0" -Language 1036
Stop-SPAssignment -Global