测试Dsc自定义资源

时间:2016-03-07 10:34:12

标签: powershell dsc

我是DSC自定义资源的新手。

我创建了1个自定义dsc资源并放入 C:\ Program Files \ WindowsPowerShell \ Modules 路径 为自定义资源代码创建的文件位于

之下

`

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $ServerURL,
        [parameter(Mandatory = $true)]
        [System.String]
        $ResultFilePath
    )
    Try
    {
        $res=Get-WmiObject win32_service -ComputerName $ServerURL -Filter "Name = 'wuauserv'"

        if($res.Status -eq "OK")
        {
            if((Test-Path $ResultFilePath))
            {
                Out-File $ResultFilePath -InputObject "$ServerURL is Running"
            }
            else
            {
                New-Item -Path $ResultFilePath -ItemType file -Value "$ServerURL is Running"
            }
        }
        else
        {
            if((Test-Path $ResultFilePath))
            {
                Out-File $ResultFilePath -InputObject "$ServerURL is not Running"
            }
            else
            {
                New-Item -Path $ResultFilePath -ItemType file -Value "$ServerURL is not Running"
            }
        }
    }
    Catch
    {
            if((Test-Path $ResultFilePath))
            {
                Out-File $ResultFilePath -InputObject "$ServerURL is not Running"
            }
            else
            {
                New-Item -Path $ResultFilePath -ItemType file -Value "$ServerURL is not Running"
            }
    }

}

`

DSC资源文件的使用在

之下
Configuration ServerTest
{ 

    param(
        [Parameter(Mandatory=$True,Position=0)]
        $MachineName,
        [Parameter(Mandatory=$True,Position=1)]
        $ServerIPOrMachineName,
        [Parameter(Mandatory=$True,Position=2)]
        $ResultFilePath


    )
    #param ($MachineName="localhost") 
    Try
    {
        Import-DscResource -ModuleName ServerStatusDSCmodule
        node "localhost" 
        { 
             ServerStatusDSCResource MyServerTest 
             { 
                ServerURL =  $ServerIPOrMachineName
                ResultFilePath = $ResultFilePath
             }
        } 

     }
     Catch
     {

     }

}
ServerTest

这将创建Servertest文件夹和localhost.mof但是当我运行 Start-DscConfiguration -Path“D:\ ServerTest \”时,它将无法创建给予$ ResultFilePath的文件

1 个答案:

答案 0 :(得分:0)

我发现我的错误,当Test函数返回false然后只设置函数调用时,在我的Test函数中它总是返回true。