使用 DSC 配置本地用户帐户
我假设您对 DSC 的工作原理有一些基本的了解。如果没有,请转到 PowerShell.org 的 Public OneDrive 文件夹并获取免费 DSC 电子书的副本。
DSC 附带了用户帐户的提供商资源。
由于账户密码的问题,官方的立场是使用证书来处理对密码的加密。您可以在 PowerShell 团队博客上阅读相关内容。但这超出了我现在想要处理的范围,而且我信任本地测试网络的安全性,因此我的配置会将密码以纯文本形式存储在生成的 MOF 中。我想我应该给你看我想出的剧本。
#requires -version 4.0
#requires -RunasAdministrator
param(
[Parameter(Position=0,Mandatory)]
[ValidatePattern("^CHI")]
[string[]]$Computername
)
Configuration LocalUserAccounts {
Param(
[Parameter(Position=0,Mandatory)]
[ValidatePattern("^CHI")]
[string[]]$Computername,
[Parameter(Position=1,Mandatory)]
[PScredential]$Password
)
Node $Computername {
User LocalAdmin {
UserName="localadmin"
Description="Local administrator account"
Disabled=$False
Ensure="Present"
Password=$Password
}
#add the account to the Administrators group
Group Administrators {
GroupName="Administrators"
DependsOn="[User]LocalAdmin"
MembersToInclude="localadmin"
}
} #node
} #end configuration
#create config data to allow plain text passwords
$ConfigData=@{AllNodes=$Null}
#initialize an array for node information
$nodes=@()
foreach ($computer in $computername) {
#write-host "Adding $computer" -foreground green
#define a hashtable for each computername and add to the nodes array
$nodes+=@{
NodeName = "$computer"
PSDscAllowPlainTextPassword=$true
}
}
#add the nodes to AllNodes
$ConfigData.AllNodes = $nodes
#you will be prompted to enter a credential
Write-Host "Enter the credential for localadmin" -foregroundcolor green
#create the configurations
localuseraccounts $computername -configurationdata $configdata -OutputPath c:\scripts\LocalUserAccounts
该脚本旨在为我的 Globomantics 域中的计算机定义一组以“CHI”开头的 MOF。该脚本采用一组计算机名。从那里它定义配置,定义必要的配置数据以允许纯文本密码,然后执行配置。该配置还有一个组资源,用于将帐户添加到本地管理员组。请注意组配置中的 DependsOn 设置。这可确保在将帐户添加到组之前先对其进行设置。
为了创建配置,我运行指定计算机名称的脚本。
PowerShell 将提示我输入凭据。完成后,我会在 C:\Scripts\LocalUserAccounts 下为每台计算机留下一个 MOF 文件,因为我指定了输出路径。准备好后,我可以将配置推送到服务器:
Start-DSCConfiguration -path c:\scripts\localuseraccounts就是这样!我可以在远程会话中使用 NET USER 命令进行验证。
DSC 承诺以积极的方式改变 IT 专业人员完成工作的方式!



