Page 1 of 1

PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Thu Jul 19, 2018 5:30 am
by ITEngineer
Hi Folks,

I need some help in modifying the PowerShell script to compare the date time created and file size for the multiple AD Domain Controllers servers shared folder

Example of the UNC path to compare:
Source will be first copied to this reference folders\\PRODDC01-VM\sysvol\domain.com\LogonScripts

Code: Select all

\\PRODDC02-VM\sysvol\domain.com\LogonScripts
\\PRODDC03-VM\sysvol\domain.com\LogonScripts
…
\\PRODDC29-VM\sysvol\domain.com\LogonScripts
This is the script that I have but not working:

Code: Select all

Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))" | ForEach-Object {
	"Processing $($_.DNSHostName) ..." | Write-Host
	
	$folderReference = "\\PRODDC01-VM\sysvol\domain.com\LogonScripts"
	$folderDifference = "\\$_.DNSHostName\sysvol\domain.com\LogonScripts"
	
	$FolderReferenceContents = Get-ChildItem $folderReference -Recurse | where-object { -not $_.PSIsContainer }
	$FolderDifferenceContents = Get-ChildItem $folderDifference -Recurse | where-object { -not $_.PSIsContainer }
	
	#Get only files that are NOT on the reference server
	Compare-Object -ReferenceObject $FolderReferenceContents -DifferenceObject $FolderDifferenceContents -Property ('Name', 'Length') -PassThru |
	where-object { $_.SideIndicator -eq '=>' } | Select FullName
	
} | Export-Csv -Path C:\SYSVOL-Script.CSV -NTI
Any help would be greatly appreciated.

Thanks,

Re: PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Thu Jul 19, 2018 6:18 am
by jvierra
You cannot do this:
$folderDifference = "\\$_.DNSHostName\sysvol\domain.com\LogonScripts"

It must be this:
$folderDifference = "\\$($_.DNSHostName)\sysvol\domain.com\LogonScripts"

Subexpression evaluation must be used for an object's properties.

Re: PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Thu Jul 19, 2018 11:46 pm
by ITEngineer
jvierra wrote:
Thu Jul 19, 2018 6:18 am
You cannot do this:
$folderDifference = "\\$_.DNSHostName\sysvol\domain.com\LogonScripts"

It must be this:
$folderDifference = "\\$($_.DNSHostName)\sysvol\domain.com\LogonScripts"

Subexpression evaluation must be used for an object's properties.

yes, you are right.

however, there is no result at the.CSV file and there is nothing showing on the PowerShell console either?

Re: PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Fri Jul 20, 2018 12:03 am
by jvierra
You will have to debug the script since we cannot run it.

Do the script at a prompt one line at a time and check the results.

Re: PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Fri Jul 20, 2018 12:05 am
by ITEngineer
This is the updated script as per your suggestion:

The output on the console is correct:

Processing PRODDC01-VM...
Processing PRODDC02-VM...
Processing PRODDC03-VM...
...
Processing PRODDC29-VM...

Code: Select all

$folderReference = "\\PRODDC01-VM\sysvol\domain.com\LogonScripts"

Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))" | ForEach-Object {
	"Processing $($_.DNSHostName) ..." | Write-Host

	$folderDifference = "\\$($_.DNSHostName)\sysvol\domain.com\LogonScripts"

	$FolderReferenceContents = Get-ChildItem $folderReference -Recurse | where-object { -not $_.PSIsContainer }
	$FolderDifferenceContents = Get-ChildItem $folderDifference -Recurse | where-object { -not $_.PSIsContainer }
	
	#Get only files that are NOT on the reference server
	Compare-Object -ReferenceObject $FolderReferenceContents -DifferenceObject $FolderDifferenceContents -Property ('Name', 'Length') -PassThru |
	where-object { $_.SideIndicator -eq '=>' } | Select FullName
	
} | Export-Csv -Path C:\TheDifferencesAre.CSV -NoTypeInformation

Re: PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Fri Jul 20, 2018 12:11 am
by jvierra
No. You have to debug it a line at a time.

Re: PowerShell script to compare files on multiple UNC shared folder and show the difference only as .CSV table ?

Posted: Fri Jul 20, 2018 12:17 am
by jvierra
Note that the correct method of using this is:

Compare-Object $a $b |Where {$_.SideIndicator -eq '=>'} | Select InputObject