在前面的四个章节,笔者介绍了一些PowerShell基础的知识,包括PowerShell DSC的基本架构,DSC的资源以及配置格式,已经在PowerShell DSC 如何安装扩展的DSC Module和resource;还有一些基本的DSC辅助命令的使用。那么本节,咱们就安装一个Chrome插件的DSC配置文件为例子,看其如何生成一个mof文件。打开Powershell ISE或者一个文本编辑器,新建一个文件名为:InstallGoogleChrome.ps1的配置文件,放在c:\dsc 目录下。内容如下:
Configuration InstallGoogleChrome {
param (
[string[]]$MachineName = "localhost",
[Parameter(Mandatory)]$Language,
[Parameter(Mandatory)]$LocalPath
)
Import-DscResource -Module xChrome
Node $MachineName {
MSFT_xChrome chrome {
Language = $Language
LocalPath = $LocalPath
}
}
}
InstallGoogleChrome -MachineName (Get-Content -Path "c:\dsc\servers.txt") -Language "en" –LocalPath "C:\Windows\Temp\GoogleChromeStandaloneEnterprise.msi"
注意:在ps1文件的最后一行,一定要把Configuration后的名字加上, InstallGoogleChrome,后面的参数是可选的。否则其将会生成不了MOF文件。
pserver51w2k12
dscc01-51w2k12
dscc02-51w2008
然后在PowerShell 命令行中输入下面的命令:
PS C:\dsc> .\InstallGoogleChrome
WARNING: The configuration 'InstallGoogleChrome' is loading one or more built-in resources without explicitly importing
associated modules. Add Import-DscResource –ModuleName 'PSDesiredStateConfiguration' to your configuration to avoid
this message.
Directory: C:\dsc\InstallGoogleChrome
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/23/2017 8:00 AM 3998 pserver51w2k12.mof
-a---- 1/23/2017 8:00 AM 3998 dscc01-51w2k12.mof
-a---- 1/23/2017 8:00 AM 3998 dscc02-51w2008.mof
Configuration InstallGoogleChrome {
param (
[string[]]$MachineName = "localhost",
[Parameter(Mandatory)]$Language,
[Parameter(Mandatory)]$LocalPath
)
Import-DscResource -Module xChrome
Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
Node $MachineName {
MSFT_xChrome chrome {
Language = $Language
LocalPath = $LocalPath
}
}
}
InstallGoogleChrome -MachineName (Get-Content -Path "c:\dsc\servers.txt") -Language "en" –LocalPath "C:\Windows\Temp\GoogleChromeStandaloneEnterprise.msi"
非常好! 警告信息消失,重新执行一遍。
PS C:\dsc> .\InstallGoogleChrome
Directory: C:\dsc\InstallGoogleChrome
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/23/2017 8:01 AM 3998 pserver51w2k12.mof
-a---- 1/23/2017 8:01 AM 3998 dscc01-51w2k12.mof
-a---- 1/23/2017 8:01 AM 3998 dscc02-51w2008.mof
打开其中的一个MOF文件查看。
/*
@TargetNode='dscc01-51w2k12'
@GeneratedBy=Admin
@GenerationDate=01/23/2017 08:01:31
@GenerationHost=PULLSERVERWMF51
*/
instance of MSFT_xRemoteFile as $MSFT_xRemoteFile1ref
{
ResourceID = "[xRemoteFile]Downloader::[MSFT_xChrome]chrome";
Uri = "https://dl.google.com/tag/s/appguid={8A69D345-D564-463C-AFF1-A69D9E530F96}&iid={00000000-0000-0000-0000-000000000000}&lang=en&browser=3&usagestats=0&appname=Google%2520Chrome&needsadmin=prefers/edgedl/chrome/install/GoogleChromeStandaloneEnterprise.msi";
DestinationPath = "C:\\Windows\\Temp\\GoogleChromeStandaloneEnterprise.msi";
ModuleName = "xPSDesiredStateConfiguration";
SourceInfo = "C:\\Program Files\\WindowsPowerShell\\Modules\\xChrome\\1.1.0.0\\DSCResources\\MSFT_xChrome\\MSFT_xChrome.schema.psm1::15::5::xRemoteFile";
ModuleVersion = "5.1.0.0";
ConfigurationName = "InstallGoogleChrome";
};
instance of MSFT_PackageResource as $MSFT_PackageResource1ref
{
ResourceID = "[Package]Installer::[MSFT_xChrome]chrome";
Path = "C:\\Windows\\Temp\\GoogleChromeStandaloneEnterprise.msi";
Ensure = "Present";
ProductId = "";
SourceInfo = "C:\\Program Files\\WindowsPowerShell\\Modules\\xChrome\\1.1.0.0\\DSCResources\\MSFT_xChrome\\MSFT_xChrome.schema.psm1::21::5::Package";
Name = "Google Chrome";
ModuleName = "PSDesiredStateConfiguration";
ModuleVersion = "1.0";
DependsOn = {
"[xRemoteFile]Downloader::[MSFT_xChrome]chrome"};
ConfigurationName = "InstallGoogleChrome";
};
instance of OMI_ConfigurationDocument
{
Version="2.0.0";
MinimumCompatibleVersion = "1.0.0";
CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};
Author="Admin";
GenerationDate="01/23/2017 08:01:31";
GenerationHost="PULLSERVERWMF51";
Name="InstallGoogleChrome";
};
末了,给大家分享一个小小的经验,当我们在写DSC Configuration的文件的时候,其中会用到一些DSC的资源(Resource),那么我们如何知道这些DSC的资源的包含哪些输入参数呢?可以通
Get-DscResource -Name Registry -Syntax
来查看DSC的资源的输入格式。比如,下面的例子:
PS C:\dsc> Get-DscResource -Name MSFT_xChrome -Syntax
MSFT_xChrome [String] #ResourceName
{
[DependsOn = [String[]]]
[PsDscRunAsCredential = [PSCredential]]
[Language = [String]]
[LocalPath = [String]]
}
我们知道,对于MSFT_xChrome的Resource,支持哪些输入参数。