PowerShell DSC 脚本资源失败

PowerShell DSC Script Resources Fail

这似乎无法正常工作

我有以下脚本资源。

 Script RDGatewayCreateRAP
        {
            SetScript = {
                $localhost = $using:localhost
                $forest = $using:forest
                Import-Module RemoteDesktopServices            
                New-Item -Path 'RDS:/GatewayServer/RAP' -Name 'RAP' -UserGroups "Domain Users@$forest" -ComputerGroupType 1 -ComputerGroup "Domain Computers@$forest"
            }
            GetScript = {
                return @{
                    GetScript = $GetScript
                    SetScript = $SetScript
                    TestScript = $TestScript
                    Result = ""
                }

            }
            TestScript = {
                Import-Module RemoteDesktopServices
                return [boolean](Get-ChildItem 'GatewayServer/RAP')
            }
            DependsOn = "[Script]RDDeploymentGatewayConfiguration"
        }

使用 Start-DscConfiguration -Wait -Verbose 对该脚本进行配置,在

处出错
VERBOSE: [WIN-EEK1CL8BED9]:                            [[Script]RDGatewayCreateRAP::[cRdsServer]RdsServer] Importing cmdlet '
Convert-License'.
Cannot find path 'C:\Windows\system32\GatewayServer\RAP' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (C:\Windows\system32\GatewayServer\RAP:) [], CimException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost

这可能是因为 RDS: 路径未找到,因为导入模块 RemoteDesktopServices 未正常工作。也就是说,紧接在失败之上,我看到了来自 Import-Module RemoteDesktopServices 的详细日志记录。

如果我更改我的脚本资源,使 SetScript、GetScript 和 TestScript 都将 powershell 作为新进程调用,它就可以工作。

powershell -NoProfile -ExecutionPolicy Bypass -Command "$using:commandStoredAsAString"

如果我使用 Invoke-Command 或 Invoke-Expression,它会爆炸,所以看来 运行 一个单独的进程是关键。有没有办法让脚本资源在没有这种 hack 的情况下正常工作,或者它们只是 useless/poorly 实施?

问题出在 TestScript 中,它试图获取“Get-ChildItem 'GatewayServer/RAP'”。 Import-Module RemoteDesktopServices 工作正常。 如果要检查 gatewayServer\RAP 的存在,请将 TestScript 实现更改为 (Test-Path RDS:GatewayServer\RAP)。 脚本 RDGatewayCreateRAP { 设置脚本 = { $localhost = $using:localhost $森林= $使用:森林 导入模块 RemoteDesktopServices
#New-Item -Path 'RDS:/GatewayServer/RAP' -Name 'RAP' -UserGroups "Domain Users@$forest" -ComputerGroupType 1 -ComputerGroup "Domain Computers@$forest" } 获取脚本 = { return@{ 获取脚本 = $获取脚本 设置脚本 = $设置脚本 测试脚本 = $测试脚本 结果=“” }

        }
        TestScript = {
            Import-Module RemoteDesktopServices
            return (Test-Path RDS:GatewayServer\RAP)
        }
    }