Please feel free to provide feedback or file bugs here.

Determine who is calling TestScript

The TestScript code in a Powershell DSC Script resource is executed by calling Test-DscConfiguration and by calling Start-DscConfiguration. I would like to determine which cmdlet called TestScript in the TestScript code.

Suggested solution: property in $MyInvocation object

Use case: write an event to the eventlog "Starting DSC config" only when Start-DscConfiguration is ran.

Example code:
Script MyScript
{
TestScript = {
if ($MyInvocation.<property> -eq 'Test-DscConfiguration') {
Write-Verbose "Called by Test-DscConfiguration"
$true #do not run SetScript
}
else {
Write-Verbose "Called by Start-DscConfiguration"
$false #run SetScript
}
}
SetScript = { Write-EventLog -LogName "Application" -Source "MyCompany" -EventID 1 -EntryType Information -Message "Starting DSC Configuration" }
GetScript = { return @{ Result = $true } }
}

Additional requirement is that Test-DscConfiguration should return InDesiredState=$true, so the following code is not a solution:

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"
}
GetScript = { return @{ Result = $true } }
}

2 votes
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)

    We’ll send you updates on this idea

    RoelP shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base