PowerShell DSC脚本资源失败

时间:2016-04-13 18:37:11

标签: powershell dsc powershell-v5.0

这似乎无法正常工作

我有以下脚本资源。

 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:路径,因为Import-Module RemoteDesktopServices无法正常工作。也就是说,在失败之后,我看到了Import-Module RemoteDesktopServices的详细日志记录。

如果我更改了我的脚本资源,那么SetScript,GetScript和TestScript都会将powershell作为一个新进程调用,它可以正常工作。

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

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

1 个答案:

答案 0 :(得分:2)

问题出现在TestScript中,它试图获取Get-ChildItem' GatewayServer / RAP'。 Import-Module RemoteDesktopServices工作正常。 如果要检查是否存在gatewayServer \ RAP更改TestScript实现(Test-Path RDS:GatewayServer \ RAP)。 脚本RDGatewayCreateRAP         {             SetScript = {                 $ localhost = $ using:localhost                 $ forest = $ using:forest                 导入模块RemoteDesktopServices
                #New-Item -Path' RDS:/ GatewayServer / RAP' -Name' RAP' -UserGroups"域用户@ $ forest" -ComputerGroupType 1 -ComputerGroup" Domain Computers @ $ forest"             }             GetScript = {                 return @ {                     GetScript = $ GetScript                     SetScript = $ SetScript                     TestScript = $ TestScript                     结果=""                 }

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