How do I consolidate 3x Get-Content commands ?

This topic contains 5 replies, has 5 voices, and was last updated by  Christian Sandfeld 1 week, 1 day ago.

  • Author
    Posts
  • #98407

    JohnDav
    Participant

    Hi Team,
    Struggling learner here.

    What is the best method to consolidate the following 3 almost identical commands into something more lean?
    I want to test for the existence of a 3 different strings in a text file and if they do not exist, add them.

    Thanks very much for any guidance.

     # Set variable to test for existence of local file #
    $a = Test-Path C:\Windows\Sun\Java\Deployment\exception.sites
    
    # Test for existence of sting, if false append to file #
    IF (Get-Content C:\Windows\Sun\Java\Deployment\exception.sites | Select-String FILE:12345) 
    {return $true}
        ELSE 
        {
        Add-Content C:\Windows\Sun\Java\Deployment\exception.sites "`r`FILE:12345"
        }
    
    # Test for existence of sting, if false append to file #
    IF (Get-Content C:\Windows\Sun\Java\Deployment\exception.sites | Select-String http://abcde) 
    {return $true}
        ELSE 
        {
        Add-Content C:\Windows\Sun\Java\Deployment\exception.sites "`r`http://abcde"
        }
    
    # Test for existence of sting, if false append to file #
    IF (Get-Content C:\Windows\Sun\Java\Deployment\exception.sites | Select-String http://qrstu) 
    {return $true}
        ELSE 
        {
        Add-Content C:\Windows\Sun\Java\Deployment\exception.sites "`r`http://qrstu"
        }
    
    # Test for existence of sting, if false append to file #
    IF (Get-Content C:\Windows\Sun\Java\Deployment\exception.sites | Select-String http://tyuio) 
    {return $true}
        ELSE 
        {
        Add-Content C:\Windows\Sun\Java\Deployment\exception.sites "`r`http://tyuio"
        } 
    
    # If variable returns False, copy file from SYSVOL to destinaton path #
    IF ($a = "False") 
        {
        copy-item -Path \\ABC\sysvol\ABC.corp\scripts -Destination C:\Windows\Sun\Java\Deployment
        } 
  • #98413

    Joel Sallow
    Participant

    Do the Get-Content once, then do your testing for different strings:

    $FileContents = Get-Content -Path $Path
    $Pattern1 = "FILE:12345"
    
    if ($FileContents | Select-String $Pattern1) {
        $true
    }
    else {
        "`r`n$Pattern1"
    }
    # etc...

    I have one question. Is the intention for this to simply skip the latter two if the first is present?

    If not, you need to either remove return or replace it with Write-Output. 'return' indicates to PS that this section of code is "over" and will be skipped, and all previously output values will be returned, along with the specified return value.

    If you want it to go through and check everything regardless of the first or second results, you need to avoid using return here. 🙂

  • #98416

    JohnDav
    Participant

    Thanks for the reply.
    The intent is to check the file for 3 different stings and if any of then are missing, add them to the file.

  • #98425

    L-Bo
    Participant

    I would expand on Joel Sallow's suggestion and go a step further with a switch statement. I think you will find the -RegEx parameter particularly interesting for your situation. See below for a quick example.

    $filePath = 'C:\temp\test.txt'
    $logPath = 'C:\temp\test_exceptions.txt'
    switch -RegEx (get-content -Path $filePath)
    {
        'FILE:12345' {Out-File -FilePath $logPath -InputObject $_ -Append}
        'http://abcde' {Out-File -FilePath $logPath -InputObject $_ -Append}
        'http://qrstu' {Out-File -FilePath $logPath -InputObject $_ -Append}
    }
    
  • #98433

    postanote
    Participant

    Why would not a simple ForLoop work for your use case.

    # Test for existence of sting, if false append to file #
    'FILE:12345','http://abcde','http://qrstu','http://tyuio' | %{ 
        IF (Get-Content C:\Windows\Sun\Java\Deployment\exception.sites | Select-String $_) 
        {return $true}
        ELSE 
        {Add-Content C:\Windows\Sun\Java\Deployment\exception.sites "`r $_"}
    }
    
  • #98499

    Christian Sandfeld
    Participant

    Hi John,

    Here is my take on it, with comments explaining what I do in each step.
    Hope that helps you out 🙂

    # Path to  exception file
    $exceptionList = 'C:\Windows\Sun\Java\Deployment\exception.sites'
    
    # array of exceptions we want to ensure is in that file
    $mandatoryExceptions = 'FILE:12345', 'http://abcde', 'http://qrstu', 'http://tyuio'
    
    # read the current data from the file
    $exceptionListData = Get-Content -Path $exceptionList
    
    # Loop through the mandatory exceptions
    foreach ($exception in $mandatoryExceptions) {
    
        # If the mandatory exception is not in the data we read from the file, add it to the file
        if ($exceptionListData -notcontains $exception) {
            $exception | Add-Content -Path $exceptionList -Confirm:$false -Force
        }
    
    }
    

You must be logged in to reply to this topic.