我使用的是windows Server 2012。
我可以這樣做:
在"管理工具"檔案夹中,双击"本地安全策略"圖標,展開"帐戶策略",然後單击"密碼策略".
在右侧窗格中,双击"密碼"必须符合複雜性要求並將其設置為"已禁用".單击"確定"以儲存策略更改。
如何使用Powershell以程式設計方式執行此操作?
最新回復
- 2019-9-121 #
- 2019-9-122 #
我使用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:\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版本
- 2019-9-123 #
我決定編寫一些函式来簡化這个過程。
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
根据@ Kayasax的迴答,没有纯粹的PowerShell方法,您必须將secedit包裝到Powershell中.