在 PowerShell 中比较数组变量 [英] Comparing array variables in PowerShell

查看:33
本文介绍了在 PowerShell 中比较数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本来比较两个文件夹.

I have this script to compare two folders.

$firstfolder = Get-ChildItem C:\firstfolder
$secondfolder = Get-ChildItem C:\firstfolder

if ($firstfolder -eq $secondfolder) {
Write-Host "Folders are the same."
} else {
    Write-Host "Error: Doesn't match."
}

如您所见,我比较了同一个文件夹.问题是,它永远不会考虑数组是相等的.为什么?

As you can see, I compare the same folder. Problem is, that it will never consider, that the arrays are equal. Why?

推荐答案

在 PowerShell 中,通过枚举数组本身的内容,在表达式中计算指向数组的变量.

In PowerShell, variables that point to arrays are evaluated in expressions by enumerating the contents of the arrays themselves.

例如这个表达式:

$firstFolder | Get-Member

将返回有关 System.IO.DirectoryInfo 类型,它确实是 $firstFolder 数组中 first 元素的类型.如果要对数组对象本身进行操作,则必须使用 , 运算符明确告诉 PowerShell 将对象视为数组.以下表达式:

will return information about the System.IO.DirectoryInfo type, which indeed is the type of the first element in the $firstFolder array. If you want to operate on the array object itself, you have to explicitly tell PowerShell to treat the object as array by using the , operator. The following expression:

,$firstFolder | Get-Member

预期会返回有关 System.Object[] 的信息.

will expectedly return information about System.Object[].

但是,当在两个数组变量之间使用 -eq 运算符时,情况就有些不同了.事实上,PowerShell 只会枚举左侧的数组,并将每个项目与右侧的数组作为一个整体进行比较.当没有匹配项时,结果将是匹配项的数组什么都没有.例如:

However, when the -eq operator is used between two array variables, things are a bit different. PowerShell will, in fact, enumerate only the array on the left side and compare each item to the array on the right side as a whole. The result will be an array of matching items or nothing at all when there are no matches. For example:

$a = 1..5
$b = 1..5
$a -eq $b         # returns zero-length array
[bool]($a -eq $b) # returns $false
$a -eq 3          # returns 3

在您的特定情况下,结果将是零长度数组(或 $false 如果转换为 boolean) 因为 $firstFolder 数组包含 System.IO.DirectoryInfo 对象与 $secondFolder 变量中的数组不完全匹配.

In your particular case, the result will be zero-length array (or $false if casted to a boolean) since the $firstFolder array contains System.IO.DirectoryInfo objects that don't quite match the array in the $secondFolder variable.

您真正想要的是比较两个数组的内容.这是 Compare-Object cmdlet 派上用场的地方:

What you really want is to compare the contents of both arrays against each other. This is where the Compare-Object cmdlet comes in handy:

Compare-Object $firstFolder $secondFolder -SyncWindow 0

这将返回两个数组之间的差异数组,或者当数组相等时返回$null.更准确地说,结果数组将包含每个项目的对象,该对象仅存在于一个数组中而不存在于另一个数组中.-SyncWindow 0 参数将使项目出现在数组中的顺序计为差异.

This will return an array of differences between the two arrays or $null when the arrays are equal. More precisely, the resulting array will contain an object for each item that exists only in one array and not the other. The -SyncWindow 0 argument will make the order in which the items appear in the arrays count as a difference.

如果您只需要一种简单的方法来判断两个数组是否不同,而无需深入了解什么不同的细节,您可以简单地检查 比较对象:

If all you need is a simple way to tell whether the two arrays are different without going into the details of what is different, you can simply check the length of the array returned by Compare-Object:

$areEqual = @(Compare-Object $firstFolder $secondFolder -SyncWindow 0).Length -eq 0

请注意我如何明确告诉 PowerShell 始终将结果打包到数组中,因为 当数组不包含任何差异时,Compare-Object 可能返回 $null.

Notice how I explicitly told PowerShell to always package the results into an array, since Compare-Object may return $null when the arrays don't contain any differences.

这篇关于在 PowerShell 中比较数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