Powershell – Generating a device config from a baseline text file

I like making things easy. 😉   Here’s a simpler one:  Simple Version.

Here’s a method to replace certain strings of text in a text file in order to generate a usable baseline configuration. Great for Cisco, Juniper, Brocade, Aruba, etc.

Start with a simple baseline configuration:
(This is not really a usable config, just an example!!! Don’t try to use it!)

hostname ###HOSTNAME###

interface Vlan###MANAGEMENT-VLAN-NUMBER###
 description MANAGEMENT VLAN
 ip address ###MANAGEMENT-VLAN-IP### ###MANAGEMENT-VLAN-MASK###
 no shutdown

logging source-interface Vlan###MANAGEMENT-VLAN-NUMBER###
ip tacacs source-interface Vlan###MANAGEMENT-VLAN-NUMBER###
snmp-server source-interface informs Vlan###MANAGEMENT-VLAN-NUMBER###
snmp-server trap-source Vlan###MANAGEMENT-VLAN-NUMBER###
ip radius source-interface Vlan###MANAGEMENT-VLAN-NUMBER###

vlan ###MANAGEMENT-VLAN-NUMBER###
 name Management_VLAN
!
vlan ###ACCESS-VLAN-NUMBER###
 name User_VLAN
!
vlan ###VOICE-VLAN-NUMBER###
 name Voice_VLAN
!
!###VLAN-PLACEHOLDER-1###
!
!###VLAN-PLACEHOLDER-2###
!
!###VLAN-PLACEHOLDER-3###
!
!###VLAN-PLACEHOLDER-4###
!
!###VLAN-PLACEHOLDER-5###
!
interface Vlan1
 description CONFIG GENERATED ###DATE### by ###USER###
 shutdown

ip default-gateway ###DEFAULT-GATEWAY###

interface range ###PORTS-1### !###PORTS-2### !###PORTS-3### !###PORTS-4### !###PORTS-5### !###PORTS-6### !###PORTS-7### 
 switchport access vlan ###ACCESS-VLAN-NUMBER###
 switchport voice vlan ###VOICE-VLAN-NUMBER###
 switchport
 switchport host
 no shutdown

default interface range ###TRUNK-PORT### !###TRUNK-PORT2###
interface range ###TRUNK-PORT### !###TRUNK-PORT2###
 !###ETHERCHANNEL-CONFIG###
 !description !--- Automatically populated by EEM with CDP
 switchport trunk allowed vlan ###ALLOWED-VLANS###
 switchport mode trunk
 no shutdown

snmp-server location ###SNMP-LOCATION###
snmp-server contact ###SNMP-CONTACT###

copy running-config startup-config
copy running-config flash:/initial-config.backup

Now use PowerShell to replace the “variables” you created:

# ============================================================================================== 
# 
#
# NAME: Config-GeneratorV2.ps1
# 
# AUTHOR: Mr. Bray Comm Squad
#
# DATE  : 27 Apr 2016
# 
# COMMENT: V2 of this script replaces text in a master baseline config txt.  Much simpler and
#          easier to make config changes. 
#
#
# 
# 
# ============================================================================================== 

# LOCATION OF SCRIPT/BASELINE
$configGenDirectory = "$env:USERPROFILE\Desktop\config_generator"



#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Clear-Host
write-host ""
write-host ""
write-host "         Enter values to generate your config." -ForegroundColor Yellow
write-host "              [Defaults are in brackets]" -ForegroundColor Cyan
write-host ""
write-host ""


#--- Generate Current Date Variable
$date = Get-Date
$date = $date.ToString('MM-dd-yyyy_hh-mm-ss')

#--- Get User Generating Config
$generatingUser = [Environment]::UserName


