Suggest Modify-Textfile function

//Suggest Modify-Textfile function
Suggest Modify-Textfile function 2015-05-14T03:56:59+00:00

The Toolkit Forums Deployment Scripts Suggest Modify-Textfile function

  • Author
    Posts
  • macxcool
    Participant
    Post count: 23
    #225 |

    I’ve created a Function called Modify-Textfile that allows one to add or remove a line from a file based on complete or partial match. It also handles removing a single or multiple lines.
    Is this something that might be useful to others? Are there any suggestions for improvements?

    #region Function Modify-TextFile
    Function Modify-TextFile {
    <#
    .SYNOPSIS
        Adds a line or removes a line from a text file.
    .DESCRIPTION
        Add a line to the end of a text file, or finds and removes a line from a text file.
    .PARAMETER Path
        Path to the text file
    .PARAMETER Action
        Sets which action the function will perform. Options: AddLine, RemoveLine
    .PARAMETER Text
        Text to either Add to end of file or Search for and remove the first line that contains it.
    .PARAMETER Partial
        If Action is RemoveLine. If True, searches for part of the line to remove, If False, searches for entire line. Default $false
    .PARAMETER Multiple
        If Action is RemoveLine. If True, will remove all lines with Text. If False, removes only first matched line. Default $false
    .EXAMPLE
        Modify-TextFile -Path 'C:\Temp\TestFile.txt' -Action AddLine -Text 'This is some text to append'
    .EXAMPLE
        Modify-TextFile -Path "$envWinDir\System32\TextFile.log" -Action RemoveLine -Text 'Installation' -Multiple $true
    .NOTES
        Created by tv45057 for the TVDSB Deployment team
    #>
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory=$true,HelpMessage='Please enter the complete path to the text file')]
            [ValidateNotNullorEmpty()]
            [string]$Path,
            [Parameter(Mandatory=$true)]
            [Validateset('AddLine','RemoveLine')]
            [string]$Action,
            [Parameter(Mandatory=$true)]
            [ValidateNotNullorEmpty()]
            [string]$Text,
            [Parameter(Mandatory=$false)]
            [ValidateNotNullorEmpty()]
            [boolean]$Multiple = $false,
            [Parameter(Mandatory=$false)]
            [ValidateNotNullorEmpty()]
            [boolean]$Partial = $false
        )
    
        Begin {
            ## Get the name of this function and write header
            [string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
            Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -CmdletBoundParameters $PSBoundParameters -Header
        }
        Process {
            Try {
                # Check to see if text file exists
                If (Test-Path -Path $Path -PathType Leaf) {
                    If ($Action -eq 'AddLine') {
                        Write-Log -Message "Append text ""$text"" to [$path]..." -Source {$CmdletName}
                        # Begin process of adding a line to the end of the file. We're assuming a Windows-formatted file right now
                        #Add-Content -Path $Path -Value "<code>r</code>n$Text"
                        Add-Content -Path $Path -Value "$Text"
                    }
                    ElseIf ($Action -eq 'RemoveLine') {
                        Write-Log -Message "Remove text ""[$text]"" from [$path]..." -Source {$CmdletName}
                        # Begin the process of finding and removing a line/lines from the file
                        $TempPath = $Path + '.temp'
                        # Make backup of text file - remove current backup if it exists
                        $BackupPath = $Path + '.backup'
                        If (Test-Path -Path $BackupPath -PathType Leaf) {
                            Remove-File -Path $BackupPath
                        }
                        Copy-File -Path $Path -Destination $BackupPath
    
                        If ($Multiple -eq $true) {
                            # Remove lines with multiple instances of text from file
                            If ($Partial -eq $true) {
                                # Remove multiple lines with instances of partial string
                                Get-Content $Path | Where-Object {$_ -notmatch $Text} | Set-Content $TempPath
                            }
                            ElseIf ($Partial -eq $false) {
                                # Remove multiple lines with complete string match
                                Get-Content $Path | Where-Object {$_ -notlike $Text} | Set-Content $TempPath
                            }
                        }
                        ElseIf ($Multiple -eq $false) {
                            # Remove single line from text file
                            If ($Partial -eq $true) {
                                # Remove single line with partial string match
                                $intLine = (Get-Content $Path | Where-Object {$_ -match $Text} | Select-Object -First 1).ReadCount                            
                                Write-Host $intLine
                            }
                            ElseIf ($Partial -eq $false) {
                                # Remove single line with complete string match
                                $intLine = (Get-Content $Path | Where-Object {$_ -like $Text} | Select-Object -First 1).ReadCount
                                Write-Host $intLine
                            }
                            If ($intLine -gt 0) {
                                $fileContents = Get-Content $Path
                                $fileContents -replace $fileContents[$intLine - 1],"" | Set-Content $TempPath
                            }
                            Else {
                                # No matchine line
                                Write-Log -Message "[$text] not found in [$path]"
                            
                            }
                        }
                    }
                    # Remove original File and rename Temp file to match
                    If (Test-Path -Path $TempPath -PathType Leaf) {
                        Remove-File -Path $Path
                        Rename-Item -Path $TempPath -NewName $Path
                    }
                }
                Else {
                    # There is no such file - abort function
                    Write-Log -Message "File [$Path] does not exist..." -Source ${CmdletName}
                }
            }
            Catch {
                Write-Log -Message "Failed to modify the text file [$path]. `
    
    n$(Resolve-Error)" -Severity 3 -Source ${CmdletName}
            }
        }
        End {
            Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer
        }
    }
    #endregion
    • This topic was modified 2 years, 6 months ago by  macxcool.
    • This topic was modified 2 years, 6 months ago by  Muhammad Mashwani.
  • macxcool
    Participant
    Post count: 23

    I’m not sure how to get everything after the ‘newline’ in line 118 to stay in the ‘code’ section.

  • Muhammad Mashwani
    Keymaster
    Post count: 118

    Thanks for sharing. I edited the post for you to make all of the code syntax highlighted. You can use the the “crayon” button in the text box to bring up a window that allows you to paste code which will then all be included as syntax highlighted coded.

    Also, this function you’re sharing with the community should really be posted under the “Toolkit Extension” forum.

    Now for suggestions about the code.
    1) Instead of naming the function with the “Modify” verb, I would use one of the approved verbs for naming functions (probably should be “Update” in this case). Microsoft has the list here and a description of each: https://msdn.microsoft.com/en-us/library/ms714428%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

    2) For the catch block in your Try/Catch to actually catch any errors, you will have to add “-ErrorAction ‘Stop'” to the end of every cmdlet you call. Otherwise, the default PowerShell behavior is to display the error and continue.

    Other then that, I think it looks great.

  • Husnu
    Participant
    Post count: 5

You must be logged in to reply to this topic.