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 } }
}
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.
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 } }
}
@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
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 ?
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 !)