PowerShell脚本:结果问题
我想知道为什么当我运行我的脚本结果显示我“;” 在我的arrays之间。 其实我试图比较两个excel列,并显示差异。 如果一个单元格不在第一列中,而是在第二列中显示该列。 我怎样才能删除这些“;” 并获得列之间的差异? 我也尝试过与一列的一些文件,它的工作原理
结果:
ComputerName;;;;;OtherComputerName ---------------------------------- uiojk;;;;;uih hjbyu;;;;;ubyhi`
脚本:
#get the content of the column in the file [string[]]$a = import-csv '.\test1.csv' | select -expand ComputerName [string[]]$d = import-csv '.\test1.csv' | select -expand OtherComputerName #display the cells which aren't in the $a list $d |? {$a -notcontains $_} #displays the columns of one file [Array]$stores[2].ComputerName [Array]$stores[2].OtherComputerName $stores
Import-Csv
使用逗号作为默认的分隔符,如果你想使用另一个字符,那么你将需要使用-Delimiter
选项。 像这样的东西:
$x = Import-Csv .\test1.csv -Delimiter ';' Compare-Object -ReferenceObject $x.ComputerName -DifferenceObject $x.OtherComputerName
此代码会生成一个警告,因为不是所有的CSV列都有一个标题,但它仍然可以工作。