[英]Why wont this powershell script execute
我正在嘗試制作執行以下操作的腳本:
一切正常,直到我在第4步中添加了功能,之后Powershell吐出了很多錯誤。 我在這里做錯了什么?
PS C:\Users\pzsr7z.000\Desktop\incidentoutput> cat .\csvtest.txt | ? {$_.trim() -ne "" } | sls "^Reassignment$" -Context 1,2 | foreach{ $_.TrimStart(">"," ")}
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
所有功能都各自獨立工作,當我嘗試添加最后一部分時,事情就搞砸了。
我的靈性告訴我,您正在嘗試對Select-String cmdlet返回的MatchInfo對象使用String
的TrimStart方法。
嘗試先將其轉換為字符串:
... | sls "^Reassignment$" -Context 1,2 | foreach{ $_.ToString().TrimStart(">"," ")
此正則表達式模式: ^Reassignment$
將僅匹配具有確切值Reassignment
-並非所有包含 Reassignment
。
當您已經使用Get-Content
( cat
)從文件中讀取-match
,也可以在所有行上使用-match
運算符( -cmatch
區分大小寫):
(cat .\csvtest.txt) -cmatch 'Reassignment'
-match
運算符僅返回匹配的字符串,因此您可以直接在這些字符串上調用TrimStart()
:
$ReassignmentLines = (cat .\csvtest.txt) -cmatch 'Reassignment' |ForEach-Object {
$_.TrimStart(">"," ")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.