Powershell – Simple find/replace configuration script

Keeping it simple with this script to replace a string in a text file:

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

#--- Config Location
$fileConfig = "$($env:USERPROFILE)\Desktop\config.docx"

#--- Output Folder Local
$folderOutLocal = "$($env:USERPROFILE)\Desktop\Generated-Configs\"
#--- Output Folder Public
$folderOutPublic = "$($env:USERPROFILE)\Desktop\Public\"

#--- Create output folders if they don't exist 
New-Item -Force -ItemType directory -Path $folderOutLocal | Out-Null
New-Item -Force -ItemType directory -Path $folderOutPublic | Out-Null

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

#----------------------------------------------
#-------------Start Functions------------------
Function OpenWordDoc($Filename)
{
$Word=NEW-Object –comobject Word.Application
Return $Word.documents.open($Filename)
}

Function ReplaceAWord($Document,$findtext,$replacewithtext)
{ 
    #https://docs.microsoft.com/en-us/office/vba/api/word.find.execute
  $FindReplace=$Document.ActiveWindow.Selection.Find
  $matchCase = $true;
  $matchWholeWord = $true;
  $matchWildCards = $false;
  $matchSoundsLike = $false;
  $matchAllWordForms = $false;
  $forward = $true;
  $format = $false;
  $matchKashida = $false;
  $matchDiacritics = $false;
  $matchAlefHamza = $false;
  $matchControl = $false;
  $read_only = $false;
  $visible = $true;
  $replace = 2;
  $wrap = 1;
  $FindReplace.Execute($findText, $matchCase, $matchWholeWord, $matchWildCards, $matchSoundsLike, $matchAllWordForms, $forward, $wrap, $format, $replaceWithText, $replace, $matchKashida ,$matchDiacritics, $matchAlefHamza, $matchControl) | Out-Null
}

Function SaveAsWordDoc($Document,$FileName)
{
$Document.Saveas([REF]$Filename)
$Document.close()
}

Function PromptAndReplace($Document,$ReadHostPrompt,$FindText,$DefaultValue)
{
$ReplaceText = Read-Host -Prompt "$ReadHostPrompt [$DefaultValue]"
        if($ReplaceText -eq $null){ $ReplaceText = $DefaultValue }
        if($ReplaceText -eq ""){ $ReplaceText = $DefaultValue }
ReplaceAWord -Document $Document -findtext $FindText -replacewithtext $ReplaceText
}
#-------------End Functions------------------
#--------------------------------------------

#--- Open the document
$CurrentDoc = OpenWordDoc -Filename $fileConfig

#--- Start replacing

#--- Get hostname and also create new file
#--- HOSTNAME
$readHostPrompt = "Hostname"
$findText = "###HOSTNAME###"
$defaultValue = "c3850-xxx-xxx"
$ReplaceText = Read-Host -Prompt "$ReadHostPrompt [$DefaultValue]"
        if($ReplaceText -eq $null){ $ReplaceText = $DefaultValue }
        if($ReplaceText -eq ""){ $ReplaceText = $DefaultValue }

SaveAsWordDoc -Document $CurrentDoc -FileName "$folderOutLocal\Config-$hostname-$date-$generatingUser.docx"

ReplaceAWord -Document $CurrentDoc -findtext $FindText -replacewithtext $ReplaceText




#--- MANAGEMENT_VLAN_NUMBER
$readHostPrompt = "Management VLAN Number"
$findText = "###MANAGEMENT_VLAN_NUMBER###"
$defaultValue = "xxx"
PromptAndReplace -Document $CurrentDoc -ReadHostPrompt $readHostPrompt -FindText $findText -DefaultValue $defaultValue

#--- Save the document
SaveAsWordDoc -Document $CurrentDoc -FileName "$folderOutLocal\Config-$hostname-$date.docx"

 

Leave a Reply

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