Advertisement
upz

Get-DhcpFilterInformation by IP

upz
Dec 3rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-DhcpFilterInformation
  2. {
  3.     #[CmdLetBinding()]
  4.     Param
  5.     (
  6.         [Parameter(Mandatory=$true)]
  7.         [string]$ip,
  8.         [Parameter(Mandatory=$true)]
  9.         [validateset("Allow","Deny","Both")]
  10.         [string]$type = "Both"
  11.     )
  12.     DynamicParam
  13.     {        
  14.         $ParameterName = 'Scope'
  15.        
  16.         $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
  17.  
  18.         $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
  19.        
  20.         $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
  21.         $ParameterAttribute.Mandatory = $true
  22.         $ParameterAttribute.Position = 1
  23.            
  24.         $AttributeCollection.Add($ParameterAttribute)
  25.  
  26.         $arrSet = (Get-DhcpServerv4Scope).ScopeId
  27.         $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
  28.  
  29.         $AttributeCollection.Add($ValidateSetAttribute)
  30.         $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
  31.         $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
  32.         return $RuntimeParameterDictionary
  33.     }
  34.  
  35.     begin
  36.     {
  37.         $Scope = $PsBoundParameters[$ParameterName]
  38.     }
  39.    
  40.     process
  41.     {
  42.         $clientinfo = @()
  43.        
  44.         if ($type -eq 'Both')
  45.         {
  46.             $filter = Get-DhcpServerv4Filter | where {$_.List -like "*"}
  47.         }
  48.         else
  49.         {
  50.             $filter = Get-DhcpServerv4Filter | where {$_.List -eq $type}
  51.         }
  52.  
  53.  
  54.         $lease = Get-DhcpServerv4Lease -ScopeId $Scope | where {$_.IPAddress -eq $ip}
  55.         foreach ($item in $filter)
  56.         {
  57.             if ($lease.ClientId -eq $item.MacAddress)
  58.             {
  59.                 $obj = New-Object -TypeName psobject
  60.                 try
  61.                 {
  62.                     $name = Resolve-DnsName $lease.IPAddress.tostring() -QuickTimeout -ErrorAction Stop
  63.                     $obj | Add-Member -MemberType NoteProperty -Name PcName -Value $name.NameHost
  64.                 }
  65.                 catch
  66.                 {
  67.                     $name = "N/A"
  68.                     $obj | Add-Member -MemberType NoteProperty -Name PcName -Value $name
  69.                 }
  70.        
  71.                 $obj | Add-Member -MemberType NoteProperty -Name IP -Value $lease.IPAddress
  72.                 $obj | Add-Member -MemberType NoteProperty -Name MAC -Value $lease.ClientId
  73.                 $obj | Add-Member -MemberType NoteProperty -Name ListType -Value $item.List
  74.                 $obj | Add-Member -MemberType NoteProperty -Name Description -Value $item.Description
  75.        
  76.                 $clientinfo += $obj
  77.             }
  78.  
  79.             if ($clientinfo -ne $null)
  80.             {
  81.                 break
  82.             }
  83.         }
  84.        
  85.         if ($clientinfo -ne $null)
  86.         {
  87.             $clientinfo
  88.         }
  89.         else
  90.         {
  91.             Write-Host  -ForegroundColor Red "No Client with $ip in $type List"
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement