首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试Dsc自定义资源

测试Dsc自定义资源
EN

Stack Overflow用户
提问于 2016-03-07 10:34:12
回答 1查看 193关注 0票数 0

我是DSC的新客户资源。

我创建了一个定制的dsc资源并放入C:\Program为自定义资源代码创建的路径文件如下

`

代码语言:javascript
复制
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资源文件的使用情况

代码语言:javascript
复制
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的文件。

EN

回答 1

Stack Overflow用户

发布于 2016-03-11 06:14:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35841460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档