2015-08-26 63 views
2

如何訪問此處描述的內置DSC資源:https://technet.microsoft.com/en-us/library/dn282121.aspx?它們應該是內置的,但當我嘗試在配置中使用它們時出現錯誤。未找到內置DSC資源

我的配置如下:

configuration Windows8VM 
{ 
param 
(
    [Parameter(Mandatory = $true)] 
    [string] $ComputerName 
) 

Import-DSCResource -Name Package 

Node $ComputerName 
{ 
    File gitFolder 
    { 
     Ensure = "Present" 
     Type = "Directory" 
     DestinationPath = "C:\git" 
    } 

    Package gitSoftware 
    { 
     Ensure = "Present" 
     Name = "git" 
     ProductId = '' 
     Path = https://chocolatey.org/api/v2/ 
     Arguments = '/SILENT /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' 
    } 
    } 
} 

我得到的錯誤是:

At C:\win8vmconfig.ps1:9 char:5 
+  Import-DSCResource -Name Package 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Unable to load resource 'Package': Resource not found. 
At C:\win8vmconfig.ps1:20 char:9 
+   Package gitSoftware 
+   ~~~~~~~ 
Undefined DSC resource 'Package'. Use Import-DSCResource to import the  resource. 
    + CategoryInfo   : ParserError: (:) [], ParseException 
    + FullyQualifiedErrorId : DscResourcesNotFoundDuringParsing 

所以,這是完全不能定位的資源。這裏發生了什麼,以及我缺少訪問Microsoft記錄的內置DSC資源的步驟?

我正在使用WMF/PowerShell 5.0。

回答

1

您不需要使用Import-DscResource來使用內置資源。這實際上可能會把它扔掉。如果你註釋掉那條線,你還會得到第二個錯誤嗎?

你也說過你在使用WMF 5.你能說清哪個操作系統?在撰寫本文時,只有Windows 10支持PowerShell 5的生產就緒版本。

WMF 5 Production Preview設置爲即將發佈,但現在任何可安裝版本都使用實驗性功能。

+0

我已經試過這兩個獨立的操作系統,Windows 7和較新的Windows Server 2012 R2數據中心,因此它似乎獨立於操作系統。 我只在試圖解決原始錯誤時添加了「Import-DscResource」行, – learningKnight

+1

@learningKnight Windows 7和2012 R2都只能使用帶有實驗功能的DSC 5,因此可能這是一個錯誤。如果你刪除了'Import-DscResource'行,那麼你會得到原始錯誤,那是第二個錯誤? – briantist

+0

正確,我的評論顯然被切斷了,但是,第二個錯誤是原來的錯誤。 如果我運行'Get-DscResource',則只顯示文件。這似乎是我錯過了一個安裝步驟,但我無法找出需要安裝哪些內容才能訪問這些資源。 另一個奇怪的是,我在原帖中鏈接到的文檔資源是從2013年開始的,所以它引發了一些疑問,以瞭解這些功能對於Window 10來說是新的,但也許這與它有關,我會看看我是否可以設置Windows 10機器... – learningKnight

0

下面的例子是推薦的方法來做到這一點(雖然你的版本工程,並在我正在運行的WMF 5版本中給我一個警告)。

這裏是我提到的警告:

警告:配置「Windows8VM」加載一個或多個內置的資源,而無需顯式地導入相關的模塊。將Import-DscResource -ModuleName'PSDesiredStateConfiguration'添加到您的配置中以避免此消息。

configuration Windows8VM 
{ 
    param 
    (
     [Parameter(Mandatory = $true)] 
     [string] $ComputerName 
    ) 

    Import-DSCResource -ModuleName PSDesiredStateConfiguration 

    Node $ComputerName 
    { 
     File gitFolder 
     { 
      Ensure   = 'Present' 
      Type   = 'Directory' 
      DestinationPath = 'C:\git' 
     } 

     Package gitSoftware 
     { 
      Ensure = 'Present' 
      Name  = 'git' 
      ProductId = '' 
      Path  = 'https://chocolatey.org/api/v2/' 
      Arguments = '/SILENT /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' 
     } 
    } 
}