我使用Windows Server 2012。
我可以这样做:
在“管理工具”文件夹中,双击“本地安全策略”图标,展开“帐户策略”,然后单击“密码策略”。
在右窗格中,双击“密码必须满足复杂性要求”并将其设置为“已禁用”。单击确定以保存策略更改。
如何使用Powershell以编程方式完成此任务?
发布于 2014-04-24 17:38:16
根据@Kayasax的回答,没有纯粹的Powershell方法可以做到这一点,你必须将secedit封装到powershell中。
secedit /export /cfg c:\secpol.cfg
(gc C:\secpol.cfg).replace("PasswordComplexity = 1", "PasswordComplexity = 0") | Out-File C:\secpol.cfg
secedit /configure /db c:\windows\security\local.sdb /cfg c:\secpol.cfg /areas SECURITYPOLICY
rm -force c:\secpol.cfg -confirm:$false发布于 2019-04-21 01:38:33
我决定写几个函数来简化这个过程。
Parse-SecPol:将本地安全策略转换为PsObject。您可以查看所有属性并对对象进行更改。
Set-SecPol:将Parse-SecPol对象转换回配置文件,并将其导入到本地安全策略中。
下面是它的用法示例:
Function Parse-SecPol($CfgFile){
secedit /export /cfg "$CfgFile" | out-null
$obj = New-Object psobject
$index = 0
$contents = Get-Content $CfgFile -raw
[regex]::Matches($contents,"(?<=\[)(.*)(?=\])") | %{
$title = $_
[regex]::Matches($contents,"(?<=\]).*?((?=\[)|(\Z))", [System.Text.RegularExpressions.RegexOptions]::Singleline)[$index] | %{
$section = new-object psobject
$_.value -split "\r\n" | ?{$_.length -gt 0} | %{
$value = [regex]::Match($_,"(?<=\=).*").value
$name = [regex]::Match($_,".*(?=\=)").value
$section | add-member -MemberType NoteProperty -Name $name.tostring().trim() -Value $value.tostring().trim() -ErrorAction SilentlyContinue | out-null
}
$obj | Add-Member -MemberType NoteProperty -Name $title -Value $section
}
$index += 1
}
return $obj
}
Function Set-SecPol($Object, $CfgFile){
$SecPool.psobject.Properties.GetEnumerator() | %{
"[$($_.Name)]"
$_.Value | %{
$_.psobject.Properties.GetEnumerator() | %{
"$($_.Name)=$($_.Value)"
}
}
} | out-file $CfgFile -ErrorAction Stop
secedit /configure /db c:\windows\security\local.sdb /cfg "$CfgFile" /areas SECURITYPOLICY
}
$SecPool = Parse-SecPol -CfgFile C:\test\Test.cgf
$SecPool.'System Access'.PasswordComplexity = 1
$SecPool.'System Access'.MinimumPasswordLength = 8
$SecPool.'System Access'.MaximumPasswordAge = 60
Set-SecPol -Object $SecPool -CfgFile C:\Test\Test.cfg发布于 2015-11-13 17:33:06
我使用Windows 7
我已经使用下面的powershell脚本解决了这个问题
$name = $PSScriptRoot + "\" + $MyInvocation.MyCommand.Name
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$name`"" -Verb RunAs; exit }
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa"
$Name = "LimitBlankPasswordUse"
$value = "0"
New-ItemProperty -Path $registryPath -Name $name -Value $value ` -PropertyType DWORD -Force | Out-Null此脚本将自动以管理员身份运行,如果尚未在管理员模式下打开
我还尝试了Raf编写的脚本。我有2.0版本,但我只让它与4.0版本一起工作
https://stackoverflow.com/questions/23260656
复制相似问题