Hello
Here is my issue :
For a job, I have to write Powershell scripts in order to make SPsite creation more automatic and easier. When someone in our company wants a Sharepoint site, he/she asks to the "Help Desk". But the Help Desk doesn't have the access to the Sharepoint Server, so they use a Powershell script which puts in a file all infos we need in order to create the Sharepoint site, and then the script moves the file in a folder shared with our Sharepoint Server.
At the same time, a wmi instance is monitoring the shared folder. When it detects new files in the folder, it uses a CommandLineEventConsumer, and the cmd command launch another powershell script.
This second powershell script is the script that create Sharepoint sites. When I use it directly (meaning without the wmi instance and the CommandLineEventConsumer, by going directly on the Sharepoint Server), it works perfectly : the site is created, and the template is added then installed then applyed without any problem. But when the script is launched with the CommandLineEventConsumer, the site is created but there is no template on it ! I don't know why, but there is an issue when I add or install the template, because when I then apply the template, I have the exception "Exception calling "ApplyWebtemplate" with "1" argument(s) : "value cannort be null. Parameter name : webtemplate"". But I have no other exception, so I don't know where and when the problem started.
There is the code for the CommandLineEventConsumer :
$filterName = "AutomaticSPSiteCreation"
$Command = "cmd /C powershell -executionpolicy bypass -file C:\Sharepoint\Scripts\CreationCollectionSites.ps1"
$consumerPath = Set-WmiInstance -Class CommandLineEventConsumer `
-ComputerName $computer -Namespace $wmiNS `
-Arguments @{Name=$filterName; CommandLineTemplate=$Command}
And there is the code for adding/installing/applying the Template :
Add-SPUserSolution -LiteralPath $TemplatePath -Site $siteUrlInstall-SPUserSolution -Identity $TemplateName".wsp" -Site $siteUrl
Start-Sleep -Seconds 10;
$siteColl = new-Object Microsoft.SharePoint.SPSite($siteUrl)
$templateID = $siteColl.GetWebTemplates(1033) | Where-Object {$_.Title -eq $TemplateName}
$web = get-SPWeb $siteUrl
$web.ApplyWebTemplate($templateID.Name)
(Yes, I use Sharepoint site collection, not really Sharepoint site, but it's not the matter here)
I am really surprised by this issue, because, hum, the site is created, so it means that the second Powershell script has enough rights to create a site, so it doesn't seem like a right issue. And there is no problem when I use directly the second Powershell script, so it means that I am probably applying my template the right way. So why does it not work when the script is launched by the wmi-instance ?
If you have any ideas, I would be really thankful, because I really don't understand that.
Thanks !