About Me

My photo
HANUMANGARH, RAJASTHAN, India

Wednesday 9 February 2022

Convert DHCP IP Address into Reserved IP Address

 <#PSScriptInfo

 
.VERSION 1.0
 
.GUID e6b8fb5c-e5c9-4807-a6e5-d5a7a719349d
 
.AUTHOR pavel_kandratsyeu@outlook.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS DHCP
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
DhcpServer
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>


<#
.SYNOPSIS
Converts IPV4 DHCP lease into DHCP reservation.
 
.DESCRIPTION
The script searches dhcp lease by IP or MAC address on all dhcp servers in Active Directory using WinRM session and converts them into DHCP reservation. If dhcp server has a failover partner, the script notifies partner server too.
The scripot requires domain admin credentials to work.
 
.EXAMPLE
Convert-LeaseToReservation.ps1 -IpAddress 192.168.0.1 -Description 'Convert lease to reservation'
 
Converts lease by IP address, searching on all dhcp servers.
 
.EXAMPLE
.Convert-LeaseToReservation.ps1 -ClientId '0102030405ff' -Description 'Convert by MAC address'
 
Converts lease by MAC address, searching on all dhcp servers.
 
.EXAMPLE
Convert-LeaseToReservation.ps1 -IpAddress 192.168.0.1 -Description 'Convert lease to reservation' -ComputerName localhost -Verbose
 
Converts lease by IP address, searching on server localhost.
 
#>
 


#requires -Version 3.0 -Modules DhcpServer


[CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = 'High',
        DefaultParametersetName = 'P1'
)]
param(
    [Parameter (
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = 'P1'
    )]
    [Alias('IP')]
    [ValidateNotNullOrEmpty()]
    [string]
    $IpAddress,

    [Parameter (
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = 'P2'
    )]
    [Alias('MAC')]
    [ValidateNotNullOrEmpty()]
    [string]
    $ClientId,

    [Parameter (
            Mandatory = $false
    )]
    [Alias('Server')]
    [ValidateNotNullOrEmpty()]
    [string]
    $ComputerName,

    [Parameter (
            Mandatory = $false
    )]
    [Alias('Notes')]
    [ValidateNotNullOrEmpty()]
    [string]
    $Description
)

begin
{
    $DhcpServers = Get-DhcpServerInDC | 
    Select-Object -Property IPAddress, DNSname |
    Sort-Object -Property Country, City, IPAddress
    
    $DhcpServers | Write-Verbose
}

process
{
    switch ($PsCmdlet.ParameterSetName)
    {
        'P1'
        {
            Write-Verbose -Message 'Parameter set P1 has been chosen.'
            $ScripBlock = {
                Import-Module -Name DhcpServer
                $VerbosePreference = $Using:VerbosePreference
                try
                {
                    Get-DhcpServerv4Lease -IPAddress $Using:IpAddress -ErrorAction Stop
                }
                catch
                {
                    Write-Verbose -Message "Failed to get lease information of IPAddress $Using:IpAddress on DHCP server $Env:COMPUTERNAME."
                }
            }
        }

        'P2'
        {
            Write-Verbose -Message 'Parameter set P2 has been chosen.'
            $ScripBlock = {
                Import-Module -Name DhcpServer
                $VerbosePreference = $Using:VerbosePreference
                Get-DhcpServerv4Scope |
                ForEach-Object -Process {
                    try
                    {
                        $ScopeId = $_.ScopeId
                        $_ | Get-DhcpServerv4Lease -ClientId ($Using:ClientId).Trim().Replace(':', '-') -ErrorAction Stop
                    }
                    catch
                    {
                        Write-Verbose -Message "Failed to get lease information of ClientId $Using:ClientId in sope $ScopeId on DHCP server $Env:COMPUTERNAME."
                    }
                }
            }
        }
    }


    Write-Verbose -Message 'Searching dhcp lease...'

    $DhcpV4Lease = Invoke-Command -ComputerName $(if($ComputerName){$ComputerName.Trim()}else{$DhcpServers.DNSname}) -ScriptBlock $ScripBlock |
    Group-Object -Property IPAddress |
    ForEach-Object -Process {$_.Group | Select-Object -First 1}

    if (-not $DhcpV4Lease)
    {
        Write-Warning -Message "Lease with Id '$(if($IpAddress){$IpAddress}else{$ClientId})' was not found. Check that PSRemoting is enabled on the DHCP servers and try agan."
        
        exit 0
    }

    $DhcpV4Lease |
    ForEach-Object -Process {
        Write-Verbose -Message "Lease has been found on server $($_.PSComputerName):"
        Write-Verbose -Message "IpAddress: $($_.IpAddress)"
        Write-Verbose -Message "ClientId: $($_.ClientId)"
        Write-Verbose -Message "ScopeId: $($_.ScopeId)"
        Write-Verbose -Message "HostName: $($_.HostName)"
        Write-Verbose -Message "AddressState: $($_.AddressState)"
    }

    $DhcpV4Lease |
    Where-Object -Property AddressState -Like -Value '*Reservation' |
    ForEach-Object -Process {
        "Lease IpAddress $($_.IpAddress) already reserved on server $($_.PSComputerName):", 
        "ClientId: $($_.ClientId)", 
        "ScopeId: $($_.ScopeId)" , 
        "HostName: $($_.HostName)", 
        "AddressState: $($_.AddressState)", 
        "Description: $($_.Description)" |
        Write-Warning
    }

    try
    {
        $AddDhcpReservationParams = @{Type = 'Dhcp'}

        if ($Description)
        {
            $AddDhcpReservationParams.Add('Description', $Description)
        }

        $DhcpV4Lease |
        Where-Object -Property AddressState -NotLike -Value '*Reservation' |
        ForEach-Object -Process {
            $Computer = $_.PSComputerName

            if($PsCmdlet.ShouldProcess($_.IPAddress, "Convert lease to reservation: [ScopeId: $($_.ScopeId); Server: $Computer]"))
            {
                $_ |
                Add-DhcpServerv4Reservation @AddDhcpReservationParams -ComputerName $Computer -PassThru -ErrorAction Stop |
                ForEach-Object {
                    if (Get-DhcpServerv4Failover -ComputerName $Computer -ScopeId $_.ScopeId -ErrorAction Ignore)
                    {
                        $null = Invoke-DhcpServerv4FailoverReplication -ComputerName $Computer -ScopeId $_.ScopeId -Force
                    }
                    Write-Host "[IpAddress: $($_.IpAddress), Scope: $($_.ScopeId), Server: $Computer] has been added to reservation." -ForegroundColor Green
                }
            }
        }
    }
    catch
    {
        Write-Host -Object "Failed to add address '$(if($IpAddress){$IpAddress}else{$ClientId})' to dhcp reservation on server '$($DhcpV4Lease.PSComputerName)': $($_.Exception.Message)" -ForegroundColor Red
        
        exit 1
    }
}

Export contact from Justdial

  Extract Data From JustDial using Selenium Let us see how to extract data from Justdial using Selenium and Python. Justdial is a company th...