You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell脚本实现Guest账户未找到时的指定输出

PowerShell脚本优化:处理Guest账户不存在的场景

现有PowerShell脚本能够检测本地Guest账户(无论其是否启用),但需要补充逻辑:当目标服务器上不存在Guest账户时,输出指定格式的提示内容。

期望输出(Guest账户不存在时)

Name  Status  
----  ------  
Guest User NOT Found

原有脚本

#requires -Version 2.0
Param
(
    [Parameter(Position=0,Mandatory=$false)]
    [ValidateNotNullorEmpty()]
    [Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,
    [Parameter(Position=1,Mandatory=$false)]
    [Alias('un')][String[]]$AccountName,
    [Parameter(Position=2,Mandatory=$false)]
    [Alias('cred')][System.Management.Automation.PsCredential]$Credential
)
    
$Obj = @()

Foreach($Computer in $ComputerName)
{
    If($Credential)
    {
        $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
        -Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction Stop
    }
    else
    {
        $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
        -Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction Stop
    }
    
    Foreach($LocalAccount in $AllLocalAccounts)
    {
        $Object = New-Object -TypeName PSObject
        
        $Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
        $Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
        $Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
        $Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
        $Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
        $Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
        $Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
        $Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
        $Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
        $Object|Add-Member -MemberType NoteProperty -Name "SID" -Value $LocalAccount.SID
        $Object|Add-Member -MemberType NoteProperty -Name "SID Type" -Value $LocalAccount.SIDType
        $Object|Add-Member -MemberType NoteProperty -Name "Account Type" -Value $LocalAccount.AccountType
        $Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
        $Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
        
        $Obj+=$Object
    }

}

$obj | Where-Object {$_.Name -eq "Guest"} | Select-Object Name,Status

修改后的脚本

#requires -Version 2.0
Param
(
    [Parameter(Position=0,Mandatory=$false)]
    [ValidateNotNullorEmpty()]
    [Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,
    [Parameter(Position=1,Mandatory=$false)]
    [Alias('un')][String[]]$AccountName,
    [Parameter(Position=2,Mandatory=$false)]
    [Alias('cred')][System.Management.Automation.PsCredential]$Credential
)
    
$Obj = @()

Foreach($Computer in $ComputerName)
{
    If($Credential)
    {
        $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
        -Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction Stop
    }
    else
    {
        $AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
        -Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction Stop
    }
    
    Foreach($LocalAccount in $AllLocalAccounts)
    {
        $Object = New-Object -TypeName PSObject
        
        $Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
        $Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
        $Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
        $Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
        $Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
        $Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
        $Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
        $Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
        $Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
        $Object|Add-Member -MemberType NoteProperty -Name "SID" -Value $LocalAccount.SID
        $Object|Add-Member -MemberType NoteProperty -Name "SID Type" -Value $LocalAccount.SIDType
        $Object|Add-Member -MemberType NoteProperty -Name "Account Type" -Value $LocalAccount.AccountType
        $Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
        $Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
        
        $Obj+=$Object
    }

}

# 筛选Guest账户结果
$guestAccount = $obj | Where-Object {$_.Name -eq "Guest"}

# 判断是否找到账户,未找到则生成指定格式的输出
if (-not $guestAccount) {
    $notFoundObj = [PSCustomObject]@{
        Name = "Guest"
        Status = "User NOT Found"
    }
    $notFoundObj | Select-Object Name,Status
} else {
    $guestAccount | Select-Object Name,Status
}

改动说明

  • 新增$guestAccount变量存储筛选后的Guest账户结果
  • 添加条件判断逻辑:若未找到Guest账户,创建包含指定内容的自定义对象,确保输出格式与正常检测结果一致
  • 找到账户时沿用原有逻辑输出账户信息

内容的提问来源于stack exchange,提问作者Arbelac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:13:29