powershell - 如何将外部DSC模块导入PowerShell环境?

标签 powershell dsc

我有 Windows 10/Windows 2016 TP5 (PowerShell 5.1)。我想在我的配置中使用来自 GitHub 的 PowerShell DSC 模块,例如 xTimeZone .

我该怎么做才能使用 Import-DSCResource 引用它?

这是我第一次尝试添加除基于安装程序的产品之外的模块,也许我遗漏了一些明显的东西。是否有像 pipgem 那样基于 git clone 的程序?


我验证了 $env:PSModulePath 的值,它定义了两个目录:C:\Users\techraf\Documents\WindowsPowerShell\Modules;C:\Windows\system32\W indowsPowerShell\v1.0\Modules

我复制了xTimeZone的内容目录到C:\Users\techraf\Documents\WindowsPowerShell\Modules

PowerShell 看不到它。尝试在存储库的不同级别进行复制 - 全部失败。


运行 example 时我得到的脚本:

At line:12 char:4
+    Import-DSCResource -ModuleName xTimeZone
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Could not find the module 'xTimeZone'.
At line:16 char:9
+         xTimeZone TimeZoneExample
+         ~~~~~~~~~
Undefined DSC resource 'xTimeZone'. Use Import-DSCResource to import the resource.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ModuleNotFoundDuringParse

根据答案的建议,将模块从 C:\Users\techraf\Documents\WindowsPowerShell\Modules 移动到 C:\Windows\system32\W 后 indowsPowerShell\v1.0\Modules 第一个错误消失了,我只得到:

At line:16 char:9
+         xTimeZone TimeZoneExample
+         ~~~~~~~~~
Undefined DSC resource 'xTimeZone'. Use Import-DSCResource to import the resource.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ResourceNotDefined

最佳答案

关于 DSC 资源

DSC 资源本质上是专门的 PowerShell 模块。资源模块必须导出 Get、Set 和 Test-TargetResource 命令(PowerShell 5 提供另一个选项,但超出了 xTimeZone 上下文的范围)。

资源始终按以下结构部署:

<Modules>\<ModuleName>\DSCResources\<ResourceName>

Get-DscResource 在 DSCResources 文件夹中查找“模块”,作为现有模块的子文件夹。如果它看不到资源,您就无法使用它。

x时区

xTimeZone 包含:

  1. 一个 PowerShell 模块,仅包含 list 和一些示例。
  2. 名为 xTimeZone 的 DSC 资源。

除非正确部署,否则 Windows PowerShell 无法找到该资源。这些说明适用于下载模块。

  1. 选择主分支并从以下位置下载存储库的 zip:https://github.com/PowerShell/xTimeZone/tree/master
  2. 将 zip 文件解压到文件夹中。
  3. xTimeZone-master 重命名为 xTimeZone
  4. xTimeZone复制到C:\Program Files\WindowsPowerShell\Modules
  5. 验证您可以发现 DSCResource:

    Get-DscResource
    

如果由于某种原因,您在使用 Program Files\Modules 时遇到问题,您可以使用:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\

但是,由于该区域是操作系统文件的一部分,因此优先级较低。

配置

发现资源后,您需要将其导入到您希望使用它的每个配置元素中。

Configuration SomeConfiguration {
    Import-DscResource -ModuleName xTimeZone

    # Nodes statement follows
}

关于powershell - 如何将外部DSC模块导入PowerShell环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38812685/

相关文章:

C# 将数组传递给 powershell 脚本

PowerShell 所需状态配置 -WhatIf

powershell - 用于配置 IIS 应用程序池的 DSC

arrays - 如何将默认参数设置为数组

powershell - Power Shell:在内存中使用变量/字符串生成器的最有效方法是什么?

powershell - 在Powershell中提取两个特殊字符之间的字符串

powershell - 调用 ASCmd 以在 PowerShell 中返回错误/警告

powershell - URL 重写器 Powershell DSC

powershell - 如何使用 DSC 在 Azure VM 上安装存储在 Azure 文件共享中的 exe 文件?