Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function format-conf
- {
- PARAM
- (
- $in
- )
- [string]$return = ""
- $line = $in.split('#')[-1].trim()
- if ($line -match "show" -or $line -match 'ping')
- {
- $Return += $line.tostring().insert(0,"do ").trim()
- }
- else
- {
- $Return += $line.trim()
- }
- if ($line -eq 'exit' -or $line -eq 'no shutdown')
- {
- $Return += "`r`n"
- }
- if ($line -match "\?")
- {
- $return = ""
- }
- return $return
- }
- while ($true)
- {
- clear
- #-----------------------------------+
- # Get info from conf from word file |
- #-----------------------------------+
- $file = Get-ChildItem -Path "$($env:USERPROFILE)\Downloads" -Filter CCNP*.doc*
- $list = @()
- for ($i = 0; $i -lt $file.count;$i++)
- {
- $prob = @{
- 'ID' = [int]$i;
- 'File' = $file[$i].BaseName
- 'Dir' = $file[$i].FullName
- }
- $obj = New-Object -TypeName Psobject -Property $prob
- $list += $obj
- }
- Write-Host -ForegroundColor Yellow "Press q to exit`n`r"
- Write-Host -ForegroundColor DarkGray "ID File"
- Write-Host -ForegroundColor DarkGray "-- ----"
- for ($i = 0; $i -lt $list.Count;$i++)
- {
- if ($i % 2)
- {
- Write-Host -ForegroundColor darkgray "$($list[$i].ID.tostring("00")) :: $($list[$i].File)"
- }
- else
- {
- Write-Host "$($list[$i].ID.tostring("00")) :: $($list[$i].File)"
- }
- }
- $l = 0.. ($list.Length - 1)
- $l += 'q'
- $choice = $null
- do
- {
- $choice = Read-Host "Vælg fil"
- } while ($l -notcontains $choice -or $choice -eq "")
- if ($choice -ne 'q')
- {
- $file = $list[$choice].dir
- Write-Host -ForegroundColor Yellow "Building conf from $($list[$choice].Dir)"
- $Word = New-Object -ComObject Word.application
- $doc = $Word.Documents.Open($file)
- $conf = $doc.Paragraphs | foreach {$_.range.text}
- foreach ($line in $conf)
- {
- [regex]::Replace($line,"^\W.?","") | Out-Null
- }
- $conf = $conf | where {($_ -match "#" -or [regex]::Match($_,"Step").success -or [regex]::Match($_,"Chapter").success) -and $_ -notmatch "Seq#"}
- $doc.Close()
- $devices = @()
- $basic = @(" ","enable","conf t")
- #--------------+
- #Build Devices |
- #--------------+
- foreach ($line in $conf)
- {
- if ($line -ne " ")
- {
- if ([regex]::Match($line,".*?(?=\()"))
- {
- if (!($devices.contains([regex]::Match($line,".*?(?=\()").value)))
- {
- $devices += [regex]::Match($line,".*?(?=\()").value.tostring().trim()
- }
- }
- if ([regex]::Match($line,".*?(?=\#)"))
- {
- if(!($devices.Contains([regex]::Match($line,".*?(?=\#)").value)))
- {
- $devices += [regex]::Match($line,".*?(?=\#)").value.tostring().trim()
- }
- }
- }
- }
- $devices = $devices | where {$_ -ne "" -and $_ -ne 'Router' -and [regex]::Match($_,"^[a-zA-Z0-9_.-]*$") -and $_ -notmatch 'config'} | Sort-Object
- #---------------+
- # Build Objects |
- #---------------+
- $Cisco_dev = @()
- foreach ($device in $devices)
- {
- $prob = @{
- 'Device' = $device;
- 'Text' = $null
- }
- $obj = New-Object -TypeName Psobject -Property $prob
- $Cisco_dev += $obj
- }
- #----------------------------------------+
- # Create Conf in text proberty of object |
- #----------------------------------------+
- foreach ($item in $Cisco_dev)
- {
- foreach ($line in $conf)
- {
- # Add Basic Conf content and Hostname Based on device
- if ([regex]::Match($line,"Chapter").success)
- {
- $item.Text += [array]"!$($line.trim())"
- $item.Text += $basic + "`r`nHostname $($item.Device)"
- $chapter = $line.trim()
- }
- }
- foreach ($line in $conf | where {[regex]::Match($_,"^$($item.Device)").success -or [regex]::Match($_,"Router").success -or [regex]::Match($_,"Step").success})
- {
- # ADD Chapter Descriptions and Conf
- if ([regex]::Match($line,"Step").success)
- {
- $border = ""
- for($i = 0; $i -lt $line.Length + 1;$i++)
- {
- $border += [string]"-"
- }
- $item.Text += "`r`n`r`n" + "!" + $border + "+"
- $item.Text += "! " + $line.trim() + " |"
- $item.Text += "!" + $border + "+"
- }
- # Add Configurations to conf file
- else
- {
- $item.Text += format-conf -in $line
- }
- }
- }
- #-----------------+
- # Out file to txt |
- #-----------------+
- $path = "$($env:USERPROFILE)\desktop\CCNP\$chapter"
- if (!(Test-Path $path))
- {
- New-Item -Path $path -ItemType Directory | Out-Null
- }
- foreach ($item in $Cisco_dev)
- {
- $item.Text | where {$_ -ne ""} | Out-File -FilePath "$path\$($item.Device).txt" -Force
- }
- }
- else
- {
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement