繁体   English   中英

测试Dsc自定义资源

[英]Test Dsc custom Resources

我是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的文件

当Test函数返回false时,我发现了自己的错误,然后仅对set函数进行了调用,而在Test函数中,它始终返回true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM