在 PowerShell 中,为什么我的 if 语句返回整个数组而不是单个对象?

问题描述

我有一个脚本,用于查找主机名所在的范围。

我的 PSCustomObject Location = $scope.name 正在获取整个范围数组而不是匹配项,如下所示。

Asset    : U1087
Location : {bdg-213,Vo213,0V-Gen-DH,0-v734...}
Status   : Online

Asset    : U1281
Location : {bdg-213,0-v734...}
Status   : Offline

Asset    : U1061
Location : {bdg-13,0-v734...}
Status   : Online

我导入了一个名为 $list 的主机名 .txt 文件。然后循环遍历 $list$scopes 以查找匹配项。

$list = Get-Content C:\script\HostNameList.txt #Defines content it pulls as list 
$DHServers = Get-DhcpserverInDC #gives variable name for loop

$Output =  
foreach ($hostname in $list) { #Calls each item in list a hostname and sends to output
    if (test-connection -count 1 -computername $hostname -quiet){  #With 1 ping,check if hostname is online
        
        [array] $scope = 
        foreach ($Server in $DHServers){
            Get-DHcpserverv4Scope -ComputerName $Server.dnsname #get all scopes
            
            $i = 0; 
            foreach ($scope in $scopes){ 
                if($scope | Get-DhcpserverV4Lease -ComputerName $DHServers[$i++] | Where-Object HostName -like "$hostName*" ) #compares the hostname to lease to find which scope it is in`
                { $scopename} #return name of the scope it found hostname in. Name property is location in DHCP
            } 
        }
        [PSCustomObject]@{ #Rename varibles in data pull for output file
        Asset = $hostname
        Location = $scope.name #only want the name of the scope
        Status = "Online"
        }
    }
    else #statement if hostname is not online
    { 
 
    }
}
$Output #show output in powershell
$Output | Export-Csv -Path C:\script\Asset_Result.csv -NoTypeinformation #outputs .csv

解决方法

正如其他人评论的那样,循环的嵌套看起来不正确。让我试着分析一下有问题的代码。

[array] $scopes = foreach ($Server in $DHServers){
 
    # Creates output that is collected in $scopes 
    Get-DHCPServerv4Scope -ComputerName $Server.dnsname

    # Now you are looping through the output of the outer loop,which is not finished yet,# in the inner loop,redundantly looping over $scopes multiple times.
    $i = 0; foreach ($scope in $scopes){
        if($scope | Get-DhcpServerV4Lease -ComputerName $DHServers[$i++] | Where-Object HostName -like "$hostName*") 
           {$scope.name}  # Here you are outputting the scope name to $scopes
                          # again,which doesn't make any sense at all.
    }
}

# Here you assign the $scope.name from the *last* iteration of the inner loop,regardless 
# of the if statement in the inner loop. Again,this doesn't look right.
[PSCustomObject]@{ 
   Asset = $hostname
   Location = $scope.name
   Status = "Online"
}

以下是“一半的答案”,因为我认为我没有完全理解您的代码的意图。至少这样对我来说看起来更合乎逻辑。

$DHServers = Get-DhcpServerInDC #gives variable name for loop

$Output = foreach ($hostname in $list){ #Calls each item in list a hostname and sends to output
    
   if (test-connection -count 1 -computername $hostname -quiet){ #With 1 ping,check if hostname is online

      [array] $scopes = foreach ($Server in $DHServers){# Collects the output from all Get-DHCPServerv4Scope calls
         Get-DHCPServerv4Scope -ComputerName $Server.dnsname
      }

      $i = 0 
      foreach ($scope in $scopes){#loop through array
         if($scope | Get-DhcpServerV4Lease -ComputerName $DHServers[$i++] | Where-Object HostName -like "$hostName*"){ #compares the hostname to lease to find which scope it is in

            [PSCustomObject]@{ #Rename varibles in data pull for output file
               Asset = $hostname
               Location = $scope.name #only want the name of the scope
               Status = "Online"
            }
         }
      }
   }
}   

$Output | Format-Table #show output in PowerShell