我是DSC的新客户资源。
我创建了一个定制的dsc资源并放入C:\Program为自定义资源代码创建的路径文件如下
`
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的文件。
发布于 2016-03-11 06:14:54
当Test函数返回false,然后只返回set函数调用时,我发现了我的错误,并且在我的Test函数中,它总是返回true。
https://stackoverflow.com/questions/35841460
复制相似问题