There seems to be something buggy with DSC and PowerShell Modules.
Resources that use the RemoteDesktop module randomly fail with the following error logged in DSC Operational event log
If I run the same configuration ten times on ten different machines it will fail about half the time. If I rerun it after it fails...it might fail again or it might succeed (meaning it won't run again)
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.
Sometimes instead of Get-RDRemoteApp
, it logs Get-RDServer
...even though I don't call Get-RDServer
anywhere in my script. It seems like there is something messed up with how DSC uses PowerShell sessions to invoke resources. If I change the class below to actually launch the required commands by starting a new powershell.exe process, it always works...
[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
}
}
Edit - If I use a ScriptBlock it consistently works:
$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