#-----------------------------------------------------------------------------------------
#--- GET HOSTNAME AND CREATE NEW CONFIG FILE
write-host ""
write-host "        Hostname " -ForegroundColor Green
write-host ""
$hostname = Read-Host -Prompt ' Enter Device Hostname [Bldg0000-Flr0-Rm000_MODEL] '
        if($hostname -eq $null){ $hostname = "Bldg0000-Flr0-Rm000_MODEL" }
        if($hostname -eq ""){ $hostname = "Bldg0000-Flr0-Rm000_MODEL" }

    #--- CREATE NEW CONFIG DIRECTORY AND FILE ON DESKTOP 
        New-Item -Force -ItemType directory -Path "$($env:USERPROFILE)\Desktop\Generated-Configs\" | Out-Null
        #--- SET OUTPUT PATH
            $configOutPath = "$($env:USERPROFILE)\Desktop\Generated-Configs\3850-NIPR-Config-$hostname-$date.txt"
        #--- CREATE NEW CONFIG FILE
            Copy-Item $configGenDirectory/3850-NIPR-Baseline.txt $configOutPath -Force

(Get-Content $configOutPath).replace('###HOSTNAME###', "$hostname") | Set-Content $configOutPath -Force


#-----------------------------------------------------------------------------------------
#--- MANAGMENT VLAN
write-host ""
write-host "        Management VLAN " -ForegroundColor Green
write-host ""

#INSTEAD OF ALLOWING USER TO SPECIFY MANAGEMENT VLAN, HARD SET IT
$mgmtVlan = "10"
#$mgmtVlan = Read-Host -Prompt ' Enter Management VLAN # [10] '
        #if($mgmtVlan -eq $null){ $mgmtVlan = "10" }
        #if($mgmtVlan -eq ""){ $mgmtVlan = "10" }

$mgmtVlanIP = Read-Host -Prompt ' Enter Management VLAN IP [10.0.0.2] '
        if($mgmtVlanIP -eq $null){ $mgmtVlanIP = "10.0.0.2" }
        if($mgmtVlanIP -eq ""){ $mgmtVlanIP = "10.0.0.2" }

$mgmtVlanMASK = Read-Host -Prompt ' Enter Management VLAN MASK [255.255.254.0] '
        if($mgmtVlanMASK -eq $null){ $mgmtVlanMASK = "255.255.254.0" }
        if($mgmtVlanMASK -eq ""){ $mgmtVlanMASK = "255.255.254.0" }
        

(Get-Content $configOutPath).replace('###MANAGEMENT-VLAN-NUMBER###', "$mgmtVlan") | Set-Content $configOutPath -Force
(Get-Content $configOutPath).replace('###MANAGEMENT-VLAN-IP###', "$mgmtVlanIP") | Set-Content $configOutPath -Force
(Get-Content $configOutPath).replace('###MANAGEMENT-VLAN-MASK###', "$mgmtVlanMASK") | Set-Content $configOutPath -Force


#-----------------------------------------------------------------------------------------
#--- VLAN 1 DESCRIPTION WITH DATE AND USER - NO PROMPT

(Get-Content $configOutPath).replace('###DATE###', "$date") | Set-Content $configOutPath -Force
(Get-Content $configOutPath).replace('###USER###', "$generatingUser") | Set-Content $configOutPath -Force


#-----------------------------------------------------------------------------------------
#--- DEFAULT GATEWAY - GENERATED - WITH PROMPT
write-host ""
write-host "        Default Gateway " -ForegroundColor Green
write-host ""

$defaultGateway = Read-Host -Prompt ' Enter the Default Gateway IP [10.0.0.1] '
        if($defaultGateway -eq $null){ $defaultGateway = "10.0.0.1" }
        if($defaultGateway -eq ""){ $defaultGateway = "10.0.0.1" }
        
(Get-Content $configOutPath).replace('###DEFAULT-GATEWAY###', "$defaultGateway") | Set-Content $configOutPath -Force


#-----------------------------------------------------------------------------------------
#--- VLAN CONFIGURATION
write-host ""
write-host "        VLAN Configuration " -ForegroundColor Green
write-host ""
$accessPortVlan = Read-Host -Prompt ' Enter the Data VLAN # '
        if($accessPortVlan -eq $null){ $accessPortVlan = "666" }
        if($accessPortVlan -eq ""){ $accessPortVlan = "666" }
