Answered by:

Replace function and wildcards

Question

i'm tryng to extract from a log file only the lines with specific text.
I'm look for each lines in the log file where i find "@todomain.it" string in a specified position, I explain:
the log file's lines structure is composed to much information and appear like this:
date-time-serverName-senderAddress-recipientAddress-relayHost
so i need to extract only the lines where the recipientAddress contains the domain "@todomain.it"

To do this I specify the domain to find in variable $DominioDaCercare using wildcards to specify it "*@todomain.it*" because without wildcards the first filtering of the log file do not return nothing.
After the first filtering i want to extract only the raws where recipientAddress field is "*@todomain.it*" but using wildcards the script search for string "todomain.it*" and trying to remove "*" using replace
function it doesn't works...
what can i do to solve this ?

ForEach ($line in $testo)
{
$arrLine = $line.split(";")
$DestinatarioNelLog = $arrLine[7] <# this is the field where in tthe log is reported the recipent address #>
$arrDominioNelLog = $DestinatarioNelLog.Split("@") <# i split the recipient address to obtain only the domain #>
$DominioNelLog = $arrDominioNelLog[1]
$arrDominioDaCercare = $DominioDaCercare.Split("@") <# i split also the domain to find to check only the part after @ #>
Out-Host -InputObject $arrDominioDaCercare[1]

<# HERE IS THE PROBLEM #>
<# I'M TRYING TO REMOVE "*" FROM THE STRING TO FIND BECAUSE WITHOUT REMOVING IT IN THE NEXT HOPE WHERE I CHECK IF THE TWO STRINGS ARE EQUAL THE TEST FAIL FOR
THE "*" PRESENT #>
$arrDominioDaCercare[1] = $arrDominioDaCercare[1] -replace("*","")
$DominioDaCercare = $arrDominioDaCercare[1]
$arrDominioDaCercare[1] | Out-File C:\LogRelay\dominiodacercare.txt

Read-Host

<# TEST DOESN'T WORKS BECAUSE THE FIRST STRING IS "DOMAINTOFIND.IT" AND THE OTHER STRING IS "DOMAINTOFIND.IT*"
if ($DominioNelLog -eq $DominioDaCercare)
{
$line | Out-File -Append C:\LogRelay\Exporteq.csv
$line | Out-File -Append C:\LogRelay\lineeq.txt
}
}

Microsoft is conducting an online survey to understand your opinion of the Technet Web site. If you choose to participate, the online survey will be presented to you when you leave the Technet Web site.