【问题标题】:Enforce Password complexity on Windows using Powershell使用 Powershell 在 Windows 上实施密码复杂性
【发布时间】:2019-09-10 11:17:26
【问题描述】:

如何在使用 Windows Powershell 的工作组计算机上启用密码复杂性?我知道如何在域级别上做到这一点。我们有一些计算机位于远程位置,它们没有域访问权限,因此它们在 Workgroup 上。

【问题讨论】:

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0 powershell-5.0


【解决方案1】:

这对于 powershell 来说不是一个好的解决方案。 这对于本地安全策略来说是可以的。

  1. 运行并输入 SecPol.msc
  2. 转到帐户策略 > 密码策略 > 密码必须满足复杂性要求
  3. 设置为启用。
  4. 设置最小密码长度
  5. 设置密码最长使用期限

所以我决定编写一些函数来通过 Powershell 为您处理所有这些问题。

您可以使用此功能Parse-SecPol 获取和编辑安全策略。这会将整个配置文件转换为 PSobject,以便您可以更改属性并对它们进行排序或任何您想做的事情。

下一个是Set-SecPol,它允许您将对象重新保存到本地安全策略中。

参数-CfgFile 是您要保存配置文件的位置。

这是带有示例的完整脚本(必须以管理员身份运行)

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

【讨论】:

    猜你喜欢
    • 2020-08-04
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多