Script Resource: Ensure Absent?

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 2 years, 1 month ago.

  • Author
    Posts
  • #19574
    Profile photo of Michael Craig
    Michael Craig
    Participant

    I'm starting to use the script resource quite a bit, and after writing a few resources I realize that there is no "Ensure = Present/Absent"
    My question is, how are people handling this?
    I'm trying to write my DSC script so that I can set a variable at the top:
    (pre)
    param(
    [string]$Action="Present"
    )
    (/pre)

    The idea being that I can use the same script to generate a mof to build a server, and also to generate a mof to revert a server.
    Has anyone else tackled this already?

    I'm thinking I could use my $Action as a switch statement in both the Test and Set actions to either ensure the result is present or absent.

    Q: In general are people doing this, or is this a waste of time?

    Thanks,

    Mike

  • #19595
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Correct. Not all resources provide an "Ensure" setting; the Script Resource only provides Get/Set/Test settings, which are script blocks. If you want something gone, you write a script item that makes sure it's gone. If you want something present, you write a separate script setting to make it present. The downside of script resources is that they're not as easily reusable across situations.

    What you probably want to do instead is write a custom resource, as a script module. There, you can definitely define an Ensure setting, and your Get/Set/Test functions can handle it accordingly. What you're trying to do with the script resource is more than it was intended for, and you're going to fight it.

You must be logged in to reply to this topic.