TransWikia.com

Не получается разделить строку в Powershell через кавычки

Stack Overflow на русском Asked by Ruslannnn on March 2, 2021

Доброй ночи,
есть большой файл vpn.csv, где строки имеют примерно такой вид:

"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:56 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""user1"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:51 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""[email protected]"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:46 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""notmydomainmary"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:46 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""user"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:44 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""john"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:41 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""mydomainmyuser"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:33 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""mydomainuser"",""4""
"Address assigned to session,""cisco asa 192.168.0.1"",""1"",""Oct 28, 2020, 3:27:26 PM"",""VPN In Progress"",""192.168.2.1"",""0"",""192.168.0.1"",""0"",""notmydomainuser"",""4"""

И есть файл all_users_AD.txt , в котором просто имена пользователей нашего домена.

Скриптом ниже пытаюсь найти vpn.csv только те строки, которые касаются только пользователей нашего домена.

$FileADUsers = $PSScriptRoot+'all_users_AD.txt'
$FileVPNData = $PSScriptRoot+'vpn.csv'

$ADUsers = Get-Content $FileADUsers

Get-Content $FileVPNData | ForEach-Object {
    $str = $_ -split '""'
    
    if ($ADUsers -match $str[17]) {
        $str
    }
}

шестая строка файла выдает ошибку:

выполняется разбор "mydomainmyuser" - Нераспознанная esc-последовательность m.

Как это можно исправить?

2 Answers

param(
    [IO.FileInfo]$ADUsersListFileName = 'D:tmpADUersList.txt',
    [IO.FileInfo]$processedFile = "D:tmpin.txt"
)

[string[]]$ADUsersArray = Get-Content $ADUsersListFileName
$reader = [System.IO.StreamReader]::new($processedFile)
while($buffer = $reader.ReadLine()){
    if ([Linq.Enumerable]::Contains($ADUsersArray, ($buffer -split ',')[11].Trim('"'))){
        $buffer
    }
}
$reader.Close()

Answered by Алексей Семенов on March 2, 2021

Пока додумался только в каждой строке, перед поиском, с помощью replace поменять на явно не встречающуюся в исходном файле комбинацию символов, например "___".

$FileADUsers = $PSScriptRoot+'all_users_AD.txt'
$FileVPNData = $PSScriptRoot+'vpn.csv'

$ADUsers = Get-Content $FileADUsers

Get-Content $FileVPNData | ForEach-Object {
    $_ = $_ -replace '\','___'

    $str = $_ -split '""'
    
    if ($ADUsers -match $str[17]) {
        $str
    }
}

Может туповато и медленно, но додумался только до этого, и вроде работает.

Answered by Ruslannnn on March 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP