-
Notifications
You must be signed in to change notification settings - Fork 108
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 throw an exception if it executed after Script with "Add-PSSnapin Microsoft.SharePoint.PowerShell" code inside. #566
Comments
Thanks for reporting this Denis - the fact that you are calling it before another script resource that loads the snapin doesn't/shouldn't matter. When we load the snapin we do it with this script:
Now each resource runs in its own runspace so loading it multiple times shouldn't make a difference, but we always test to see if it is loaded before we load it, so we'll never load it twice. So working backwards from there, we're going to need to see the verbose logs from this one executing so we can figure out which line its likely failing at. Are you able to share the verbose output from that resource when you run it so I can confirm where this goes? |
Hi, Brian. Thanks for your response. Yes, I've uploaded archive with logs and DSC scripts files. I'll also copy logs here:
I'm not sure, but may be this error is connected to case, described here Thanks. |
ok, so maybe you might be on to something here. I would expect to see the verbose line from the start of our test function before the error, so the only place this can be faulting has got to be where we are loading the snap in. So lets go through the stuff you have here. In the linked example, the issue is that you are genuinely loading it twice. I can see that the code here is mimicking what we do in our module, but the issue there is that in Module3Function you end up reloading the module because it has already been loading by the script invoker in Module2Function - just that the Get-PSSnapin doesn't return it properly (which is something we saw very early on), so we have to make the assumption that anything that mimics the Module3Function will be loaded where the snapin is already loaded, so it shouldn't make a call to load the snapin ever. So extrapolate that out to what we do in SharePointDsc and I think what I would do for your script block is to do things like we do and use "Invoke-SPDscCommand" to run your set script code, and not load the snapin there at all. So something like this:
Now this will load the snapin for you the way we do, which we know works from one resource to the next with no issues, so it should be able to do the same. We haven't publicly documented how to call Invoke-SPDscCommand but I'm willing to open it up and make it a publicly usable approach for exactly this scenario. So let me know how that goes and we can go from there. |
Hi, Brian. Workaround with Invoke-SPDscCommand is working for me. Now I have no errors during script execution. Thanks a lot. |
Closing the loop on this, I've formally documented this in the wiki |
Details of the scenario you try and problem that is occurring:
If SPSite resource is executed after Script resource with "Add-PSSnapin Microsoft.SharePoint.PowerShell" it throw an exception:
The DSC configuration that is using the resource:
Script Script1
{
TestScript = {
return $false
}
SetScript = {
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
}
GetScript = { }
}
SPSite TestAppSite
{
DependsOn = "[Script]Script1"
Url = "https://sharepoint.com/sites/TestSite"
OwnerAlias = "DOMAIN\LOGIN"
Name = "Test site"
Template = "STS#0"
PsDscRunAsCredential = $installCredentials
}
Version of the Operating System and PowerShell the DSC Target Node is running:
Windows Server 2012 R2, PowerShell 5.1, SharePoint 2016
Version of the DSC module you're using:
1.6.0.0
The text was updated successfully, but these errors were encountered: