运行 Test-TargetResource 的结果必须是布尔值 True 或 False。数码相机

The results from running Test-TargetResource must be the boolean value True or False. DSC

我正在尝试构建自定义 DSC 资源,但失败并出现以下错误。

返回的结果格式无效。 运行ning Test-TargetResource 的结果必须是布尔值 True 或 False。

当我 运行 在 powershell 提示中手动执行该函数时,它确实成功并且 returns 我得到一个布尔值。

这是我用于 Test-TargetResource

的代码
function Test-TargetResource
{

[OutputType([boolean])]
param (
[parameter(Mandatory)]
[string]
$vcentername,
[parameter(Mandatory)]
[string]
$credentialfile,
[parameter(Mandatory)]
[string]
$resourcepoolname,
[ValidateSet('Present','Absent')]
[string]
$Ensure = 'Present'
)

try 

{

#Addpssnapin VMware.VimAutomation.Core

$snap = Add-PSSnapin VMware.VimAutomation.Core

$MyCredentials= Import-Clixml $credentialfile

Write-Verbose "Connecting to vCenter $vcentername"

$connect = Connect-VIServer $vcentername -Credential $MyCredentials

Write-Verbose "Checking if the snapshots exist or doesnot exist"
$snapshotExist = (Get-ResourcePool $resourcepoolname | Get-VM | Get-Snapshot)


if ($Ensure -eq "Present") {

if ($snapshotExist) {

Write-Verbose "Snapshots Exists for these set of VM's. No need of taking further action"
return $true

} 
else {
Write-Verbose "Snapshots don't exists for these set of VM's in $resourcepoolname"
return $false
}
} 


else {

if ($snapshotExist) {
Write-Verbose "Snapshots exist on these VM's; snapshots must be removed."
return $false
} 

else {
Write-Verbose "Snapshots does not exist; nothing to remove."
return $true
}

}


}
    catch {
    $exception = $_
    Write-Verbose "Error occurred while executing Test-TargetResource function"
    while ($exception.InnerException -ne $null)
    {
    $exception = $exception.InnerException
    Write-Verbose $exception.message
    }
    }


    }

谁能帮帮我。

您的 catch 块没有 return [bool]。您可以在屏幕截图的详细输出中看到 catch 块被命中。你应该在 catch.

的末尾 return $false
catch {
    $exception = $_
    Write-Verbose "Error occurred while executing Test-TargetResource function"
    if ($exception.InnerException -ne $null)
    {
        $exception = $exception.InnerException
        Write-Verbose $exception.message
    }
    return $false
}