none
Help building script to collect file missmatch information RRS feed

  • Question

  • I want to start this off that I am a self taught google fu coder. 

    I'm looking for assistance in building a script to collect information regarding files and folders in a share from a source server and then comparing it to several other shares that has the exact same structure and then reporting back what is / isn't consistent with the source server.

    The point of this is not to just make the files consistent with the source, if that was the case I would just kick off a robocopy and overwrite everything.  I specifically need to know what files and folders are not in sync with the source.

    What I am thinking is that it would query through the source folder structure and would record all files and folders in the structure, these would probably be recorded via the full system path.  Then for each folder it would collect the things like the creation date, last access date, last modify date, folder size etc.  For files in the same path it would collect the same information but will also generate a hash of the file that can be used to compare.  Don't believe its possible to create a hash of a folder as well, if it is I would love to see that as well.  Also if any folders or files are missing I need that info as well. 

    After it built this list it would then go through the other shares and collect the same information, right now in my case there will be about 5 different servers that I will need to collect data on including the source server. 

    For the output I am thinking something that shows the server with the file path and then some kind of spread sheet showing the other servers with a Y/N if they are consistent.  Eventually I want to roll this into a visual studio project to have a GUI on it so that everything can have pretty Green / Red boxes to display the info.   

    I have already started with some basic commands and I can see using the Get-ChildItem command to get the file/folder information.  I can also pipe the results to the Get-FileHash command to get the file hash. 

    I am then running this all through Compare-Object to see what is/isn't consistent. 

    I think my biggest problem arises with first trying to combine the two data sets together, and then being able to call them properly to use the compare function to note if things are correct.  I'm hoping that if I get some base examples of how to build and call the data sets I can take this and run with it. 

    Friday, November 8, 2019 12:42 AM

