使用Powershell修改本地安全策略



我使用的是Windows Server 2012。

我可以做到:

在"管理工具"文件夹中,双击"本地安全策略"图标,展开"帐户策略",然后单击"密码策略"。

在右侧窗格中双击"密码必须满足复杂性要求",并将其设置为"禁用"。单击"确定"保存您的策略更改。

如何使用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:windowssecuritylocal.sdb /cfg c:secpol.cfg /areas SECURITYPOLICY
rm -force c:secpol.cfg -confirm:$false

我决定写几个函数来简化这个过程。

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 "rn" | ?{$_.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:windowssecuritylocal.sdb /cfg "$CfgFile" /areas SECURITYPOLICY
}

$SecPool = Parse-SecPol -CfgFile C:testTest.cgf
$SecPool.'System Access'.PasswordComplexity = 1
$SecPool.'System Access'.MinimumPasswordLength = 8
$SecPool.'System Access'.MaximumPasswordAge = 60
Set-SecPol -Object $SecPool -CfgFile C:TestTest.cfg

我使用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:SYSTEMCurrentControlSetControlLsa"
$Name = "LimitBlankPasswordUse"
$value = "0"
New-ItemProperty -Path $registryPath -Name $name -Value $value ` -PropertyType DWORD -Force | Out-Null

如果尚未在管理模式中打开,此脚本将自动以管理员身份运行

我也试过拉夫写的剧本。我有2.0版本,但我只能使用4.0版本的

最新更新