Am at last getting my script working as intended. Hopefully last issue is that i have only read-access to a network drive. This is okay since it is a somewhat critical place to operate in. Makes me want to write the output to a local folder instead, without trying to delete the original files.
$encoding = [System.Text.Encoding]::GetEncoding(1252)
$filter = 'M:\ALM.\*.alm'
$pattern = "ASE|MBE|MB1|Network interfaces|Handle|default collector|Proficy|Recovered|AlmODBC|Close Digital Point|DataBlock|Channel|Record|TESTBOOL|OK|OPC|Item|_COMMCHECK_|SYSTEM|ADMINISTRATOR|Unknown|ItemID|Field's|Connection|hutdown|acknowledged|Alarming|Suspended|primary|SAC|reloaded|Fix32|1601-01-01"
(Select-String -encoding $encoding -Path $filter -Pattern $pattern -NotMatch) |
ForEach-Object -Begin {Remove-Item $filter} -Process {
$_.line | Out-File -Append -Encoding $encoding $_.filename
}
What i don't under stand is how to make this part:{$_.line | Out-File -Append -Encoding $encoding $_.filename }
append the result to new local files with the same name, and leave the original alone apart from being read. Peace!
Update:
My problem was what the second comment helped me solve: construct a local path as part of the Out-File
call. I replaced $_.filename
with (Join-Path $LocalDir $_.filename)
and that did it.
The script works perfectly, but still tries to delete (or just edit?)the network-folder files though. Making the terminal shine up red for every file.
Update:
Corrected this be removing the whole -Begin {Remove-Item $filter}
part.
-all your base are belong to us