Non-Terminating error executing Test-TargetResource

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 2 years, 6 months ago.

  • Author
    Posts
  • #15922
    Profile photo of Jeff Pflum
    Jeff Pflum
    Participant

    I am trying to write a custom resource to create Smb Shares for Win2008 R2. I am using "net share somename" to see if a share existing in my implementation of Test-TargetResource. When I test this function directly, it works fine even though the "net share" command writes out an error. But when this gets called by the LCM, it reports a bunch of errors and discontinues processing the resource. One error is:

    >>>
    Job {35B5FC76-0FC6-4199-8B33-91CD0AACE2C8} :
    This event indicates that a non-terminating error was thrown when DSCEngine was executing Test-TargetResource on FMGlobal_SmbShare provider. FullyQualifiedErrorId is NativeCommandError. ErrorMessage is This shared resource does not exist..
    < << ... followed by... >>>
    Job {35B5FC76-0FC6-4199-8B33-91CD0AACE2C8} :
    DSC Engine Error :
    Error Message The SendConfigurationApply function did not succeed.
    Error Code : 1
    < << I am invoking "net share" as follows.. <<< $Name = "SomeShare" $command = "net share " + $Name $results = Invoke-Expression $command -ErrorAction Ignore -WarningAction Ignore if ($LASTEXITCODE -eq 0) { } >>>

    How can I tell the LCM to ignore this and continue to process?

  • #15923
    Profile photo of Jacob Benson
    Jacob Benson
    Member

    Jeff –

    If you set $ErrorAction to SilentlyContinue does the same thing still happen?

  • #15924
    Profile photo of Jeff Pflum
    Jeff Pflum
    Participant

    If you mean the -ErrorAction parameter to Invoke-Expression, yes I had tries setting it to "SilentlyContinue" as well, and had the same problem.

    Here is what I came up with that works, but does require making a second call to get the actual results I need to process which are not available when directing StdOut and StdErr to Out-Null. As being new to PowerShell and DSC, I am still curious how to suppress errors from native calls that result in the LCM treating it as an error and discontinuing. The code...

    >>>
    $command = "net share " + $Name
    Invoke-Expression $command -ErrorAction Ignore -WarningAction Ignore 2>&1 | Out-Null
    if ($LASTEXITCODE -eq 0)
    {
    $results = Invoke-Expression $command
    <<<

  • #15927
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Jeff,

    I would leverage CIM/WMI to check if the share exists instead of "net.exe share $Name". I know that creating a share is different story and net.exe might be easier if you're dealing with complex share permissions.

    $ShareName = 'Users'
    $Share = Get-CimInstance -ClassName Win32_Share -Filter "Name='$ShareName'"
    return ($Share -ne $null)

    Just an idea,
    Daniel

  • #15933
    Profile photo of Jeff Pflum
    Jeff Pflum
    Participant

    Yes – much better than calling "net share $Name" to see if it exists, and if it does, to get its path. I was having to parse the array of strings returned to get at the Path information. Thanks for pointing this out.

  • #15934
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Jeff,

    Richard Siddaway posted a script on another post to create a share using CIM/WMI a few weeks ago which I think could be used as part of Set-TargetResource instead of "net.exe share".

    Check it out,
    Daniel

You must be logged in to reply to this topic.