programing

Powershell을 사용하여 로컬 보안 정책 수정

starjava 2023. 8. 30. 21:03
반응형

Powershell을 사용하여 로컬 보안 정책 수정

윈도우즈 서버 2012를 사용합니다.

할 수 있습니다.

관리 도구 폴더에서 로컬 보안 정책 아이콘을 두 번 클릭하고 계정 정책을 확장한 다음 암호 정책을 클릭합니다.

오른쪽 창에서 암호가 복잡성 요구 사항을 충족해야 하며 사용 안 함으로 설정해야 함을 두 번 클릭합니다.확인을 클릭하여 정책 변경 내용을 저장합니다.

Powershell을 사용하여 프로그래밍 방식으로 수행하려면 어떻게 해야 합니까?

@Kayasax의 답변에 따르면 순수한 파워셸 방식은 없습니다. 파워셸로 seed를 포장해야 합니다.

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

저는 이 과정을 더 쉽게 하기 위해 몇 가지 기능을 쓰기로 결정했습니다.

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

Windows 7을 사용합니다.

다음 파워셸 스크립트를 사용하여 해결했습니다.

$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에서만 작동했습니다.

언급URL : https://stackoverflow.com/questions/23260656/modify-local-security-policy-using-powershell

반응형