我有 Windows 10/Windows 2016 TP5 (PowerShell 5.1)。我想在我的配置中使用来自 GitHub 的 PowerShell DSC 模块,例如 xTimeZone .
我该怎么做才能使用 Import-DSCResource
引用它?
这是我第一次尝试添加除基于安装程序的产品之外的模块,也许我遗漏了一些明显的东西。是否有像 pip
或 gem
那样基于 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 包含:
- 一个 PowerShell 模块,仅包含 list 和一些示例。
- 名为 xTimeZone 的 DSC 资源。
除非正确部署,否则 Windows PowerShell 无法找到该资源。这些说明适用于下载模块。
- 选择主分支并从以下位置下载存储库的 zip:https://github.com/PowerShell/xTimeZone/tree/master
- 将 zip 文件解压到文件夹中。
- 将
xTimeZone-master
重命名为xTimeZone
。 - 将
xTimeZone
复制到C:\Program Files\WindowsPowerShell\Modules
验证您可以发现 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/