2016-04-13 72 views
2

這似乎無法正常工作PowerShell DSC腳本資源失敗

我有以下腳本資源。

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

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

使用啓動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-表達,它吹起來,所以它似乎是運行一個單獨的進程是關鍵。有沒有一種方法可以讓腳本資源在沒有這種黑客攻擊的情況下正常工作,還是僅僅是沒有用處/執行不力?

回答

2

問題是在TestScript中它試圖獲取'Get-ChildItem'GatewayServer/RAP''。導入模塊RemoteDesktopServices工作正常。 如果要檢查gatewayServer \ RAP是否存在將TestScript實現更改爲(Test-Path RDS:GatewayServer \ RAP)。 腳本RDGatewayCreateRAP { SetScript = {$ 本地主機= $使用:本地主機 $森林= $使用:森林 導入模塊RemoteDesktopServices
#新建項目-Path 'RDS:/ GatewayServer/RAP' 雜牌「RAP 「-UserGroups 」域用戶@ $森林「 -ComputerGroupType 1 -ComputerGroup 」域計算機@ $森林「 } getScript加入= { 返回@ { getScript加入= $ getScript加入 SetScript = $ SetScript TestScript = $ TestScript 結果= 「」 }

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