[5] Window PowerShell DSC 学习系列----如何生成一个DSC MOF文件?

在前面的四个章节,笔者介绍了一些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文件。


其中,引用的到的c:\dsc\servers.txt服务器列表如下:

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

注意,上面会弹出一个警告信息,然后修改InstallGoogleChrome.ps1文件,显式加上Import-DscResource –ModuleName 'PSDesiredStateConfiguration' 这一行。

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文件已经生成。
打开其中的一个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";


                    };

恭喜你!!! MoF文件已经成功生成!!!

末了,给大家分享一个小小的经验,当我们在写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,支持哪些输入参数。





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值