此批处理命令在PowerShell中将是什么样?

时间:2018-09-14 07:47:42

标签: powershell for-loop command-line powershell-core

这在命令行中效果很好。尝试在PowerShell中运行时没有运气,语法显然有所不同。

for %%f in (*.hej) do (
    findstr /V "Channel Record TESTBOOL OK OPC Item _COMMCHECK_ SYSTEM ADMINISTRATOR Unknown ItemID Field's Connection hutdown acknowledged Alarming Suspended primary SAC reloaded Fix32" %%f > %%f.txt
    del %%f
    ren %%f.txt %%f
)

我正在遍历所有带有hej结尾的文件,并删除任何包含findstr /V之后的单词的行。

编辑: 除了编码,代码现在可以正常工作,并且现在看起来像这样:

$encoding = [System.Text.Encoding]::GetEncoding(1252)
$filter  = 'M:\ALM\*.alm'
$pattern = "Channel|Record|TESTBOOL|OK|OPC|Item|_COMMCHECK_|SYSTEM|ADMINISTRATOR|Unknown|ItemID|Field's|Connection|hutdown|acknowledged|Alarming|Suspended|primary|SAC|reloaded|Fix32"

(Select-String -encoding $encoding -Path $filter -Pattern $pattern -NotMatch) |
    ForEach-Object -Begin {Remove-Item $filter} -Process {$_.line | Out-File -Append -encoding $encoding $_.filename }

编码方面存在问题,但是从Powershell 5切换到了Powershell Core,在那里可以键入$encoding = [System.Text.Encoding]::GetEncoding(1252)

最近的补充是我没有对网络文件夹的写访问权,因此我希望将“清理过的”文件存储在本地文件夹中。尝试通过使用

去除$_.filename的路径而没有成功
| Out-File -Append -encoding $encoding c:\temp\(Get-Item '$_.filename').Basename }

欢呼

原始数据样本:

2018-05-19  00:22:07,166 [LUNSC1  ] Fix32.LUNSC1.LUNOP9_COMMCHECK_DV2.F_CV set to 0 by LUNOP9::SYSTEM ADMINISTRATOR                 
2018-05-19  00:23:35,226 [LUNSC2  ] Fix32.LUNSC2.LUNOP10_COMMCHECK_DV2.F_CV set to 0 by LUNOP10::SYSTEM ADMINISTRATOR               
2018-05-19  00:24:07,169 [LUNSC1  ] Fix32.LUNSC1.LUNOP9_COMMCHECK_DV2.F_CV set to 0 by LUNOP9::SYSTEM ADMINISTRATOR                 
2018-05-19  00:25:02,046 [LUNSC1  ] Fix32.LUNSC1.LUNOP9_COMMCHECK_DV.F_CV set to 0 by LUNSC1                                        
2018-05-19  00:25:07,975 [LUNSC1  ] Fix32.LUNSC1.LUNOP9_COMMCHECK_DV2.F_CV set to 1 by LUNOP9::SYSTEM ADMINISTRATOR                 
2018-05-19  00:25:35,232 [LUNSC2  ] Fix32.LUNSC2.LUNOP10_COMMCHECK_DV2.F_CV set to 0 by LUNOP10::SYSTEM ADMINISTRATOR               
2018-05-19  00:26:00,551 [LUNSC1  ] D_TA204_GT1_DV_AL             CFN              LARM      D_TA204_GT1_Reglerfel                  
2018-05-19  00:27:07,165 [LUNSC1  ] Fix32.LUNSC1.LUNOP9_COMMCHECK_DV2.F_CV set to 1 by LUNOP9::SYSTEM ADMINISTRATOR                 
2018-05-19  00:27:35,235 [LUNSC2  ] Fix32.LUNSC2.LUNOP10_COMMCHECK_DV2.F_CV set to 0 by LUNOP10::SYSTEM ADMINISTRATOR               
2018-05-19  00:28:01,049 [LUNSC1  ] D_TA204_GT41_DV_AL            CFN              LARM      D_TA204_GT41_Reglerfel                 
2018-05-19  00:28:01,049 [LUNSC1  ] D_TA204_GT31_DV_AL            CFN              LARM      D_TA204_GT31_Reglerfel                 
2018-05-19  00:28:01,049 [LUNSC1  ] D_TA204_GT21_DV_AL            CFN              LARM      D_TA204_GT21_Reglerfel   

1 个答案:

答案 0 :(得分:1)

在PowerShell中,类似这样的操作将按照您的描述进行操作:

$filter  = '*.hej'
$pattern = "Channel|Record|TESTBOOL|OK|OPC|Item|_COMMCHECK_|SYSTEM|ADMINISTRATOR|Unknown|ItemID|Field's|Connection|hutdown|acknowledged|Alarming|Suspended|primary|SAC|reloaded|Fix32"

(Select-String -Path $filter -Pattern $pattern -NotMatch) |
    ForEach-Object -Begin {Remove-Item $filter} -Process {$_.line >> $_.filename}