All replies

  • I recommend that you learn PowerShell before trying to design this. Your assumptions sound really technical but really show that you have little experience with computer technology , data and programming.  You use of technical terms will keep you from seeing how to do this because most of your terms and ideas are either wrong or unnecessary Without some basic skill in programming this will be impossible for you.

    I recommend that you learn PowerShell and learn what a file is in PowerShell.  Also realize that you can compare all files in the folder tree using RoboCopy by dumping a log of the files that are the same name/path but differ by date and size.   This will also give you files missing between the two copies.

    You need to back up and rethink what you are asking but first you need to learn basic PowerShell and what a file is.

    The very first post at the top of the forum will help you understand how to proceed.  Her is a link to it:

    https://social.technet.microsoft.com/Forums/en-US/c47b1bc2-f7fd-4d2e-8ff2-e8a81ce090d4/this-forum-is-for-scripting-questions-rather-than-script-requests?forum=ITCG

    To learn PowerShell you can start with the below links:

    Learning to script properly with PowerShell

    .


    \_(ツ)_/

    Friday, November 8, 2019 12:57 AM
  • Here is a little teaser that will help you to understand the issues and how you will need to approach them once you have learned basic PS.

    The following will compare all files in the two lists.

    $filelist1 = Get-ChildItem c:\folder1\* -File -Recurse | Select-Object FullName, Length, LastWriteTime
    $filelist2 = Get-ChildItem c:\folder2\* -File -Recurse | Select-Object FullName, Length, LastWriteTime
    Compare-Object $filelist1 $filelist2
    


    \_(ツ)_/

    Friday, November 8, 2019 1:06 AM
  • If you need to retain the original objects you can do it like this:

    $filelist1 = Get-ChildItem c:\folder1\* -File -Recurse
    $filelist2 = Get-ChildItem c:\folder2\* -File -Recurse
    Compare-Object $filelist1 $filelist2 -Property FullName, Length, LastWriteTime
    


    \_(ツ)_/

    Friday, November 8, 2019 1:08 AM
  • So i actually already had that code, and was working through it the issue i was looking at was getting additional information that was not part of the original data set and then putting it together to make a single comparison. 

    I have another project that i am working on that is somewhat similar.  I have two old DHCP servers that need to get moved over from 2008R2 to 2016 and start using the new tech for high avail.  the reservations are were not maintained so i am trying to get the info off of them to know what came from where and clean up right now before i migrate. 

    This is the code that i ended up going with and i think with a little tweaking i can make it also work for the file compare that i want to do.  Was able to put this together by a few hours of looking at code online and some testing.  If anything I am curious if there are any ways to improve on what I am doing to make it more efficient. 

    If I'm understanding what i read online and looking at the code below.  the values that are in $reservations1, $reservations2, $myobj1, and myobj2 are considered objects from power shell's perspective and have properties etc to each item.  I am then putting them into an array within the $Res1Info and $Res2Info so that i can control the data and compare them with the combined data that i want to look at, while still passing through the extra info i am collecting so its part of my output. 

    $DhcpServer1 = "ServerName1.Domain.local"
    $DhcpServer2 = "ServerName2.Domain.local"
    $ScopeID = "IP.ADDRESS.OF.SCOPE"
    
    
    $reservations1 = Get-DhcpServerv4Reservation -ComputerName $DhcpServer1 -ScopeID $ScopeID
    $reservations2 = Get-DhcpServerv4Reservation -ComputerName $DhcpServer2 -ScopeID $ScopeID
    $Res1Info = @()
    $Res2Info = @()
    
    foreach ($res in $reservations1) {
    	$myobj1 = New-Object -TypeName PSObject
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name Name -Value $res.Name
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name IPAddress -Value $res.IPAddress
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name MAC -Value $res.ClientID
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name Server -Value $DhcpServer1
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name Description -Value $res.Description
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name Type -Value $res.Type
    	Add-Member -InputObject $myobj1 -MemberType NoteProperty -Name State -Value $res.AddressState
    	
    	$Res1Info += $myobj1
    }
    
    
    
    foreach ($res in $reservations2) {
    	$myobj2 = New-Object -TypeName PSObject
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name Name -Value $res.Name
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name IPAddress -Value $res.IPAddress
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name MAC -Value $res.ClientID
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name Server -Value $DhcpServer2
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name Description -Value $res.Description
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name Type -Value $res.Type
    	Add-Member -InputObject $myobj2 -MemberType NoteProperty -Name State -Value $res.AddressState
    
    	$Res2Info += $myobj2
    }
    
    
    $Results = Compare-Object $Res1Info $Res2Info -Property Name, MAC, IPAddress -PassThru | Select-Object -Property Server, Name, IPAddress, MAC, SideIndicator, Description, Type, State
    $Results | out-gridview
    


    • Edited by tszilagyi3 8 hours 48 minutes ago Add info
    8 hours 52 minutes ago
  • There is no need to use custom properties or to do most of what you are doing. Just compare the two initial objects and format the output. It takes three or four lines.


    \_(ツ)_/

    8 hours 49 minutes ago
  • Why reinvent the wheel when there are already file/directory folder comparison/synchronization tools available?

    -- Bill Stewart [Bill_Stewart]

    8 hours 30 minutes ago
    Moderator
  • In this case i need to get a list of what is inconsistent and am required to report what is out of sync prior to fixing it.
    7 hours 45 minutes ago
  • I refer the honorable gentleman to my first comment posted some 47 minutes ago. You don't need to script this yourself.

    -- Bill Stewart [Bill_Stewart]

    7 hours 42 minutes ago
    Moderator
  • I think my biggest issue is that the $reservations# variables have all the info i need in this case except the actual name of the server that i need to have included in the output. 

    So in the section where i am doing the add-member into the $myobj# section i am adding most of the data from the reservations variable but pulling the server name which i could not get out of that same variable from what i pass down to get the completed data set to compare and then build the final output.

    7 hours 40 minutes ago
  • Just to add, I am not against learning how to properly code in power shell, right now i just have an immediate need to take care of something so i am attempting to solve this and will be following up with some time to study as 
    7 hours 38 minutes ago
  • Just to add, I am not against learning how to properly code in power shell, right now i just have an immediate need to take care of something so i am attempting to solve this and will be following up with some time to study as 

    Which is fundamentally saying that you want others to do your work for you because you don't have time to do it yourself.

    We have a number of ways for you to do this.  The script Gallery is the first place to look for solutions.  The second is to take basic advice from thoise with experience.  The third is to use your issue to help you research the technology and learn how to design a solution.

    Using RoboCopy will give you everything you ask for faster then any other method.  It also takes no scripting skills to run as it is a simple command line utility and is available on all Windows systems.

    Have you tried RoboCopy yet?

    if this is mission critical then why aren't you just contacting a consultant to solve the issue.  If it is not critical then you should be researching and searching for a prewritten solution.

    My earlier post criticizes you need to create custom objects.  Eliminate them and you have a solution that compare-object will display.  just reliable the output to make the "SideIndicator" the server name.  It is that simple however RoboCopy will be faster and more informative for non-technical humans.

    help compare-object -online

    RoboCopy /?

    To relabel the sideindictor use a calculated select-object property.

    help select-object -online

    The first requirement for anyone wanting to become a technician is to learn to research all issues before asking for help.  This may mean learning enough to understand how to research something outside of the current question.  Don't assume the solution before researching the problem.  As Bill noted above, there are many utilities available that can do this comparison to any lever of detail and produce excellent and useful reports.  PowerShell is more suited to techs trained in a technology and trained in PowerShell.


    \_(ツ)_/

    6 hours 31 minutes ago