首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Powershell修改本地安全策略

使用Powershell修改本地安全策略
EN

Stack Overflow用户
提问于 2014-04-24 13:40:25
回答 3查看 75K关注 0票数 25

我使用Windows Server 2012。

我可以这样做:

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

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

如何使用Powershell以编程方式完成此任务?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-24 17:38:16

根据@Kayasax的回答,没有纯粹的Powershell方法可以做到这一点,你必须将secedit封装到powershell中。

代码语言:javascript
运行
复制
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
票数 29
EN

Stack Overflow用户

发布于 2019-04-21 01:38:33

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

Parse-SecPol:将本地安全策略转换为PsObject。您可以查看所有属性并对对象进行更改。

Set-SecPol:将Parse-SecPol对象转换回配置文件,并将其导入到本地安全策略中。

下面是它的用法示例:

代码语言:javascript
运行
复制
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
票数 8
EN

Stack Overflow用户

发布于 2015-11-13 17:33:06

我使用Windows 7

我已经使用下面的powershell脚本解决了这个问题

代码语言:javascript
运行
复制
$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版本一起工作

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23260656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档