成功执行Powershell DSC脚本资源失败安装Apache

时间:2015-12-02 17:51:00

标签: powershell powershell-v4.0 dsc

我正在尝试使用Powershell 4.0 / Desired State Configuration在Windows 2012上安装Apache 2.4。由于Apache显然不更新WMI数据库,我认为我不能使用包资源,因为它永远不会匹配名称或产品ID。 相反,我写了一个脚本资源:

Script InstallApache
{
    DependsOn = @("[Environment]Apache","[File]httpdConf")
    SetScript = {
        $Quiet = Invoke-Expression "d:\Apache24\bin\httpd.exe -k install"
    }
    TestScript = {
        if(Get-Service | Where-Object {$_.Name -eq "Apache2.4"}) {
            $true
        } else {
            $false
        }
    }
    GetScript = { 
        Get-Service | Where-Object {$_.Name -eq "Apache2.4"}
    }
}

除了httpd.exe抛出DSC正在解释为错误的一些信息性消息之外,哪个“有效”:

This event indicates that a non-terminating error was thrown when DSCEngine was executing Set-TargetResource on MSFT_ScriptResource DSC resource. FullyQualifiedErrorId is NativeCommandErrorMessage. ErrorMessage is The 'Apache2.4' service is successfully installed.

有人可以推荐一种通过DSC安装Apache的方法吗?我将尝试在等待答案时使用try / catch块来转移此问题。

这是try / catch的代码。仍然是同样的错误:

Script InstallApache
{
    DependsOn = @("[Environment]Apache","[File]httpdConf")
    SetScript = {
        try{
            $Quiet = Invoke-Expression "d:\Apache24\bin\httpd.exe -k install"
        } catch {
            $true
        }
    }
    TestScript = {
        if(Get-Service | Where-Object {$_.Name -eq "Apache2.4"}) {
            $true
        } else {
            $false
        }
    }
    GetScript = { 
        Get-Service | Where-Object {$_.Name -eq "Apache2.4"}
    }
}

0 个答案:

没有答案