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