question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoke-SPDscCommand documentation improvement

See original GitHub issue

Just ran into previously described issue (because I was loading the SP PSSnapin in an external script) and found this great wiki entry: https://github.com/PowerShell/SharePointDsc/wiki/Using-the-Script-resource-in-configurations-that-use-SharePointDsc

It should be improved though as Arguments & Credential are not documented and I had to dig through source code to figure out how they work. I propose the following changes:

Script example:

Script SPDscExample
{
  GetScript = {
    Invoke-SPDscCommand -ScriptBlock {
      # your code goes here
      $params = $args[0]
      Write-Verbose $params.Foo
    } -Arguments @{ Foo = "Bar" } -Credentials (Get-Credentials)
  }
  ...
}

Additionally it should be noted that you can’t use DSC specific variables within the SPDscCommand, that means e.g. using $using:DscWorkingFolder in the script block will not work. You need to pass all external variables into SPDscCommand via its arguments.

Maybe we can even get a real wiki entry for Invoke-SPDscCommand like we have for all the resources?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ykuijscommented, Oct 31, 2019

I did some testing and it looks like this has something to do with the Script resource and the fact that snap-ins are loaded in a module scope. In this case inside of the module of the Script resource.

I tried to skip the Invoke-Command (when using PsDscRunAsCredential, this is not required since the resource already runs in a user context) cmdlet, but that also doesn’t work:

        Script TestAddSnapin
        {
          GetScript = {
            $SPSnapin = Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
            if ($null -eq $SPSnapin) {
		        Add-PsSnapin Microsoft.SharePoint.PowerShell
            }

            Write-Verbose "Returning Farm Build Version"
            $farm = Get-SPFarm
            Write-Verbose "Version: $($farm.BuildVersion)"
            return $farm.BuildVersion
          }
          TestScript = {
            $SPSnapin = Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
            if ($null -eq $SPSnapin) {
		        Add-PsSnapin Microsoft.SharePoint.PowerShell
            }

            Write-Verbose "Getting Farm"
            $farm = Get-SPFarm
            Write-Verbose "Version: $($farm.BuildVersion)"

            return $false
          }
          SetScript = {
            $SPSnapin = Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
            Write-Verbose "--$SPSnapin--"
            if ($null -eq $SPSnapin) {
		        Add-PsSnapin Microsoft.SharePoint.PowerShell
            }

            Write-Verbose "Getting Farm"
            $farm = Get-SPFarm
            Write-Verbose "Version: $($farm.BuildVersion)"
          }
          PsDscRunAsCredential = $SPSetupAccount
        }

When doing a Test-DscConfiguration, this works just fine. However when doing a Start-DscConfiguration, the Test part works but the Set part fails with the “An item with the same key has already been added” error.

When using Invoke-SPDscCommand this works fine, since that method exists in a separate module (SharePointDsc) and therefore has its own scope. Loading the snap-in from that module and testing if it is already loaded works as expected.

So you can keep using Invoke-SPDscCommand or create your own module, which you call from the Script resource and does the magic for you.

1reaction
ykuijscommented, Sep 12, 2019

Thanks for pointing this out! I have updated the example in the article.

The reason that we don’t have a real wiki article is that this function is a helper function for SharePointDsc and wasn’t created to be published (and used) outside of the module. However, due to the way modules work, we currently have no way of limiting the use of this function.

On the other hand: This is convenient because it enables the possibilities described in the wiki article you are referring to 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is Clinical Documentation Improvement (CDI)?
CDI (Clinical Documentation Improvement) is the process of improving medical record documentation for completeness, specifics, and accuracy.
Read more >
Insider Tips for getting Physician Participation in CDI ...
The message to physicians should be simple-good clinical documentation will improve communication, increase recognition of comorbid conditions that are ...
Read more >
Clinical Documentation Improvement Add-on
The CDI add-on to the online coding solutions helps coders in the translation of clinical documentation for complete and accurate coding, ...
Read more >
Five ways to improve clinical documentation and bridge ...
Five strategies for improving clinical documentation compliance · 1. Do the pre-work before launching a CDI program · 2. Find a physician champion...
Read more >
Clinical Documentation Improvement
Support high-quality care with quality documentation. Clinical documentation improvement (CDI) makes a difference to everyone on the care team.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found