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

标签: PowerShellDSCDevOpsPowershell
1105人阅读 评论(0) 收藏 举报
分类:

在这节中,笔者给大家分享几个使用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


未完待续。。。。。。。

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:384971次
    • 积分:6326
    • 等级:
    • 排名:第4065名
    • 原创:227篇
    • 转载:0篇
    • 译文:5篇
    • 评论:157条
    博客专栏