如何将外部 DSC 模块导入 PowerShell 环境?
How to import an external DSC module into PowerShell environment?
我有 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 文件夹中查找 "modules",作为现有模块的子文件夹。如果它看不到资源,则无法使用它。
xTimeZone
xTimeZone 包括:
- 一个 PowerShell 模块,仅包含清单和一些示例。
- 名为 xTimeZone 的 DSC 资源。
Windows 除非正确部署,否则 PowerShell 找不到资源。这些说明适用于下载模块。
- Select master 分支并从以下位置下载存储库的 zip:https://github.com/PowerShell/xTimeZone/tree/master
- 将 zip 文件解压到一个文件夹。
- 将
xTimeZone-master
重命名为 xTimeZone
。
- 复制
xTimeZone
到C:\Program Files\WindowsPowerShell\Modules
确认您可以发现 DSCResource:
Get-DscResource
如果由于某种原因,您在使用程序 Files\Modules 时遇到问题,您可以使用:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
但是,由于此区域是操作系统文件的一部分,因此优先级较低。
配置
发现资源后,您需要将其导入到您希望使用它的每个配置元素中。
Configuration SomeConfiguration {
Import-DscResource -ModuleName xTimeZone
# Nodes statement follows
}
*不想回答,只是想提供额外的反馈,但没有足够的 stack-street-cred 来添加评论...
根据您尝试使用的 DSC,有时您必须从 "source" 文件夹(包含上述 "modules" 和 "DSCResources" 子文件夹)复制内容到您尝试使用的 dscname 的根目录。在我的实例中,我使用的是 SqlServerDsc(下面的url)并且我不是很精通所以花了一段时间才弄清楚发生了什么。
https://github.com/dsccommunity/SqlServerDsc
我最初按照 github 上提供的说明将 zip 解压到 PS 可访问目录,但无法通过 get-dscresource 访问任何新内容。在看到上面的海报概述的语法后,我尝试手动将 DSC 源文件夹的内容复制到 modules/SqlServerDsc 根目录,然后一切都恢复了。
我所需要的只是刷新我的资源,因为出现了一些新方法并且不记得我以前是怎么做的......这绝对不同。
我有 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 文件夹中查找 "modules",作为现有模块的子文件夹。如果它看不到资源,则无法使用它。
xTimeZone
xTimeZone 包括:
- 一个 PowerShell 模块,仅包含清单和一些示例。
- 名为 xTimeZone 的 DSC 资源。
Windows 除非正确部署,否则 PowerShell 找不到资源。这些说明适用于下载模块。
- Select master 分支并从以下位置下载存储库的 zip:https://github.com/PowerShell/xTimeZone/tree/master
- 将 zip 文件解压到一个文件夹。
- 将
xTimeZone-master
重命名为xTimeZone
。 - 复制
xTimeZone
到C:\Program Files\WindowsPowerShell\Modules
确认您可以发现 DSCResource:
Get-DscResource
如果由于某种原因,您在使用程序 Files\Modules 时遇到问题,您可以使用:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
但是,由于此区域是操作系统文件的一部分,因此优先级较低。
配置
发现资源后,您需要将其导入到您希望使用它的每个配置元素中。
Configuration SomeConfiguration {
Import-DscResource -ModuleName xTimeZone
# Nodes statement follows
}
*不想回答,只是想提供额外的反馈,但没有足够的 stack-street-cred 来添加评论...
根据您尝试使用的 DSC,有时您必须从 "source" 文件夹(包含上述 "modules" 和 "DSCResources" 子文件夹)复制内容到您尝试使用的 dscname 的根目录。在我的实例中,我使用的是 SqlServerDsc(下面的url)并且我不是很精通所以花了一段时间才弄清楚发生了什么。
https://github.com/dsccommunity/SqlServerDsc
我最初按照 github 上提供的说明将 zip 解压到 PS 可访问目录,但无法通过 get-dscresource 访问任何新内容。在看到上面的海报概述的语法后,我尝试手动将 DSC 源文件夹的内容复制到 modules/SqlServerDsc 根目录,然后一切都恢复了。
我所需要的只是刷新我的资源,因为出现了一些新方法并且不记得我以前是怎么做的......这绝对不同。