Search code examples
powershellfile-get-contentsget-childitem

Power Shell Get-ChildItem then pipe to Get-Content


I am trying to retrieve the unknown file path of a known filename and then do a Get-Content on that filename so that I can modify it. I know that the file will be in C:\Users\$env:UserName\ but thats as far as I can go (the end user may have it in further in, like Documents or Desktop or somewhere else).

It seems to can do either a Get-ChildItem or a Get-Content but for some reason I can't do both in the same script.

Overall goal: Pipe the results of a Get-ChildItem to the Get-Contents function so that I can modify the file.

Here's my code:

(Get-ChildItem -Path C:\Users\$env:UserName -Filter QA11test.hep) | 
    Foreach-Object {
        $_ # send the current line to output
        if($_ -match "SSL/TLS User Cipher Suites=DEFAULT"){
            "IP Port=2023"
            "Security Option=2"
            $Message = "Your host explorer session has been updated."
            Msg $env:UserName $Message
            exit
        } else {
            $Message = "Script Failed."
            Msg $env:UserName $Message
            exit
        }
    } | Set-Content C:\Users\$env:UserName\Telnet\QA11TestSecure.hep

Need help! Thank you!


Solution

  • Thank you everyone who helped!

    (@mklement0, I'm going to modify this to do something close to what your last post does! That helps me a lot.)

    I took bits and pieces from different posts and this is what I came up with that does what I need it to do.

    This PowerShell script grabs the full path of a known file, "QA11test.hep" and loads it into a variable for later use. It then searches the contents of that file for a specific string "SSL/TLS..etc" and if it finds it, it adds two lines, "Port=2023" and "Security Option 2" below the line it found. Then it takes that new file and adds it to C:\Users\$env:UserName\Desktop\QA11TestSecure.hep without changing the original QA11test.hep file. So instead of piping from get-childitem to get-content, it puts the item retrieved from the get-childitem function into a variable that the get-content function can then search for. Works great!

    $FileToLookAt = (Get-ChildItem -Path C:\Users\$env:UserName\ -Recurse -Filter "QA11test.hep") | % { $_.FullName }
    $User = $env:UserName
    if ($FileToLookAt){
        $updated = $false
        $addtls = Get-Content ($FileToLookAt) | Foreach-Object {
            $_ # send the current line to output
            if($_ -match "SSL/TLS User Cipher Suites=DEFAULT"){
                "IP Port=2023"
                "Security Option=2"
                $updated = $true
            }
        }
        if ($updated) {
            $addtls | Set-Content -Path C:\Users\$env:UserName\Desktop\QA11TestSecure.hep
            $Message = "Host Explorer updated.`nPlease ensure there is a gold LOCK icon at the bottom left `nof the host explorer window.`nIf not, please contact the help center. Thank you."
        } else {
            $message = "File was not updated.`nPlease contant the help center immediatly."
        }
    } else {
        $Message = "QA11test.hep not found.`nPlease contact the helpdesk immediately."
    }
    Msg $env:UserName $Message
    exit