0

我有一个脚本,假设要查找主机名所在的范围。

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

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

我导入一个名为 .txt 的主机名文件$list。然后循环$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
4

1 回答 1

2

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

[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
于 2021-04-07T19:35:16.860 回答