在每个文件中查找字符串。如果存在,请不要追加。如果不存在,请附加

时间:2018-10-08 03:57:23

标签: powershell

以下脚本将测试路径。如果找到路径,它将在每个文本文件中循环并附加一个字符串。有人帮助我达到了这一点。

我仍然不知道的是如何检查字符串是否存在。如果存在,请继续。如果不存在,请将其添加。

我尝试了各种get-content。我只是不知道如何实现它来循环遍历每个文本文件,直到全部选中为止。

    # Check to see if AWP is actually installed

    $awpPath = "C:\Program Files\Middleware\Card"
    $aWPExists = Test-Path $awpPath
    $toAppend = "
    library=C:\Program Files\Middleware\middleware.dll
    name=PKCS 11 Middleware
    "

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

    $pKCSFiles = Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'

    If ($aWPExists -eq $True) {
        Write-Log "AWP Exists and module wasn't found in the pkcs11.txt file. Copying pkcs11.txt to all firefox profiles"

        ForEach ($file in ($pKCSFiles))  {
            $toAppend | Out-File -Append $file -Encoding 'Ascii'


        }
    }
    else {

        Write-Log "AWP doesn't seem to be installed. Please install  before activating this module."
        Exit-Script ExitCode 1
    }

1 个答案:

答案 0 :(得分:0)

这应该做到。

# Check to see if AWP is actually installed

$awpPath = "C:\Program Files\Middleware\Card"
$aWPExists = Test-Path $awpPath
$toAppend = "
library=C:\Program Files\Middleware\middleware.dll
name=PKCS 11 Middleware
"

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

$pKCSFiles = Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'

If ($aWPExists -eq $True) {
    Write-Log "AWP Exists and module wasn't found in the pkcs11.txt file. Copying pkcs11.txt to all firefox profiles"

    ForEach ($file in ($pKCSFiles))  {
        If ((get-content $file -tail 2) -notmatch "PKCS 11") {
            $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."
    Exit-Script ExitCode 1
}

此外,您的脚本仅会按要求添加pkcs11.txt文件,而不是全部文本文件

以下代码与上面的代码相同,但是读取整个文件而不是文件末尾进行验证。

# Check to see if AWP is actually installed

$awpPath = "C:\Program Files\Middleware\Card"
$aWPExists = Test-Path $awpPath
$toAppend = "
library=C:\Program Files\Middleware\middleware.dll
name=PKCS 11 Middleware
"

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

$pKCSFiles = Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'

If ($aWPExists -eq $True) {
    Write-Log "AWP Exists and module wasn't found in the pkcs11.txt file. Copying pkcs11.txt to all firefox profiles"

    ForEach ($file in ($pKCSFiles))  {
        If ((get-content $file -raw) -notmatch "PKCS 11") {
            $toAppend | Out-File -Append $file -Encoding 'Ascii'
        }

    }
}
else {

    Write-Log "AWP doesn't seem to be installed. Please install web pack before activating this module."
    Exit-Script ExitCode 1
}

最后和最后一个选项在逻辑上是最合理的。它强制它找到字符串。如果没有,它将附加它。在强制它找不到字符串之前。并且由于该文件有多行,因此仍然成功。

# Check to see if AWP is actually installed

$awpPath = "C:\Program Files\Middleware\Card"
$aWPExists = Test-Path $awpPath
$toAppend = "
library=C:\Program Files\Middleware\middleware.dll
name=PKCS 11 Middleware
"

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

$pKCSFiles = Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\pkcs11.txt'

If ($aWPExists -eq $True) {
    Write-Log "AWP Exists and module wasn't found in the pkcs11.txt file. Copying pkcs11.txt to all firefox profiles"

    ForEach ($file in ($pKCSFiles))  {
        If (!((get-content $file) -match "PKCS 11")) {
            $toAppend | Out-File -Append $file -Encoding 'Ascii'
        }

    }
}
else {

    Write-Log "AWP doesn't seem to be installed. Please install web pack before activating this module."
    Exit-Script ExitCode 1
}