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

Switch语句未处理所有条件:检查OU是否存在

PowerShell Switch语句问题排查与替代方案

我想用Switch语句测试不同OU,找到匹配位置代码的OU后,将计算机移动到该OU的子OU中。每个位置代码仅对应一个OU,因此通过[adsi]::Exists()检查OU是否存在,找到匹配OU时该方法会返回True。但目前Switch语句只评估第一个条件就退出了,不清楚原因,求建议。

预期效果:希望Switch逐个评估每个条件,直到找到返回True的条件,然后执行对应代码,将计算机移到正确的目录位置。

想问下是不是改用if/else语句更合适?

原代码如下:

$LocationCode = $this.PCName.split("-")[0]
        
try{
    $PCDC = (Get-ADComputer $this.PCName).DistinguishedName
    $FoundLCInOU = $true
    Switch ($FoundLCInOU) {
        # Check for location Code in Stores
        ([adsi]::Exists("OU DN")){
            Write-Host " This computer belongs to the Location 1"
        }
        # Check for location code in CustomerExperienceCenter
        ([adsi]::Exists("OU DN")){
            Write-Host "This Computer blongs to the Location 2"
        }
        # Check for location code in CAF
        ([adsi]::Exists("OU DN")){
            Write-Host "This Computer Belongs to Location 3"
        }
        # Check for location code in HomeOffice 
        ([adsi]::Exists("OU DN")){
            Write-Host "This computer belongs to the Location 4"
        }
    } catch {
        Write-Host "Couldn't find a home for this PC, just keep swimming..."
    }

问题根源

  1. Switch输入值逻辑错误:你将固定为$true的$FoundLCInOU作为Switch的测试值,导致第一个返回$true的[adsi]::Exists()分支会被触发,而PowerShell Switch默认匹配到第一个符合条件的分支后就终止执行,不会继续检查后续条件。
  2. OU路径未动态代入位置代码:代码中"OU DN"是固定字符串,没有将$LocationCode嵌入实际OU路径,导致所有分支检查的是同一个无效OU,完全偏离需求逻辑。

修复方案

方案1:修正Switch语句

如果坚持使用Switch,需要调整测试值并嵌入位置代码,确保逻辑生效:

$LocationCode = $this.PCName.split("-")[0]
        
try{
    $PCDC = (Get-ADComputer $this.PCName).DistinguishedName
    # 以$true为测试值,逐个评估条件分支
    Switch ($true) {
        # 检查Stores下对应位置代码的OU
        ([adsi]::Exists("OU=$LocationCode,OU=Stores,DC=yourdomain,DC=com")){
            Write-Host "这台计算机属于Location 1"
            # 添加移动计算机的代码,例如:Move-ADObject -Identity $PCDC -TargetPath "OU=SubOU,OU=$LocationCode,OU=Stores,DC=yourdomain,DC=com"
            break # 找到匹配后退出Switch
        }
        # 检查CustomerExperienceCenter下对应位置代码的OU
        ([adsi]::Exists("OU=$LocationCode,OU=CustomerExperienceCenter,DC=yourdomain,DC=com")){
            Write-Host "这台计算机属于Location 2"
            # 添加移动计算机的代码
            break
        }
        # 检查CAF下对应位置代码的OU
        ([adsi]::Exists("OU=$LocationCode,OU=CAF,DC=yourdomain,DC=com")){
            Write-Host "这台计算机属于Location 3"
            # 添加移动计算机的代码
            break
        }
        # 检查HomeOffice下对应位置代码的OU
        ([adsi]::Exists("OU=$LocationCode,OU=HomeOffice,DC=yourdomain,DC=com")){
            Write-Host "这台计算机属于Location 4"
            # 添加移动计算机的代码
            break
        }
        default {
            Write-Host "找不到这台计算机对应的OU"
        }
    }
} catch {
    Write-Host "获取计算机信息失败:$_"
}

注意:需将代码中的域路径DC=yourdomain,DC=com替换为实际环境的域结构,确保$LocationCode正确嵌入OU路径。

方案2:改用if/elseif语句

在这种"顺序检查、单匹配"的场景下,if/elseif的逻辑更直观,可读性和维护性更强,更推荐使用:

$LocationCode = $this.PCName.split("-")[0]
        
try{
    $PCDC = (Get-ADComputer $this.PCName).DistinguishedName
    
    if ([adsi]::Exists("OU=$LocationCode,OU=Stores,DC=yourdomain,DC=com")) {
        Write-Host "这台计算机属于Location 1"
        # 添加移动计算机的代码
    }
    elseif ([adsi]::Exists("OU=$LocationCode,OU=CustomerExperienceCenter,DC=yourdomain,DC=com")) {
        Write-Host "这台计算机属于Location 2"
        # 添加移动计算机的代码
    }
    elseif ([adsi]::Exists("OU=$LocationCode,OU=CAF,DC=yourdomain,DC=com")) {
        Write-Host "这台计算机属于Location 3"
        # 添加移动计算机的代码
    }
    elseif ([adsi]::Exists("OU=$LocationCode,OU=HomeOffice,DC=yourdomain,DC=com")) {
        Write-Host "这台计算机属于Location 4"
        # 添加移动计算机的代码
    }
    else {
        Write-Host "找不到这台计算机对应的OU"
    }
} catch {
    Write-Host "获取计算机信息失败:$_"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 15:10:27