DSC不可预测的导入模块

时间:2016-05-04 04:43:49

标签: powershell dsc powershell-v5.0

DSC和PowerShell模块似乎有些问题。

使用RemoteDesktop模块的资源随机失败,并在DSC操作事件日志中记录以下错误

如果我在十台不同的机器上运行十次相同的配置,它将失败大约一半的时间。如果我在失败后重新运行它......它可能会再次失败或者它可能会成功(意味着它不会再次运行)

The term 'Get-RDRemoteApp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

有时代替Get-RDRemoteApp,它会记录Get-RDServer ...即使我没有在我的脚本中的任何地方调用Get-RDServer。似乎有些东西搞砸了DSC如何使用PowerShell会话来调用资源。如果我通过启动一个新的powershell.exe进程更改下面的类来实际启动所需的命令,它总是有效...

[DSCResource()]
class zRemoteApp {
    $ErrorActionPreference = "Stop"

    [DscProperty(Key)]
    [string]$Name = "MyApp"

    [DscProperty()]
    [string]$Path

    [bool]$RemoteAppExists

    [zRemoteApp]Get() {
        Import-Module RemoteDesktop

        $this.RemoteAppExists = [boolean](Get-RDRemoteApp -Alias $this.Name)

        Return $this
    }

    [void]Set() {
        Import-Module RemoteDesktop
        $this.Get()

        Get-RDSessionCollection |% {
            New-RDRemoteApp -CollectionName $_.CollectionName -Alias $this.Name -DisplayName $this.Name -FilePath $Path
        }
    }

    [bool]Test() {    
        $this.Get()
        Return $this.RemoteAppExists
    }
}

编辑 - 如果我使用ScriptBlock,它一直有效:

    $job = Start-Job -ScriptBlock {
        param($Name)
        Import-Module RemoteDesktop
        [boolean](Get-RDRemoteApp -Alias $Name)
    } -ArgumentList $this.Name

    $this.RemoteAppExists = $job | Wait-Job | Receive-Job

    Remove-Job $job

    Return $this

0 个答案:

没有答案