Author Posts

September 1, 2016 at 1:07 pm

The TestScript code in a Powershell DSC Script resource is executed by calling Test-DscConfiguration and by calling Start-DscConfiguration. Is it possible in the TestScript code to determine which cmdlet called TestScript?
I have played with $MyInvocation but that didn't solve my problem
Thanks!

Example:

Script MyScript
{
  TestScript = {
    if "determine caller code" {
      Write-Verbose "Called by Test-DscConfiguration"
      $true #do not run SetScript
    }
    else {
      Write-Verbose "Called by Start-DscConfiguration"
      $false #run SetScript
    }
  }
  SetScript = { Write-Verbose "Execute some code here" }
  GetScript = { return @{ Result = $true } }
}
  • This topic was modified 2 years ago by  RoelP.

September 1, 2016 at 2:03 pm

Nope. The LCM doesn't relay that information – from the script's point of view, there's no difference. Test is supposed to be an atomic and consistent operation – it shouldn't behave differently.

September 1, 2016 at 5:43 pm

RoelP, I am curious to know the scenario that lead you to this question.

September 2, 2016 at 7:46 am

Thanks for the replies. My scenario is as follows:
At the start of a Configuration I would like to log an event in the eventlog

I would like to only write this event when Start-DscConfiguration is executed, not when someone starts Test-DscConfiguration (which does not do any actual configuration).

This could be done with below example, but the problem is that Test-DscConfiguration -Detailed returns InDesiredState=$false. This resource should not prevent the Node from being in the desired state.

Example

Script Init
{
  TestScript = {
    $false #problem: Test-DscConfiguration reports the resource is not in the desired state
  }
  SetScript = {
    Write-EventLog -LogName "Application" -Source "MyCompany" -EventID 1 -EntryType Information -Message "Starting DSC Configuration with parameter $using:Node.SomeParameter"
  }
  GetScript = { return @{ Result = $true } }
}
  • This reply was modified 2 years ago by  RoelP. Reason: Added example code

September 2, 2016 at 8:05 am

@Don Jones: the LCM may not relay this information to the script, but it sure knows it
If you run Get-DscConfigurationStatus in the TestScript section, you get an error message: The ... cmdlet is in progress...
... contains Start-DscConfiguration or Test-DscConfiguration

  • This reply was modified 2 years ago by  RoelP.

September 6, 2016 at 6:10 pm

You are right, DSC doesn't support it yet and there is no reliable way to achieve it. Can you file a uservoice item for this functionality PowerShell DSC UserVoice ?

November 23, 2016 at 4:38 pm

Having exactly the same issue as RoelP (Writing to a log from inside a resource), I've followed Don's idea.
In my case the workaround works perfectly (in WMF 5 !)

           TestScript = {                      
                           $Lcm =  Get-DscLocalConfigurationManager 
                           $mode = $LCM.LCMStateDetail
                           return ($mode -match "Testing") 
                         }   

So maybe someone can reuse this.
jc