$accessPortVoiceVlan = Read-Host -Prompt ' Enter the Voice VLAN # '
        if($accessPortVoiceVlan -eq $null){ $accessPortVoiceVlan = "666" }
        if($accessPortVoiceVlan -eq ""){ $accessPortVoiceVlan = "666" }

(Get-Content $configOutPath).replace('###ACCESS-VLAN-NUMBER###', "$accessPortVlan") | Set-Content $configOutPath -Force
(Get-Content $configOutPath).replace('###VOICE-VLAN-NUMBER###', "$accessPortVoiceVlan") | Set-Content $configOutPath -Force

    #--- ADDITIONAL VLANS
        write-host ""
        write-host "        Additional VLANs " -ForegroundColor Yellow
        write-host ""

    #--- ADDITIONAL VLAN 1
    $additionalvlan = Read-Host -Prompt ' Configure another VLAN? (y/n) [n] '
            if($additionalvlan -eq $null){ $additionalvlan = "n" }
            if($additionalvlan -eq ""){ $additionalvlan = "n" }
    if ($additionalvlan -eq "y") {
        $vlanNumber = Read-Host -Prompt '  VLAN Number '
                if($vlanNumber -eq $null){ $vlanNumber = "!" }
                if($vlanNumber -eq ""){ $vlanNumber = "!" }
        $vlanDescription = Read-Host -Prompt '  VLAN Description (no spaces) '
                if($vlanDescription -eq $null){ $vlanDescription = "!" }
                if($vlanDescription -eq ""){ $vlanDescription = "!" }

$additionalvlantext = @"
vlan $vlanNumber
name $vlanDescription
"@

        (Get-Content $configOutPath).replace('!###VLAN-PLACEHOLDER-1###', "$additionalvlantext") | Set-Content $configOutPath -Force

    }

    #--- ADDITIONAL VLAN 2
    if ($additionalvlan -eq "y") {
        write-host ""
        $additionalvlan = Read-Host -Prompt ' Configure another VLAN? (y/n) [n] '
                if($additionalvlan -eq $null){ $additionalvlan = "n" }
                if($additionalvlan -eq ""){ $additionalvlan = "n" }
        if ($additionalvlan -eq "y") {
            $vlanNumber = Read-Host -Prompt '  VLAN Number '
                    if($vlanNumber -eq $null){ $vlanNumber = "!" }
                    if($vlanNumber -eq ""){ $vlanNumber = "!" }
            $vlanDescription = Read-Host -Prompt '  VLAN Description (no spaces) '
                    if($vlanDescription -eq $null){ $vlanDescription = "!" }
                    if($vlanDescription -eq ""){ $vlanDescription = "!" }

$additionalvlantext = @"
vlan $vlanNumber
 name $vlanDescription
"@

            (Get-Content $configOutPath).replace('!###VLAN-PLACEHOLDER-2###', "$additionalvlantext") | Set-Content $configOutPath -Force

        }
    }

    #--- ADDITIONAL VLAN 3
    if ($additionalvlan -eq "y") {
        write-host ""
        $additionalvlan = Read-Host -Prompt ' Configure another VLAN? (y/n) [n] '
                if($additionalvlan -eq $null){ $additionalvlan = "n" }
                if($additionalvlan -eq ""){ $additionalvlan = "n" }
        if ($additionalvlan -eq "y") {
            $vlanNumber = Read-Host -Prompt '  VLAN Number '
                    if($vlanNumber -eq $null){ $vlanNumber = "!" }
                    if($vlanNumber -eq ""){ $vlanNumber = "!" }
            $vlanDescription = Read-Host -Prompt '  VLAN Description (no spaces) '
                    if($vlanDescription -eq $null){ $vlanDescription = "!" }
                    if($vlanDescription -eq ""){ $vlanDescription = "!" }

$additionalvlantext = @"
vlan $vlanNumber
 name $vlanDescription
"@

            (Get-Content $configOutPath).replace('!###VLAN-PLACEHOLDER-3###', "$additionalvlantext") | Set-Content $configOutPath -Force

        }
    }

    #--- ADDITIONAL VLAN 4
    if ($additionalvlan -eq "y") {
        write-host ""
        $additionalvlan = Read-Host -Prompt ' Configure another VLAN? (y/n) [n] '
                if($additionalvlan -eq $null){ $additionalvlan = "n" }
                if($additionalvlan -eq ""){ $additionalvlan = "n" }
        if ($additionalvlan -eq "y") {
            $vlanNumber = Read-Host -Prompt '  VLAN Number '
                    if($vlanNumber -eq $null){ $vlanNumber = "!" }
                    if($vlanNumber -eq ""){ $vlanNumber = "!" }
            $vlanDescription = Read-Host -Prompt '  VLAN Description (no spaces) '
                    if($vlanDescription -eq $null){ $vlanDescription = "!" }
                    if($vlanDescription -eq ""){ $vlanDescription = "!" }

$additionalvlantext = @"
vlan $vlanNumber
 name $vlanDescription
"@

            (Get-Content $configOutPath).replace('!###VLAN-PLACEHOLDER-4###', "$additionalvlantext") | Set-Content $configOutPath -Force

        }
    }

    #--- ADDITIONAL VLAN 5
    if ($additionalvlan -eq "y") {
        write-host ""
        $additionalvlan = Read-Host -Prompt ' Configure another VLAN? (y/n) [n] '
                if($additionalvlan -eq $null){ $additionalvlan = "n" }
                if($additionalvlan -eq ""){ $additionalvlan = "n" }
        if ($additionalvlan -eq "y") {
            $vlanNumber = Read-Host -Prompt '  VLAN Number '
                    if($vlanNumber -eq $null){ $vlanNumber = "!" }
                    if($vlanNumber -eq ""){ $vlanNumber = "!" }
            $vlanDescription = Read-Host -Prompt '  VLAN Description (no spaces) '
                    if($vlanDescription -eq $null){ $vlanDescription = "!" }
                    if($vlanDescription -eq ""){ $vlanDescription = "!" }

$additionalvlantext = @"
vlan $vlanNumber
 name $vlanDescription
"@

            (Get-Content $configOutPath).replace('!###VLAN-PLACEHOLDER-5###', "$additionalvlantext") | Set-Content $configOutPath -Force

        }
    }


