通过匹配字符串查找文件内容并输出PowerShell 2.0中找不到的内容
To find the file contents by matching strings and output those which are not found in PowerShell 2.0
我有代码片段检查文件中的字符串内容并列出未出现在 file.The 中的字符串下面的代码片段在 PowerShell 5.0 中工作正常,但在 PowerShell 2.0 中我得到错误如:
Foreach object: Cannot convert 'System.object[]' to type 'System.Management.Automation.ScriptBlock' required by a parameter 'Process'.Specified method is not supported in power shell 2.0
以下代码适用于 PowerShell 2.0 和 5.0。但目前它仅适用于 5.0。
$Pattern = 'Hello|new|World|Hi|greet'
$Test = (Get-Content -Path .\file.txt | Select-String -Pattern $Pattern -AllMatches) | foreach {$_.matches.Value} $($pattern -split '\|') | where {$Test -notcontains $_}
file.txt 具有以下内容:
$q = Get-Content -Path .\file.txt |Select-String 'Hello' -SimpleMatch
$w = Get-Content -Path .\file.txt |Select-String 'new' -SimpleMatch
$e = Get-Content -Path .\file.txt |Select-String 'World' -SimpleMatch
$r = Get-Content -Path .\file.txt |Select-String 'Hi' -SimpleMatch
$t = Get-Content -Path .\file.txt |Select-String 'greet' -SimpleMatch
我已经尝试了新代码:
$strings = ('Hello','new','World','Hi','greet')
$file = '.\file.txt'
$Found = (Get-Content -Path $file |
Select-String -Pattern $strings -AllMatches).Matches.Value
$Missing = $Strings | Where {$Found -notcontains $_}
if ($Missing) {
"Strings missing in $file"
$Missing
} else {
"All strings present in $file"
}
我能够将输出作为所有字符串的列表,而不是获取缺失字符串的列表。如何解决问题?
您更新后的问题的最后一个代码片段正在尝试使用名为 member enumeration 的功能。该功能是在 PowerShell v3 中引入的,在 PowerShell v2 中不可用。
更改声明
$Found = (Get-Content -Path $file |
Select-String -Pattern $strings -AllMatches).Matches.Value
进入
$Found = Get-Content -Path $file |
Select-String -Pattern $strings -AllMatches |
Select-Object -Expand Matches |
Select-Object -Expand Value
问题就会消失。
我有代码片段检查文件中的字符串内容并列出未出现在 file.The 中的字符串下面的代码片段在 PowerShell 5.0 中工作正常,但在 PowerShell 2.0 中我得到错误如:
Foreach object: Cannot convert 'System.object[]' to type 'System.Management.Automation.ScriptBlock' required by a parameter 'Process'.Specified method is not supported in power shell 2.0
以下代码适用于 PowerShell 2.0 和 5.0。但目前它仅适用于 5.0。
$Pattern = 'Hello|new|World|Hi|greet'
$Test = (Get-Content -Path .\file.txt | Select-String -Pattern $Pattern -AllMatches) | foreach {$_.matches.Value} $($pattern -split '\|') | where {$Test -notcontains $_}
file.txt 具有以下内容:
$q = Get-Content -Path .\file.txt |Select-String 'Hello' -SimpleMatch
$w = Get-Content -Path .\file.txt |Select-String 'new' -SimpleMatch
$e = Get-Content -Path .\file.txt |Select-String 'World' -SimpleMatch
$r = Get-Content -Path .\file.txt |Select-String 'Hi' -SimpleMatch
$t = Get-Content -Path .\file.txt |Select-String 'greet' -SimpleMatch
我已经尝试了新代码:
$strings = ('Hello','new','World','Hi','greet')
$file = '.\file.txt'
$Found = (Get-Content -Path $file |
Select-String -Pattern $strings -AllMatches).Matches.Value
$Missing = $Strings | Where {$Found -notcontains $_}
if ($Missing) {
"Strings missing in $file"
$Missing
} else {
"All strings present in $file"
}
我能够将输出作为所有字符串的列表,而不是获取缺失字符串的列表。如何解决问题?
您更新后的问题的最后一个代码片段正在尝试使用名为 member enumeration 的功能。该功能是在 PowerShell v3 中引入的,在 PowerShell v2 中不可用。
更改声明
$Found = (Get-Content -Path $file |
Select-String -Pattern $strings -AllMatches).Matches.Value
进入
$Found = Get-Content -Path $file |
Select-String -Pattern $strings -AllMatches |
Select-Object -Expand Matches |
Select-Object -Expand Value
问题就会消失。