Determine who is calling TestScript?

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of JC Ruiz JC Ruiz 2 weeks, 2 days ago.

  • Author
    Posts
  • #52991
    Profile photo of RoelP
    RoelP
    Participant

    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 3 months, 1 week ago by Profile photo of RoelP RoelP.
  • #53031
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #53086
    Profile photo of Nitin Gupta
    Nitin Gupta
    Participant

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

  • #53143
    Profile photo of RoelP
    RoelP
    Participant

    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 3 months, 1 week ago by Profile photo of RoelP RoelP. Reason: Added example code
  • #53148
    Profile photo of RoelP
    RoelP
    Participant

    @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 3 months, 1 week ago by Profile photo of RoelP RoelP.
  • #53569
    Profile photo of Nitin Gupta
    Nitin Gupta
    Participant

    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 ?

  • #54091
    Profile photo of RoelP
    RoelP
    Participant
  • #58574
    Profile photo of JC Ruiz
    JC Ruiz
    Participant

    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

You must be logged in to reply to this topic.