#-----------------------------------------------------------------------------------------
#--- ACCESS PORT CONFIGURATION
write-host ""
write-host "        Access Port Configuration " -ForegroundColor Green
write-host ""
$accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 1 (24 or 48) [48] '
        if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
        if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
$switchAccessPorts = "Gi1/0/1 - $accessPortsHowMany"

(Get-Content $configOutPath).replace('###PORTS-1###', "$switchAccessPorts") | Set-Content $configOutPath -Force


    #--- ADDITIONAL STACKED SWITCHES - 3750-X SWITCH 2
    write-host ""
    write-host "        Additional Stacked Switches " -ForegroundColor Yellow
    write-host ""
    $3750xStack = Read-Host -Prompt ' Configure Switch 2 (stacked)? [N] '
            if($3750xStack -eq $null){ $3750xStack = "n" }
            if($3750xStack -eq ""){ $3750xStack = "n" }
    if ($3750xStack -eq "y") {
        $accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 2 (24 or 48) [48] '
                if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
                if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
        $switchAccessPorts = ", Gi2/0/1 - $accessPortsHowMany"

        (Get-Content $configOutPath).replace('!###PORTS-2###', "$switchAccessPorts") | Set-Content $configOutPath -Force
    }

    #--- ADDITIONAL STACKED SWITCHES - 3750-X SWITCH 3
    if ($3750xStack -eq "y") {
        write-host ""
        $3750xStack = Read-Host -Prompt ' Configure Switch 3 (stacked)? [N] '
                if($3750xStack -eq $null){ $3750xStack = "n" }
                if($3750xStack -eq ""){ $3750xStack = "n" }
        if ($3750xStack -eq "y") {
            $accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 3 (24 or 48) [48] '
                    if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
                    if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
            $switchAccessPorts = ". Gi3/0/1 - $accessPortsHowMany"

            (Get-Content $configOutPath).replace('!###PORTS-3###', "$switchAccessPorts") | Set-Content $configOutPath -Force
        }
    }

    #--- ADDITIONAL STACKED SWITCHES - 3750-X SWITCH 4
    if ($3750xStack -eq "y") {
        write-host ""
        $3750xStack = Read-Host -Prompt ' Configure Switch 4 (stacked)? [N] '
                if($3750xStack -eq $null){ $3750xStack = "n" }
                if($3750xStack -eq ""){ $3750xStack = "n" }
        if ($3750xStack -eq "y") {
            $accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 4 (24 or 48) [48] '
                    if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
                    if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
            $switchAccessPorts = ", Gi4/0/1 - $accessPortsHowMany"

            (Get-Content $configOutPath).replace('!###PORTS-4###', "$switchAccessPorts") | Set-Content $configOutPath -Force
        }
    }


    #--- ADDITIONAL STACKED SWITCHES - 3750-X SWITCH 5
    if  ($3750xStack -eq "y") {
        write-host ""
        $3750xStack = Read-Host -Prompt ' Configure Switch 5 (stacked)? [N] '
                if($3750xStack -eq $null){ $3750xStack = "n" }
                if($3750xStack -eq ""){ $3750xStack = "n" }
        if ($3750xStack -eq "y") {
            $accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 5 (24 or 48) [48] '
                    if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
                    if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
            $switchAccessPorts = ", Gi5/0/1 - $accessPortsHowMany"

            (Get-Content $configOutPath).replace('!###PORTS-5###', "$switchAccessPorts") | Set-Content $configOutPath -Force
        }
    }

    #--- ADDITIONAL STACKED SWITCHES - 3750-X SWITCH 6
    if  ($3750xStack -eq "y") {
        write-host ""
        $3750xStack = Read-Host -Prompt ' Configure Switch 6 (stacked)? [N] '
                if($3750xStack -eq $null){ $3750xStack = "n" }
                if($3750xStack -eq ""){ $3750xStack = "n" }
        if ($3750xStack -eq "y") {
            $accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 6 (24 or 48) [48] '
                    if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
                    if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
            $switchAccessPorts = ", Gi6/0/1 - $accessPortsHowMany"

            (Get-Content $configOutPath).replace('!###PORTS-6###', "$switchAccessPorts") | Set-Content $configOutPath -Force
        }
    }

    #--- ADDITIONAL STACKED SWITCHES - 3750-X SWITCH 7 
    if ($3750xStack -eq "y") {
        write-host ""
        $3750xStack = Read-Host -Prompt ' Configure Switch 7 (stacked)? [N] '
                if($3750xStack -eq $null){ $3750xStack = "n" }
                if($3750xStack -eq ""){ $3750xStack = "n" }
        if ($3750xStack -eq "y") {
            $accessPortsHowMany = Read-Host -Prompt '  Number of Access Ports on Switch 7 (24 or 48) [48] '
                    if($accessPortsHowMany -eq $null){ $accessPortsHowMany = "48" }
                    if($accessPortsHowMany -eq ""){ $accessPortsHowMany = "48" }
            $switchAccessPorts = ", Gi7/0/1 - $accessPortsHowMany"

            (Get-Content $configOutPath).replace('!###PORTS-7###', "$switchAccessPorts") | Set-Content $configOutPath -Force
        }
    }


