【问题标题】:How to add a boolean flag to a variable based on a condition in powershell?如何根据powershell中的条件向变量添加布尔标志?
【发布时间】:2021-09-29 19:27:41
【问题描述】:

我有一个 PS 脚本从 AD 中检索所有设备及其 OU 信息,如下所示:

$UserList = Get-ADComputer -Filter * -Property * | Select-Object Name,DistinguishedName,LastLogonDate 

$Results = foreach ($UL_Item in $UserList)
    {
        [array]$OuList = @($UL_Item.DistinguishedName.Split(',')).
            Where({$_ -match 'OU='}).
            ForEach({$_.Split('=')[-1]}).
            Trim()

        [PSCustomObject]@{
            ComputerName = $UL_Item.Name
            OU_1 = $OuList[0]
            OU_2 = $OuList[1]
            OU_3 = $OuList[2]
            OU_4 = $OuList[3]
            OU_5 = $OuList[4]
        }
    }

我想做的是在我的变量中添加一个名为 IsServer 的标志:$Results 如果任何 OU 等于“域服务器”,则将其设置为 True,所以我正在寻找正确的类似这样的语法:

...
OU_5 = $OuList[4]
IsServer = if ($OuList[0]="Domain Servers" OR 
               $OuList[1]="Domain Servers" OR 
               $OuList[2]="Domain Servers" OR 
               $OuList[3]="Domain Servers" OR 
               $OuList[4]="Domain Servers" OR ) then true else false end

管理此问题的最佳方法是什么?任何帮助将不胜感激。

【问题讨论】:

  • if($OuList -contains "Domain Servers")

标签: powershell powershell-4.0


【解决方案1】:

你可以这样做:

IsServer = @{$true="true";$false="false"}[($OuList -contains "Domain Servers"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多