Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在文本文件中查找字符串并在末尾追加文本_Powershell - Fatal编程技术网

Powershell 在文本文件中查找字符串并在末尾追加文本

Powershell 在文本文件中查找字符串并在末尾追加文本,powershell,Powershell,我有一个Firefox模块的问题。为了解决这个问题,我需要将文本附加到每个用户的firefox配置文件下的文本文件中 我可以使用以下powershell脚本将新文本文件复制到每个用户配置文件: # Check to see if AWP is actually installed $chkAWPExists = "C:\Program Files\Middleware Directory\Middleware" $aWPExists = Test-Path $chkAW

我有一个Firefox模块的问题。为了解决这个问题,我需要将文本附加到每个用户的firefox配置文件下的文本文件中

我可以使用以下powershell脚本将新文本文件复制到每个用户配置文件:

    # 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
    }
但我的问题是,每个文本文件似乎对每个用户都有唯一的信息。在txt文件的末尾添加新的文本行可能是有意义的

有谁能帮我弄清楚如何将文本附加到每个用户配置文件中并循环浏览它们吗

这是我需要添加的两行文本的示例:

library=etpkcs11.dll 
name=eToken PKCS#11 Module
任何帮助都将不胜感激

我也不知道一旦我运行脚本,我将如何解析新的配置文件。但这是另一天的战斗。

这应该是可行的:

$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
}
我更改的是以下代码部分(foreach循环):

致:

它不是将Get Childitem调用传递给Foreach对象,而是在Foreach循环中调用Get Childitem,使$file成为特定文件的路径

实际的append只是将$toAppend变量(在顶部定义)传递给Out File-append函数:

$toAppend | Out-File -Append $file

只需使用我们的文件-AppendHey Owain-感谢您的回复。我如何使用out file-append场景循环遍历所有概要文件?抱歉,我似乎不知道如何在我的脚本中使用它。你能再提供一些建议吗?
ForEach ($file in (Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'))  {
            $toAppend | Out-File -Append $file -Encoding 'Ascii'
$toAppend | Out-File -Append $file