#-----------------------------------------------------------------------------------------
#--- TRUNK PORT CONFIGURATION
write-host ""
write-host "        Trunk Port Configuration " -ForegroundColor Green
write-host ""

$trunkPortNumber = Read-Host -Prompt ' Enter Trunk Port Interface [Gi1/1/1] '
        if($trunkPortNumber -eq $null){ $trunkPortNumber = "Gi1/1/1" }
        if($trunkPortNumber -eq ""){ $trunkPortNumber = "Gi1/1/1" }

Write-Host " Enter allowed VLANs separated by a comma [$mgmtVlan,$accessPortVlan,$accessPortVoiceVlan] " -NoNewline

$trunkPortAllowedVlans = Read-Host 
       if($trunkPortAllowedVlans -eq $null){ $trunkPortAllowedVlans = "$mgmtVlan,$accessPortVlan,$accessPortVoiceVlan" }
       if($trunkPortAllowedVlans -eq ""){ $trunkPortAllowedVlans = "$mgmtVlan,$accessPortVlan,$accessPortVoiceVlan" }

(Get-Content $configOutPath).replace('###TRUNK-PORT###', "$trunkPortNumber") | Set-Content $configOutPath -Force
(Get-Content $configOutPath).replace('###ALLOWED-VLANS###', "$trunkPortAllowedVlans") | Set-Content $configOutPath -Force

    #--- ETHERCHANNEL CONFIG
    write-host ""
    write-host "        EtherChannel Configuration " -ForegroundColor Yellow
    write-host ""
    $configureEtherchannel = Read-Host -Prompt ' Configure EtherChannel? (y/n) [n] '
            if($configureEtherchannel -eq $null){ $configureEtherchannel = "n" }
            if($configureEtherchannel -eq ""){ $configureEtherchannel = "n" }
    if ($configureEtherchannel -eq "y") {
        $trunkPortNumber2 = Read-Host -Prompt '  Enter Second Trunk Port Interface [Gi1/1/2] '
                if($trunkPortNumber2 -eq $null){ $trunkPortNumber2 = "Gi1/1/2" }
                if($trunkPortNumber2 -eq ""){ $trunkPortNumber2 = "Gi1/1/2" }

$etherchannelInsert = @"
 channel-group 1 mode on

interface Port-Channel 1
"@

$trunkPortNumber2insert = ", $trunkPortNumber2"

        (Get-Content $configOutPath).replace('!###ETHERCHANNEL-CONFIG###', "$etherchannelInsert") | Set-Content $configOutPath -Force
        (Get-Content $configOutPath).replace('!###TRUNK-PORT2###', "$trunkPortNumber2insert") | Set-Content $configOutPath -Force

    }


