2

如何访问此处描述的内置 DSC 资源:https ://technet.microsoft.com/en-us/library/dn282121.aspx ?它们应该是内置的,但是当我尝试在配置中使用它们时出现错误。

我的配置如下:

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。

4

2 回答 2

1

您不需要Import-DscResource使用内置资源。那可能实际上是把它扔掉了。如果您注释掉该行,您还会收到第二个错误吗?

您还说您使用的是 WMF 5。您能澄清一下是哪个操作系统吗?在撰写本文时,只有 Windows 10 具有受支持的 PowerShell 5 生产就绪版本。

WMF 5 Production Preview 即将发布,但目前任何可安装版本都在使用实验性功能。

于 2015-08-26T22:24:25.200 回答
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"'
        }
    }
}
于 2016-05-13T05:53:06.277 回答