Arrays 检查csv中是否存在数组中的任何值
我有下面的代码snippitArrays 检查csv中是否存在数组中的任何值,arrays,loops,powershell,csv,Arrays,Loops,Powershell,Csv,我有下面的代码snippit $dllCheckList = import-csv "c:\path\dllCheckList.csv" #dllCheckList.csv contains two items 'test1.dll' , 'test2.dll' $resultsArray1 = @("test2.dll","test3.dll") $resultsArray2 = @("test3.dll","test4.dll") $resultsArray3 = @("test1.dl
$dllCheckList = import-csv "c:\path\dllCheckList.csv"
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'
$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")
我将使用什么样的比较/循环,以便
$resultsArray1
写入test2.dll
,$resultsArray2
不写入任何内容,$resultsArray3
写入test.dll,test2.dll
我建议使用比较对象-passthru-excludeDifferent-includeEqual
我建议使用比较对象-passthru-excludeDifferent-includeEqual
我建议使用比较对象-passthru-excludeDifferent-includeEqual
使用Compare Object-passthru-excludeDifferent-includeEqual
一种方法是使用从csv数据动态构建的正则表达式,然后对每个数组使用-match。这样可以避免通过csv中的每个条目迭代每个数组。不确定您的csv看起来是什么样子,但您需要导入它,然后首先构建一个包含所有dll名称的数组,或者修改它以直接使用csv代替数组
#$dllCheckList = import-csv "c:\path\dllCheckList.csv"
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'
$dll_checklist = @('test1.dll','test2.dll')
$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")
[regex] $dll_regex = ‘(?i)^(‘ + (($dll_checklist |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’
$resultsArray1,$resultsArray2,$resultsArray3 |
foreach {$_ -match $dll_regex}
test2.dll
test1.dll
test2.dll
创建正则表达式的行的解释可以在这里找到:
一种方法是使用从csv数据动态构建的正则表达式,然后对每个数组使用-match。这样可以避免通过csv中的每个条目迭代每个数组。不确定您的csv看起来是什么样子,但您需要导入它,然后首先构建一个包含所有dll名称的数组,或者修改它以直接使用csv代替数组
#$dllCheckList = import-csv "c:\path\dllCheckList.csv"
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'
$dll_checklist = @('test1.dll','test2.dll')
$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")
[regex] $dll_regex = ‘(?i)^(‘ + (($dll_checklist |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’
$resultsArray1,$resultsArray2,$resultsArray3 |
foreach {$_ -match $dll_regex}
test2.dll
test1.dll
test2.dll
创建正则表达式的行的解释可以在这里找到:
一种方法是使用从csv数据动态构建的正则表达式,然后对每个数组使用-match。这样可以避免通过csv中的每个条目迭代每个数组。不确定您的csv看起来是什么样子,但您需要导入它,然后首先构建一个包含所有dll名称的数组,或者修改它以直接使用csv代替数组
#$dllCheckList = import-csv "c:\path\dllCheckList.csv"
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'
$dll_checklist = @('test1.dll','test2.dll')
$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")
[regex] $dll_regex = ‘(?i)^(‘ + (($dll_checklist |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’
$resultsArray1,$resultsArray2,$resultsArray3 |
foreach {$_ -match $dll_regex}
test2.dll
test1.dll
test2.dll
创建正则表达式的行的解释可以在这里找到:
一种方法是使用从csv数据动态构建的正则表达式,然后对每个数组使用-match。这样可以避免通过csv中的每个条目迭代每个数组。不确定您的csv看起来是什么样子,但您需要导入它,然后首先构建一个包含所有dll名称的数组,或者修改它以直接使用csv代替数组
#$dllCheckList = import-csv "c:\path\dllCheckList.csv"
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'
$dll_checklist = @('test1.dll','test2.dll')
$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")
[regex] $dll_regex = ‘(?i)^(‘ + (($dll_checklist |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’
$resultsArray1,$resultsArray2,$resultsArray3 |
foreach {$_ -match $dll_regex}
test2.dll
test1.dll
test2.dll
创建正则表达式的行的解释可以在这里找到:
假设
$resultsArray3
的输出应该是test1.dll,test2.dll
,而不是test.dll,test2.dll
,并且还假设c:\path\dllCheckList.csv
是一个实际的csv,带有一个头,我将使用-包含和-连接操作符:
PS C:\> $resultsArray1 = @("test2.dll","test3.dll")
PS C:\> $resultsArray2 = @("test3.dll","test4.dll")
PS C:\> $resultsArray3 = @("test1.dll","test2.dll")
PS C:\> cat 'C:\path\dllCheckList.csv'
Libs
test1.dll
test2.dll
PS C:\> $dllCheckList = Import-Csv 'C:\path\dllCheckList.csv' | % { $_.Libs }
PS C:\> $dllCheckList
test1.dll
test2.dll
PS C:\> ($resultsArray1 | ? { $dllCheckList -contains $_ }) -join ', '
test2.dll
PS C:\> ($resultsArray2 | ? { $dllCheckList -contains $_ }) -join ', '
PS C:\> ($resultsArray3 | ? { $dllCheckList -contains $_ }) -join ', '
test1.dll, test2.dll
PS C:\>$resultsArray1=@(“test2.dll”、“test3.dll”)
PS C:\>$resultsArray2=@(“test3.dll”、“test4.dll”)
PS C:\>$resultsArray3=@(“test1.dll”、“test2.dll”)
PS C:\>cat'C:\path\dllCheckList.csv'
自由基
test1.dll
test2.dll
PS C:\>$dllCheckList=Import Csv'C:\path\dllCheckList.Csv'|%{$\u0.Libs}
PS C:\>$dllCheckList
test1.dll
test2.dll
PS C:\>($resultsArray1 |?{$dllCheckList-包含${})-连接','
test2.dll
PS C:\>($resultsArray2 |?{$dllCheckList-包含${})-连接','
PS C:\>($resultsArray3 |?{$dllCheckList-包含${})-连接','
test1.dll,test2.dll
假设$resultsArray3
的输出应该是test1.dll,test2.dll
,而不是test.dll,test2.dll
,并且还假设c:\path\dllCheckList.csv
是一个实际的csv,带有一个头,我将使用-包含和-join
操作符:
PS C:\> $resultsArray1 = @("test2.dll","test3.dll")
PS C:\> $resultsArray2 = @("test3.dll","test4.dll")
PS C:\> $resultsArray3 = @("test1.dll","test2.dll")
PS C:\> cat 'C:\path\dllCheckList.csv'
Libs
test1.dll
test2.dll
PS C:\> $dllCheckList = Import-Csv 'C:\path\dllCheckList.csv' | % { $_.Libs }
PS C:\> $dllCheckList
test1.dll
test2.dll
PS C:\> ($resultsArray1 | ? { $dllCheckList -contains $_ }) -join ', '
test2.dll
PS C:\> ($resultsArray2 | ? { $dllCheckList -contains $_ }) -join ', '
PS C:\> ($resultsArray3 | ? { $dllCheckList -contains $_ }) -join ', '
test1.dll, test2.dll
PS C:\>$resultsArray1=@(“test2.dll”、“test3.dll”)
PS C:\>$resultsArray2=@(“test3.dll”、“test4.dll”)
PS C:\>$resultsArray3=@(“test1.dll”、“test2.dll”)
PS C:\>cat'C:\path\dllCheckList.csv'
自由基
test1.dll
test2.dll
PS C:\>$dllCheckList=Import Csv'C:\path\dllCheckList.Csv'|%{$\u0.Libs}
PS C:\>$dllCheckList
test1.dll
test2.dll
PS C:\>($resultsArray1 |?{$dllCheckList-包含${})-连接','
test2.dll
PS C:\>($resultsArray2 |?{$dllCheckList-包含${})-连接','
PS C:\>($resultsArray3 |?{$dllCheckList-包含${})-连接','
test1.dll,test2.dll
假设$resultsArray3
的输出应该是test1.dll,test2.dll
,而不是test.dll,test2.dll
,并且还假设c:\path\dllCheckList.csv
是一个实际的csv,带有一个头,我将使用-包含和-join
操作符:
PS C:\> $resultsArray1 = @("test2.dll","test3.dll")
PS C:\> $resultsArray2 = @("test3.dll","test4.dll")
PS C:\> $resultsArray3 = @("test1.dll","test2.dll")
PS C:\> cat 'C:\path\dllCheckList.csv'
Libs
test1.dll
test2.dll
PS C:\> $dllCheckList = Import-Csv 'C:\path\dllCheckList.csv' | % { $_.Libs }
PS C:\> $dllCheckList
test1.dll
test2.dll
PS C:\> ($resultsArray1 | ? { $dllCheckList -contains $_ }) -join ', '
test2.dll
PS C:\> ($resultsArray2 | ? { $dllCheckList -contains $_ }) -join ', '
PS C:\> ($resultsArray3 | ? { $dllCheckList -contains $_ }) -join ', '
test1.dll, test2.dll
PS C:\>$resultsArray1=@(“test2.dll”、“test3.dll”)
PS C:\>$resultsArray2=@(“test3.dll”、“test4.dll”)
PS C:\>$resultsArray3=@(“test1.dll”、“test2.dll”)
PS C:\>cat'C:\path\dllCheckList.csv'
自由基
test1.dll
test2.dll
PS C:\>$dllCheckList=Import Csv'C:\path\dllCheckList.Csv'|%{$\u0.Libs}
PS C:\>$dllCheckList
test1.dll
test2.dll
PS C:\>($resultsArray1 |?{$dllCheckList-包含${})-连接','
test2.dll
PS C:\>($resultsArray2 |?{$dllCheckList-包含${})-连接','
PS C:\>($resultsArray3 |?{$dllCheckList-包含${})-连接','
test1.dll,test2.dll
假设$resultsArray3
的输出应该是test1.dll,test2.dll
,而不是test.dll,test2.dll
,并且还假设c:\path\dllCheckList.csv
是一个实际的csv,带有一个头,我将使用-包含和-join
操作符:
PS C:\> $resultsArray1 = @("test2.dll","test3.dll")
PS C:\> $resultsArray2 = @("test3.dll","test4.dll")
PS C:\> $resultsArray3 = @("test1.dll","test2.dll")
PS C:\> cat 'C:\path\dllCheckList.csv'
Libs
test1.dll
test2.dll
PS C:\> $dllCheckList = Import-Csv 'C:\path\dllCheckList.csv' | % { $_.Libs }
PS C:\> $dllCheckList
test1.dll
test2.dll
PS C:\> ($resultsArray1 | ? { $dllCheckList -contains $_ }) -join ', '
test2.dll
PS C:\> ($resultsArray2 | ? { $dllCheckList -contains $_ }) -join ', '
PS C:\> ($resultsArray3 | ? { $dllCheckList -contains $_ }) -join ', '
test1.dll, test2.dll
PS C:\>$resultsArray1=@(“test2.dll”、“test3.dll”)
PS C:\>$resultsArray2=@(“test3.dll”、“test4.dll”)
PS C:\>$resultsArray3=@(“test1.dll”、“test2.dll”)
PS C:\>cat'C:\path\dllCheckList.csv'
自由基
test1.dll
test2.dll
PS C:\>$dllCheckList=Import Csv'C:\path\dllCheckList.Csv'|%{$\u0.Libs}
私人秘书长:\>$dllCheckL