在 PowerShell 中,为什么我的 if 语句返回整个数组而不是单个对象? [英] In PowerShell, why is my if statement returning the whole array rather than a single object?

查看:57
本文介绍了在 PowerShell 中,为什么我的 if 语句返回整个数组而不是单个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a script that is suppose to Find which Scope the Hostname in.

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

My PSCustomObject Location = $scope.name is getting the whole array of scopes rather than the match, as seen below.

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

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

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

我导入了一个名为 $list 的主机名 .txt 文件.然后遍历 $list$scopes 寻找匹配.

I Import a .txt file of hostnames called $list. Then loop through $list and $scopes looking for a match.

$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

推荐答案

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

As others commented, the nesting of the loops doesn't look right. Let me try to analyze the problematic code.

[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"
}

以下是一半的答案";因为我不认为我完全理解你的代码的意图.至少这样对我来说看起来更合乎逻辑.

The following is a "half answer" as I don't think I fully understand the intent of your code. At least this way it looks more logical to me.

$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

这篇关于在 PowerShell 中,为什么我的 if 语句返回整个数组而不是单个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