Non-Terminating error executing Test-TargetResource

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?

Jeff -

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

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
<<<

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

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.

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”.

https://powershell.org/forums/topic/using-cim-to-create-a-shared-folder/#post-15296

Check it out,
Daniel