【问题标题】:Looping and adding extra properties to a Object循环并向对象添加额外的属性
【发布时间】:2021-01-05 12:18:21
【问题描述】:

我正在运行一个简单的脚本来检查多个特定服务是否在一组计算机上运行。它工作正常,除了循环的每次迭代都会出现每个“行”。在这种情况下 3 次。如何编辑/添加到现有对象的属性? IE。我想为同一个对象/行添加额外的属性...

ComputerName Qualys Cloud Agent SplunkForwarder Service Cb Defense
------------ ------------------ ----------------------- ----------
dc01         Found              Found                   Found     
dc01         Found              Found                   Found     
dc01         Found              Found                   Found     
dc02         Found              Found                   Found     
dc02         Found              Found                   Found     
dc02         Found              Found                   Found     
ds01         Found              Found                   Found     
ds01         Found              Found                   Found     
ds01         Found              Found                   Found     


$ComputerName = 'dc01','ds01','dc02'
$ServiceList ='Qualys Cloud Agent',
              'SplunkForwarder Service',
              'Cb Defense'

$objarray = @()
$obj = @()
$ServiceArray = get-service -ComputerName $ComputerName -DisplayName $ServiceList  |  select-object -property MachineName,DisplayName,Name,Status

Foreach ($Computer in $ComputerName){

    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $Computer

    Foreach($Service in $ServiceList){
        If ((gsv -computername $computer -displayname $Service).Status -eq 'Running'){
            #write-output "Found $Service on $Computer"
            $obj | Add-Member -MemberType NoteProperty -Name $service -Value 'Found'
            $objArray += $obj        
        }
        else{
            #write-output "Not  Found"
            $obj | Add-Member -MemberType NoteProperty -Name $service -Value 'Not Found'
            $objArray += $obj
        }
    }


}

$objarray | sort computername

【问题讨论】:

    标签: arrays powershell object foreach


    【解决方案1】:

    $objArray += $obj 语句移到内部循环之外——这样每个对象只被数组引用一次

    Foreach ($Computer in $ComputerName){
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $Computer
    
        Foreach($Service in $ServiceList){
            If ((gsv -computername $computer -displayname $Service).Status -eq 'Running'){
                #write-output "Found $Service on $Computer"
                $obj | Add-Member -MemberType NoteProperty -Name $service -Value 'Found'
            }
            else{
                #write-output "Not  Found"
                $obj | Add-Member -MemberType NoteProperty -Name $service -Value 'Not Found'
            }
        }
        
        # Only need to add the resulting object ONCE
        $objArray += $obj        
    }
    

    我个人更喜欢将每个对象属性列表构造为字典,然后在循环的最后对其进行转换 + 输出——此时我们只需将整个foreach() 语句的结果赋值给 em> 到所需的变量:

    $objArray = foreach($Computer in $ComputerName){
        # Create dictionary to keep track of the new properties
        $props = [ordered]@{ ComputerName = $Computer}
    
        foreach($Service in $ServiceList){
            # Populate dictionary with remaining properties inside the inner loop
            if((gsv -computername $computer -displayname $Service).Status -eq 'Running'){
                $props["$service"] = 'Found'
            }
            else{
                $props["$service"] = 'Not Found'
            }
        }
        
        # convert to object, 
        [pscustomobject]$props
    }
    

    【讨论】:

    • 谢谢,将 $objArray 移到外面工作了。我真正想要做的是通过添加另外几个属性/列(例如 QuaysVersion、SplunkVersion)来扩展它是一个适合这个的字典(哈希表?)?
    • @Aziz 是的,绝对 - 只需在转换为 [pscustomobject] 之前将它们添加到 $props 字典中,例如 $props['SplunkVersion'] = Get-SplunkVersion -ComputerName $computer(将 Get-SplunkVersion ... 替换为您用于获取版本)
    • 我做到了,是的,哈希表方法更直观、更干净。再次感谢!
    【解决方案2】:

    如果您想向对象添加属性,请将对象通过管道传递给“add-member”。这是一个简单的例子:

    $files = Get-childitem "$env:USERPROFILE\Documents\*"
    Foreach ($item in $files)
    {
        $item | Add-Member -MemberType NoteProperty -Name "extention" -Value $item.Name.Substring($item.name.Length - 3)
    }
    $files.extention
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2012-01-13
      • 2021-06-19
      • 2015-06-13
      相关资源
      最近更新 更多