Sunday 20 January 2013

Mass import DHCP reservations using Powershell (and netsh)



        <#
        .SYNOPSIS
            This script is used to generate a netsh CMD file to import a series of machines to DHCP
        .DESCRIPTION
            Rather than manually setting a series of IP reservations in DHCP, this script may be used to generate a batch file to do the work. This requires a CSV of the mac addresses and IPs to assign.
        .PARAMETER DHCPServer
            Specify the name or IP address of the DHCP server
        .PARAMETER DHCPScope
            Enter the scope you are applying the machines to
        .PARAMETER InputCSV
            Specify the path to the input CSV where the values are coming from
        .PARAMETER OutputCMD
            Specify the file path to save the resulting CMD file
        .INPUTS
            A CSV file with the headers DESCRIPTION,MAC,IP,NAME
        .OUTPUTS
            A CMD file with NETSH commands. This file can be run directly and will result in the IPs being reserved in DHCP
        .EXAMPLE
            Make-DNSReservationCMD.ps1 -DHCPServer 192.168.0.10 -DHCPScope 192.168.10.0 -InputCSV C:\Input.csv -OutputCMD C:\out.cmd
        .NOTES
            FunctionName : Make-DNSReservationCMD
            Created by   : Andrew Paterson
            Date Coded   : 2012-10-16 22:05:58Z
        .LINK
     #>
     
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory=$true,HelpMessage="Enter the DHCP server name",ValueFromPipelineByPropertyName=$true )][String] $DHCPServer = $null,
        [Parameter(Mandatory=$true,HelpMessage="Enter the scope you are applying the machines to",ValueFromPipelineByPropertyName=$true )][String] $DHCPScope = $null,
        [Parameter(Mandatory=$true,HelpMessage="Specify the path to the input CSV where the values are coming from",ValueFromPipelineByPropertyName=$true )][String] $InputCSV = $null,
        [Parameter(Mandatory=$true,HelpMessage="Specify the file path to save the resulting CMD file" )][String] $OutputCMD = $null
    )


# Assumes a CSV with three columns, MAC, IP and NAME.
Import-CSV $InputCSV | ForEach-Object {add-content -Encoding ASCII -Path $OutputCMD -Value "netsh Dhcp Server $DHCPServer Scope $DHCPScope Add reservedip $($_.IP) $($_.MAC) `"$($_.NAME)`" `"$($_.DESCRIPTION)`" `"BOTH`""}

No comments:

Post a Comment