PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] How can I change my output from DHCP Lease info to ScopeId "Name"

 August 16, 2022     arrays, dhcp, output, powershell, scope-id     No comments   

Issue

Here is what My output looks like, It's just lease information where the host name is:

IPAddress       ScopeId         ClientId           HostName       AddressState         
---------       -------         --------          --------        ------------  
10.10.10.10   99.99.99.99     11-11-11-11-11-11    AL10              Active               

Using this PowerShell script:

$hostname = "AL10"
$locationArray = @()
  foreach ($Server in $DHServers){
        
        $scope = Get-DHCPServerv4scope -ComputerName $Server.dnsname  | Get-DHCPServerv4Lease -ComputerName $Server.dnsname | Where-Object HostName -like "$hostName*"
        
        $locationArray += $scope
}
 
$locationArray

What I would like, is it just to output:

ScopeID Name               
---------        
Name 

Goal is: Provide a .txt of hostnames, find the corresponding DHCP Server leases, then output the "name" of the ScopeID like when using Get-DHCPServerv4scope -ComputerName $Server.dnsname | Select-Object "name"


Solution

I used a hash table.

$hashtable = @{} #create hash table

$i=0
foreach ($Server in $DHServers){
    $scopes = Get-DHCPServerv4Scope -ComputerName $Server.dnsname #get all scopes
    foreach ($hostname in (Import-Csv C:\script\Asset_List.csv | Select-Object -ExpandProperty asset)){ #get hostnames from list
        foreach ($scope in $scopes) {
            
            if($scope | Get-DhcpServerV4Lease -ComputerName $server.dnsname | Where-Object HostName -like "$hostName*" ) { #compares the hostname to find which lease it is in
                try{
                $hashtable.add($hostname, $scope.name)# add keys, values to table
                }
                
                catch {
                    $ErrorMessage = $_.Exception.Message
                    if ($ErrorMessage  -like '*Key in dictionary Already*') {
                         write-host 'Network issues could be causing process to take longer than normal' -ForegroundColor Green
                    }
                }
           
            } 
        }
    }
}

Then I called it in my PSCustomObject

[PSCustomObject]@{ #Rename varibles in data pull for output file
    Asset = $hostname
    Model = $System.Model
    SerialNumber = $BIOS.SerialNumber
    Location = $hashtable[$hostname]
    LastUser = $User
    MacAddress = $mac
    IpAddress = $IpV
    Status = "Online"
    }
}   


Answered By - feelsgood
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing