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

Ask your 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.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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,
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

No. You have to debug it a line at a time.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Note that the correct method of using this is:

Compare-Object $a $b |Where {$_.SideIndicator -eq '=>'} | Select InputObject
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked