【问题标题】:Test Dsc custom Resources测试 Dsc 自定义资源
【发布时间】:2016-03-07 10:34:12
【问题描述】:

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

【问题讨论】:

  • 在调用ServerTest的时候有没有给ServerTest指定参数(生成mof文件的步骤)

标签: powershell dsc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多