培训
认证
Microsoft Certified: Dynamics 365 Field Service Functional Consultant Associate - Certifications
演示如何配置 Microsoft Dynamics 365 for Field Service 实现,以最大限度地提高可用于管理移动工作组的工具和功能。
适用于:Windows PowerShell 4.0 和 Windows PowerShell 5.0
DSC 配置是定义某一特殊类型函数的 PowerShell 脚本。 若要定义配置,你可使用 PowerShell 关键字 Configuration。
Configuration MyDscConfiguration {
Node "TEST-PC1" {
WindowsFeature MyFeatureInstance {
Ensure = 'Present'
Name = 'RSAT'
}
WindowsFeature My2ndFeatureInstance {
Ensure = 'Present'
Name = 'Bitlocker'
}
}
}
MyDscConfiguration
将脚本保存为 .ps1
文件。
配置脚本由以下部分组成:
MyDscConfiguration
。TEST-PC1
。
节点块可以接受多个计算机名称。备注
WindowsFeature DSC 资源仅在 Windows Server 计算机上可用。 对于具有客户端 OS(如 Windows 11)的计算机,需要改用 WindowsOptionalFeature。 有关详细信息,请参阅 PSDscResources 文档的“WindowsOptionalFeature”部分。
在配置块中,可执行在 PowerShell 函数中通常可执行的任何操作。 在前面的示例中,如果不想在配置中对目标计算机的名称进行硬编码,则可以为节点名称添加参数。
在此示例中,指定节点名称,具体方法为在编译配置时以 ComputerName 参数的形式传递节点名称。 名称默认为 localhost
。
Configuration MyDscConfiguration
{
param
(
[string[]]$ComputerName='localhost'
)
Node $ComputerName
{
WindowsFeature MyFeatureInstance
{
Ensure = 'Present'
Name = 'RSAT'
}
WindowsFeature My2ndFeatureInstance
{
Ensure = 'Present'
Name = 'Bitlocker'
}
}
}
MyDscConfiguration
节点块还可以接受多个计算机名称。 在上述示例中,可以使用 -ComputerName
参数,也可以将以逗号分隔的计算机列表直接传递到节点块。
MyDscConfiguration -ComputerName "localhost", "Server01"
从配置中向 Node 块指定计算机列表时,需要使用数组表示法。
Configuration MyDscConfiguration
{
Node @('localhost', 'Server01')
{
WindowsFeature MyFeatureInstance
{
Ensure = 'Present'
Name = 'RSAT'
}
WindowsFeature My2ndFeatureInstance
{
Ensure = 'Present'
Name = 'Bitlocker'
}
}
}
MyDscConfiguration
必须将其编译为 MOF 文档才能执行配置。 可通过调用配置(像调用 PowerShell 函数一样)来执行此操作。 此示例的最后一行仅包含配置名称,用于调用配置。
备注
函数必须在全局范围内(与其他任何 PowerShell 函数一样),才能调用配置。 可通过以下方式来实现此操作:对脚本执行“dot-source”操作,或者使用 F5 或单击 ISE 中的“运行脚本”按钮以运行配置脚本。 若要对脚本执行“dot-source”操作,请运行命令 . .\myConfig.ps1
,其中 myConfig.ps1
是包含配置的脚本文件的名称。
调用配置时,它会:
备注
MOF 文件包含目标节点的所有配置信息。 因此,确保其安全非常重要。 有关详细信息,请参阅保护 MOF 文件。
编译上述第一个配置会形成以下文件夹结构:
. .\MyDscConfiguration.ps1
MyDscConfiguration
Directory: C:\users\default\Documents\DSC Configurations\MyDscConfiguration
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/23/2015 4:32 PM 2842 localhost.mof
如果配置采用了参数(如示例 2 所述),则需在编译时提供该参数。 其形式如下:
. .\MyDscConfiguration.ps1
MyDscConfiguration -ComputerName 'MyTestNode'
Directory: C:\users\default\Documents\DSC Configurations\MyDscConfiguration
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/23/2015 4:32 PM 2842 MyTestNode.mof
如果运行了前面的示例,你可能注意到你已收到警告信息,提示你正在使用未显式导入的资源。 现在,DSC 附带 12 种资源作为 PSDesiredStateConfiguration 模块的一部分。
cmdlet - Get-DscResource,可用来确定哪些资源已安装在系统上并且可供 LCM 使用。
一旦这些模块已置于 $env:PSModulePath
中并由 Get-DscResource 正确识别,你仍需在配置中加载它们。
Import-DscResource 是仅可在配置块中识别的动态关键字,它不是 cmdlet。 Import-DscResource 支持两种参数:
有关使用 Import-DSCResource
的详细信息,请参阅使用 Import-DSCResource
DSC 资源需要在 PowerShell 4.0 中存储的位置存在差异。 有关详细信息,请参阅资源位置。
培训
认证
Microsoft Certified: Dynamics 365 Field Service Functional Consultant Associate - Certifications
演示如何配置 Microsoft Dynamics 365 for Field Service 实现,以最大限度地提高可用于管理移动工作组的工具和功能。