1

I have an issue with a Firefox module. To resolve the issue I need to append text to a text file that is found under each user's firefox profile.

I can copy the new text file to each users profile using the below powershell script:

    # Check to see if AWP is actually installed

    $chkAWPExists = "C:\Program Files\Middleware Directory\Middleware"
    $aWPExists = Test-Path $chkAWPExists

    # If AWP Exists copy the pkcs11.txt file to all Firefox Profiles found.

    If ($aWPExists -eq $True) {
        Write-Log "AWP Exists. Copying pkcs11.txt to all firefox profiles"
        Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt' |
        ForEach-Object {
            Copy-Item -Path 'pkcs11.txt' -Destination $_.DirectoryName
        }
        }
    else {
        Write-Log "AWP doesn't seem to be installed. Please install Oberthur Authentic web pack before activating this module."
        ExitCode 1
    }

But my issue is that each text file seems to have a unique info to each user. It may make sense to just append the new text lines on the end of the txt file.

Can anyone help me figure out how to append text to each users profile and loop through them all?

This is an example of two lines of text i need to add:

library=etpkcs11.dll 
name=eToken PKCS#11 Module

Any help would be appreciated.

I also can't think of how im going to resolve new profiles once i run the script. But that's a battle for another day.

2
  • Just use Out-File -Append Commented Oct 3, 2018 at 23:59
  • Hey Owain - thank you for replying. How would i use the out-file -append scenario to loop through all the profiles? sorry i can't seem to figure out how to use that in my script. Can you kindly provide some more advice please? Commented Oct 4, 2018 at 0:14

1 Answer 1

1

This should work:

$toAppend = "
library=etpkcs11.dll 
name=eToken PKCS#11 Module
"

# Check to see if AWP is actually installed

$chkAWPExists = "C:\Program Files\Middleware Directory\Middleware"
$aWPExists = Test-Path $chkAWPExists

# If AWP Exists copy the pkcs11.txt file to all Firefox Profiles found.

If ($aWPExists -eq $True) {
    Write-Log "AWP Exists. Copying pkcs11.txt to all firefox profiles"
    ForEach ($file in (Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'))  {
        $toAppend | Out-File -Append $file -Encoding 'Ascii'
    }
}
else {
    Write-Log "AWP doesn't seem to be installed. Please install Oberthur Authentic web pack before activating this module."
    ExitCode 1
}

What i changed is the following section of code (the foreach loop) :

Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt' |
ForEach-Object {
    Copy-Item -Path 'pkcs11.txt' -Destination $_.DirectoryName
}

to:

ForEach ($file in (Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'))  {
            $toAppend | Out-File -Append $file -Encoding 'Ascii'

Instead of passing the Get-Childitem call to Foreach-Object, it is instead calling Get-Childitem in the foreach loop making $file the path to the specific file.

The actual append is simply passing the $toAppend variable (defined at the top) to the Out-File -Append function:

$toAppend | Out-File -Append $file
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.