AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 930582
Accepted
mythofechelon
mythofechelon
Asked: 2018-09-13 06:08:27 +0800 CST2018-09-13 06:08:27 +0800 CST 2018-09-13 06:08:27 +0800 CST

检查用户帐户是否设置了密码

  • 772

在独立(未加入域)Windows 中,是否可以使用 PowerShell 检查给定的本地用户帐户是否设置了密码/密码为空?

根据https://gallery.technet.microsoft.com/scriptcenter/How-to-check-if-a-local-870ab031和https://blogs.technet.microsoft.com/heyscriptingguy/2005/10/06/ how-can-i-verify-that-none-of-my-local-user-accounts-have-a-blank-password/,这可以通过 VBScript 实现,但这只是因为ChangePassword需要您提供原始密码,而 PowerShell 命令似乎没有。

我在某处读到,您可以通过使用其凭据以用户身份运行进程并记下结果来验证密码,但显然,您不能使用空字符串作为凭据。

windows
  • 2 2 个回答
  • 3355 Views

2 个回答

  • Voted
  1. Best Answer
    mythofechelon
    2018-09-13T07:56:53+08:002018-09-13T07:56:53+08:00

    我通过以下 PowerShell 命令/脚本实现了这一点:

    Write-Output "It's only possible to detect whether user accounts have blank passwords if the minimum password length is 0.";
    
    $PasswordMinimumLength = 0;
    Write-Output "Implementing new minimum password length of $PasswordMinimumLength...";
    
    $Secedit_CFGFile_Path = [System.IO.Path]::GetTempFileName();
    $Secedit_Path = "$env:SystemRoot\system32\secedit.exe";
    $Secedit_Arguments_Export = "/export /cfg $Secedit_CFGFile_Path /quiet";
    $Secedit_Arguments_Import = "/configure /db $env:SystemRoot\Security\local.sdb /cfg $Secedit_CFGFile_Path /areas SecurityPolicy";
    
    Start-Process -FilePath $Secedit_Path -ArgumentList $Secedit_Arguments_Export -Wait;
    
    $SecurityPolicy_Old = Get-Content $Secedit_CFGFile_Path;
    
    $SecurityPolicy_New = $SecurityPolicy_Old -Replace "MinimumPasswordLength = \d+", "MinimumPasswordLength = $PasswordMinimumLength";
    
    Set-Content -Path $Secedit_CFGFile_Path -Value $SecurityPolicy_New;
    
    Try {
        Start-Process -FilePath $Secedit_Path -ArgumentList $Secedit_Arguments_Import -Wait;
    } Catch {
        Write-Output "...FAILED.";
        Break;
    }
    If ($?){
        Write-Output "...Success.";
    }
    Write-Output "";
    Write-Output "----------------------------------------------------------------";
    Write-Output "";
    
    Write-Output "Searching for user accounts with blank passwords...";
    
    $BlankPasswordsFoundWording_PreUsername = "Found user account";
    $BlankPasswordsFoundWording_PostUsername = "with a blank password.";
    $NoBlankPasswordsFoundWording = "No user accounts with blank passwords found.";
    
    $VBS_IdentifyBlankPasswords_Commands = @"
    On Error Resume Next
    
    Dim strComputerName
    Dim strPassword
    
    strComputerName = WScript.CreateObject("WScript.Network").ComputerName
    strPassword = ""
    
    Set LocalAccounts = GetObject("WinNT://" & strComputerName)
    LocalAccounts.Filter = Array("user")
    
    Dim Flag
    Flag = 0 
    
    For Each objUser In LocalAccounts
        objUser.ChangePassword strPassword, strPassword
        If Err = 0 or Err = -2147023569 Then
            Flag = 1
            Wscript.Echo "$BlankPasswordsFoundWording_PreUsername """ & objUser.Name & """ $BlankPasswordsFoundWording_PostUsername"
        End If
        Err.Clear
    Next
    
    If Flag = 0 Then
        WScript.Echo "$NoBlankPasswordsFoundWording"
    End If
    "@
    # The above here-string terminator cannot be indented.;
    
    # cscript won't accept / process a file with extension ".tmp" so ".vbs" needs to be appended.;
    $VBS_IdentifyBlankPasswords_File_Path_TMP = [System.IO.Path]::GetTempFileName();
    $VBS_IdentifyBlankPasswords_File_Directory = (Get-ChildItem $VBS_IdentifyBlankPasswords_File_Path_TMP).DirectoryName;
    $VBS_IdentifyBlankPasswords_File_Name_TMP = (Get-ChildItem $VBS_IdentifyBlankPasswords_File_Path_TMP).Name;
    $VBS_IdentifyBlankPasswords_File_Name_VBS = $VBS_IdentifyBlankPasswords_File_Name_TMP + ".vbs";
    $VBS_IdentifyBlankPasswords_File_Path_VBS = "$VBS_IdentifyBlankPasswords_File_Directory\$VBS_IdentifyBlankPasswords_File_Name_VBS";
    
    Set-Content -Path $VBS_IdentifyBlankPasswords_File_Path_VBS -Value $VBS_IdentifyBlankPasswords_Commands;
    
    $VBS_IdentifyBlankPasswords_Output = & cscript /nologo $VBS_IdentifyBlankPasswords_File_Path_VBS;
    # Write-Output $VBS_IdentifyBlankPasswords_Output;
    
    $UsersWithBlankPasswords = $VBS_IdentifyBlankPasswords_Output | Select-String -Pattern "$BlankPasswordsFoundWording_PreUsername";
    
    If ($UsersWithBlankPasswords -NE $Null){
        ForEach ($UserWithBlankPassword in $UsersWithBlankPasswords){
            $Username = [regex]::match($UserWithBlankPassword, '"([^"]+)"').Groups[1].Value;
    
            Write-Output "...$BlankPasswordsFoundWording_PreUsername ""$Username"" $BlankPasswordsFoundWording_PostUsername";
        }
    } ElseIf ($UsersWithBlankPasswords -Eq $Null){
        Write-Output "$NoBlankPasswordsFoundWording";
    }
    
    Write-Output "";
    Write-Output "----------------------------------------------------------------";
    Write-Output "";
    
    Write-Output "Implementing original minimum password length...";
    
    Set-Content -Path $Secedit_CFGFile_Path -Value $SecurityPolicy_Old;
    
    Try {
        Start-Process -FilePath $Secedit_Path -ArgumentList $Secedit_Arguments_Import -Wait;
    } Catch {
        Write-Output "...FAILED.";
        Break;
    }
    If ($?){
        Write-Output "...Success.";
    }
    

    在此处输入图像描述

    • 2
  2. SteepUnderstanding
    2020-11-15T05:02:47+08:002020-11-15T05:02:47+08:00

    大量的搜索和反复试验导致我开发了这个:

    $PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('Machine')
    
    Get-LocalUser | Where-Object Enabled -eq $true | ForEach-Object {
        $myUsername = $_.Name
        $myPasswordIsBlank = $PrincipalContext.ValidateCredentials($myUserName, $null)
        If ($myPasswordIsBlank) {
            # Do whatever you want here to output or alert the fact that you found a blank password.
        }
    }
    

    要通过我的 RMM 运行它,我必须在代码开头添加以下内容以防止出现错误:

    Add-Type -AssemblyName System.DirectoryServices.AccountManagement
    
    • 2

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果 Windows 服务崩溃,如何自动重新启动它?

  • 无法安排任务(访问被拒绝)

  • 物理机重启时自动重启虚拟机(VMWare)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve