Найти строку в каждом файле.Если он существует, не добавляйте.Если его не существует, добавьте - PullRequest
0 голосов
/ 08 октября 2018

Приведенный ниже скрипт проверит путь.Если путь найден, он перебирает каждый текстовый файл и добавляет строку.Кто-то любезно помог мне добраться до этой точки.

Что я до сих пор не могу понять, так это как проверить, существует ли строка.Если это существует, двигаться дальше.И если его не существует, добавьте его.

Я пробовал варианты get-контента.Я просто не знаю, как реализовать это для циклического прохождения каждого текстового файла, пока все не проверено.

    # 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 голосов
/ 08 октября 2018

Это должно сделать это.

# 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...