#-----------------------------------------------------------------------------------------
#--- SNMP INFORMATION - GENERATED - WITH PROMPT
write-host ""
write-host "        SNMP Information " -ForegroundColor Green
write-host ""


        #--- SNMP LOCATION
        Write-Host " Describe the location for a 911 Operator"
        $snmpLocation = Read-Host -Prompt '  (Example: Bldg 123 3FL Rm100 Secret Squirrel Facility) '
                if($snmpLocation -eq $null){ $snmpLocation = "Get $generatingUser to enter this" }
                if($snmpLocation -eq ""){ $snmpLocation = "Get $generatingUser to enter this" }
        (Get-Content $configOutPath).replace('###SNMP-LOCATION###', "$snmpLocation") | Set-Content $configOutPath -Force
        Write-Host ""

        #--- SNMP CONTACT
        Write-Host " Describe the Organization or Entity and include a contact number"
        $snmpContact = Read-Host -Prompt '  (Example: Secret Squirrel Facility 555-1337) '
                if($snmpContact -eq $null){ $snmpContact = "Get $generatingUser to enter this" }
                if($snmpContact -eq ""){ $snmpContact = "Get $generatingUser to enter this" }
        (Get-Content $configOutPath).replace('###SNMP-CONTACT###', "$snmpContact") | Set-Content $configOutPath -Force








#=========================================================================================
#=========================================================================================
#--- OPEN CONFIG FILE
Invoke-Item $configOutPath

See a simpler version HERE

Leave a Reply

Your email address will not be published. Required fields are marked *