0

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

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

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
feelsgood
  • 135
  • 14
  • 1
    What are you trying to return? Just the location? I.e: "bdg-213"? – Abraham Zinala Apr 07 '21 at 19:15
  • Essentially, yes. The scope property "Name" in DHCP is the location hence ```scope.name``` – feelsgood Apr 07 '21 at 19:21
  • 1
    It looks like you misplaced some of the curly brackets. E. g. you are using `$scopes`, which gets assigned the output from outer loop, within inner loop. – zett42 Apr 07 '21 at 19:23
  • 2
    Your inner loop doesn't look right: `$scopes` is used to collect output but it's also is used inside the innermost loop, `Get-DHCPServerv4Scope` result is output to the pipeline and will be assigned to the topmost `$scopes`, you return scriptblock `{$scope.name}` from the innermost loop. I'm not sure how it's supposed to work and I get a feeling that it outputs _something_ just because you have leftover variables in your session or somewhere else in the script. – beatcracker Apr 07 '21 at 19:24
  • 1
    What you can do is the following: `$Scope.Name.Location | Select -First 1` given that`$scope.name` returns that output shown in the first code block – Abraham Zinala Apr 07 '21 at 19:27
  • Thank you all for the replies and your patience , I had a missing bracket and updated to an actually working version. – feelsgood Apr 07 '21 at 20:02
  • @AbrahamZinala That option didn't work. This would give the first listed in scopes rather than a match right? – feelsgood Apr 07 '21 at 20:06

1 Answers1

2

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
mklement0
  • 382,024
  • 64
  • 607
  • 775
zett42
  • 25,437
  • 3
  • 35
  • 72
  • My overall intent is pull server information using, ```Get-DhcpServerInDC``` ```Get-DHCPServerv4Scope``` and ```Get-DhcpServerV4Lease```. Then take my list of hostnames to find the corresponding lease they reside. This will tell me what building the hostname is located. This is really, a follow-up question, if you'd like to take a look at my last post. – feelsgood Apr 07 '21 at 20:15