使用Powershell修改本地安全策略
我使用Windows Server 2012 我可以这样做: 在“管理工具”文件夹中,双击“本地安全策略”图标,展开“帐户策略”,然后单击“密码策略” 在右侧窗格中,双击“密码必须满足复杂性要求”并将其设置为“禁用”。单击“确定”保存策略更改使用Powershell修改本地安全策略,powershell,local-security-policy,Powershell,Local Security Policy,我使用Windows Server 2012 我可以这样做: 在“管理工具”文件夹中,双击“本地安全策略”图标,展开“帐户策略”,然后单击“密码策略” 在右侧窗格中,双击“密码必须满足复杂性要求”并将其设置为“禁用”。单击“确定”保存策略更改 如何使用Powershell以编程方式完成此操作?根据@Kayasax的回答,没有纯粹的Powershell方法,您必须将secedit包装到Powershell中 secedit /export /cfg c:\secpol.cfg (gc C:\sec
如何使用Powershell以编程方式完成此操作?根据@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
我使用Windows7
我已使用以下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
如果尚未在管理模式下打开,则此脚本将自动作为管理运行
我也试过英国皇家空军写的剧本。我有2.0版,但我只是让它与4.0版一起使用,我决定编写几个函数,使这个过程更简单
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
函数解析SecPol($CfgFile){
secedit/export/cfg“$CfgFile”|输出空值
$obj=新对象psobject
$index=0
$contents=获取内容$CfgFile-原始
[regex]::匹配($contents,”(?我不知道如何使用PS,但您仍然可以使用secedit.exe。请参阅添加的有用示例。需要注意的一点是,导出策略会违反系统安全性,因为它会暴露策略,并将文件放在引导卷的根目录c:\secpol.cfg中,这很容易在安全性方面滥用。因此,我不会在生产环境中使用此逐字记录。)nEnvironment-而是使用$env:userdata位置存储临时文件并解析命令行实用程序“${env:appdata}\secpol.cfg”的文件名“疯狂,丑陋,但它可以工作。看起来是与组策略交互的唯一简单方法。GPOs API是用C++编写的。请注意。这更改了GPO最初未设置的其他安全设置,例如powershell执行策略。在这一行上获取”参数不正确““secedit/configure/db c:\windows\security\local.sdb/cfg c:\secpol.cfg/areas SECURITYPOLICY”