[12]Windows PowerShell DSC学习系列---PowerShell DSC的几个例子

在这节中,笔者给大家分享几个使用PowerShell DSC的例子,并且会持续更新中。。。。。

@修改命令行窗口的背景颜色

把命令行的背景色变成白色

Configuration ChangeCmdBackGroundColor    
{
    Import-DscResource -ModuleName PSDesiredStateConfiguration


    Node $AllNodes.NodeName
    {
        Registry CmdPath
        {
            Key                  = 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor'
            ValueName            = 'DefaultColor'
            ValueData            = 'F0'
            ValueType            = 'DWORD'
            Ensure               = 'Present'
            Force                = $true
            Hex                  = $true
            PsDscRunAsCredential = Get-Credential
        }
    }                   
}


$configData = @{
    AllNodes = @(
        @{
            NodeName             = 'localhost';
            PSDscAllowDomainUser = $true
            PSDscAllowPlainTextPassword = $true
        }
    )
}


ChangeCmdBackGroundColor -ConfigurationData $configData
Start-DscConfiguration -path .\ChangeCmdBackGroundColor -wait -Verbose -Force


@解压一个Zip文件

configuration  testzip{
  Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
  Archive ArchiveExample {
    Ensure = "Present"  # You can also set Ensure to "Absent"
    Path = "d:\DSC\22222.zip"
    Destination = "d:\DSC\dd"
  }


}
testzip 
Start-DscConfiguration -path d:\DSC\testZip -wait -Verbose -Force


@安装Chrome浏览器

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 "d:\dsc\servers.txt") -Language "en" –LocalPath "C:\Windows\Temp\GoogleChromeStandaloneEnterprise.msi"  

@修改LCM的Debug模式

[DscLocalConfigurationManager()]
Configuration LCMDebugMode{
  Settings{
     DebugMode='ForceModuleImport'
  }


}
LCMDebugMode
Set-DscLocalConfigurationManager -Path .\LCMDebugMode -Force -Verbose

@对于引入的Module制定版本

configuration VersionTest
{
    Import-DscResource -ModuleName xFailOverCluster -ModuleVersion 1.5.0.0


    Node 'localhost'
    {
       xCluster ClusterTest
       {
            Name                          = 'TestCluster'
            StaticIPAddress               = '10.0.0.3'
            DomainAdministratorCredential = Get-Credential
        }
     }
}
VersionTest

@通过数据配置文件(Configuration Data)动态生成MOF文件

#Prompt user for their credentials
#credentials will be unencrypted in the MOF
$promptedCreds = get-credential -Message "Please enter your credentials to generate a DSC MOF:"
# Store passwords in plaintext, in the document itself
# will also be stored in plaintext in the mof
$password = "ThisIsAPlaintextPassword" | ConvertTo-SecureString -asPlainText -Force
$username = "User1"
[PSCredential] $credential = New-Object System.Management.Automation.PSCredential($username,$password)
# DSC requires explicit confirmation before storing passwords insecurely
$ConfigurationData = @{
    AllNodes = @(
        @{
            # The "*" means "all nodes named in ConfigData" so we don't have to repeat ourselves
            NodeName="*"
            PSDscAllowPlainTextPassword = $true
        },
        #however, each node still needs to be explicitly defined for "*" to have meaning
        @{
            NodeName = "TestMachine1"
        },
        #we can also use a property to define node-specific passwords, although this is no more secure
        @{
            NodeName = "TestMachine2";
            UserName = "User2"
            LocalPassword = "ThisIsYetAnotherPlaintextPassword"
        }
        )
}
configuration unencryptedPasswordDemo
{
    Node "TestMachine1"
    {
        # We use the plaintext password to generate a new account
        User User1
        {
            UserName = $username
            Password = $credential
            Description = "local account"
            Ensure = "Present"
            Disabled = $false
            PasswordNeverExpires = $true
            PasswordChangeRequired = $false
        }
        # We use the prompted password to add this account to the local admins group
        Group addToAdmin
        {
            # Ensure the user exists before we add the user to a group
            DependsOn = "[User]User1"
            Credential = $promptedCreds
            GroupName = "Administrators"
            Ensure = "Present"
            MembersToInclude = "User1"
        }
    }
    Node "TestMachine2"
    {
        # Now we'll use a node-specific password to this machine
        $password = $Node.LocalPassword | ConvertTo-SecureString -asPlainText -Force
        $username = $node.UserName
        [PSCredential] $nodeCred = New-Object System.Management.Automation.PSCredential($username,$password)
        User User2
        {
            UserName = $username
            Password = $nodeCred
            Description = "local account"
            Ensure = "Present"
            Disabled = $false
            PasswordNeverExpires = $true
            PasswordChangeRequired = $false
        }
        Group addToAdmin
        {
            Credential = $domain
            GroupName = "Administrators"
            DependsOn = "[User]User2"
            Ensure = "Present"
            MembersToInclude = "User2"
        }
    }
}
unencryptedPasswordDemo -ConfigurationData $ConfigurationData


未完待续。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值