User PowerShell to compare whether two arrays are exactly the same

Requirement

Compare two arrays in PowerShell to determine if both are exactly this same. This code was used in my script to find document libraries that inherited permissions from the site level.  With a server side script, you could do this easily with the HasUniqueRoleAssignments property. Not so easy when using SharePoint Web Service.

Solution

The solution is to convert both arrays into a MD5 checksums and to compare them using a simple if statement.

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$sitehash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($sitepermissions)))
$listhash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($listpermissions)))
if ($sitehash -eq $listhash)
{
Write-host "Permissions inherited"
}

Tags:

No comments yet.

Leave a Reply

%d bloggers like this: