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

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Thu Jul 19, 2018 5:30 am

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,
/* IT Engineer */

User avatar
jvierra
Posts: 13730
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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.

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Thu Jul 19, 2018 11:46 pm

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?
/* IT Engineer */

User avatar
jvierra
Posts: 13730
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jul 20, 2018 12:03 am

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.

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Fri Jul 20, 2018 12:05 am

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
/* IT Engineer */

User avatar
jvierra
Posts: 13730
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jul 20, 2018 12:11 am

No. You have to debug it a line at a time.

User avatar
jvierra
Posts: 13730
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jul 20, 2018 12:17 am

Note that the correct method of using this is:

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

Locked