locked
Powershell Compare command RRS feed

  • Question

  • Powershell script what I create will verify to imported AD csv files and this is also working. Now the functionality I would like to see in my exported is also the old ParentContainer only cannot get the information out see the following script:

    # Import new and week old .csv file and compare the differents removed
    $CSV1 = './Servers moved Last Week/servers.csv'
    $CSV2 = './Servers moved Last Week/servers_lstwk.csv'
    $ff = Import-Csv $csv1
    $sf = Import-Csv $csv2
    Compare-Object -referenceobject $ff -differenceobject $sf -includeequal -Property "Name", "ParentContainer" -PassThru | Where-Object{$_.SideIndicator -eq '=>' } | 
    Select-Object  "Name", "ParentContainer", "OSName", "operatingSystemServicePack", "whenCreated", "LastLogonTimestamp", "AccountIsDisabled" | Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'

    Wednesday, April 9, 2014 12:08 PM

Answers

  • $CSV1='servers.csv'
    $CSV2='servers_lstwk.csv'
    $ff = Import-Csv $CSV1
    $sf = Import-Csv $CSV2
    $compare = Compare-Object $ff $sf  -Property Name, ParentContainer | group name |?{$_.count -eq 2}
    $compare |
         ForEach-Object{
            $oldParent=($_.Group|sort sideindicator)[1].ParentContainer
            $obj=($_.Group|sort sideindicator)[0]
            $obj | Add-Member -MemberType NoteProperty -Name PreviousParent -Value $oldParent
            $obj
        } | 
        Format-Table -Property Name, ParentContainer,PreviousParent -auto


    ¯\_(ツ)_/¯

    • Marked as answer by JNH Budding Friday, April 18, 2014 9:43 AM
    Friday, April 18, 2014 9:05 AM

All replies

  • What do you mean you cannot get the information out?  Why do you quote everything.

    If something is not in the CSV file then it will not be in the results.

    This is all you need:

    Compare-Object $ff $sf -includeequal -Property Name,ParentContainer -PassThru |
         Where-Object{$_.SideIndicator -eq '=>' } |
         Select-Object  Name, ParentContainer,OSName,operatingSystemServicePack,whenCreated,LastLogonTimestamp,AccountIsDisabled |
         Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'

    If you have DistinguishedName in the files then this would be a better compare choice.


    ¯\_(ツ)_/¯

    Wednesday, April 9, 2014 3:28 PM
  • Hi jrv your correct I don't need to quote everything only this should make any change to the outcome the information I would like to get out of the csv file are the following field:

    Name, ParentContainer, Old ParentContainer, OSName,operatingSystemServicePack,whenCreated,LastLogonTimestamp,AccountIsDisabled

    Only how do I get the old ParentContainer information out of the system?

    Tuesday, April 15, 2014 11:48 AM
  • You have to capture both sides and save the left side OU.

    Use add-member to add it to the right side object and output updated right side object.


    ¯\_(ツ)_/¯

    Tuesday, April 15, 2014 1:40 PM
  • Hi can you maybe help me a bit here who do I set this up with the current code?

    Wednesday, April 16, 2014 10:37 AM
  • Now created the following script only still don't get the oldparentcontainer out in the csv file.

    # Import new and week old .csv file and compare the differents moved
    $objcol = @()
    $CSV1 = './Servers moved Last Week/servers.csv'
    $CSV2 = './Servers moved Last Week/servers_lstwk.csv'
    $ff = Import-Csv $csv1
    $sf = Import-Csv $csv2
    $compare = Compare-Object -referenceobject $sf -differenceobject $ff -includeequal -Property "Name", "ParentContainer" -PassThru | Where-Object{$_.SideIndicator -eq '=>' }  
    
    
    ForEach ($objItem in $compare)
        {
            $obj = New-Object PSobject 
            $obj | Add-Member -MemberType NoteProperty –Name Name –Value $objItem.Name
            $obj | Add-Member -MemberType NoteProperty –Name ParentContainer –Value $objItem.ParentContainer 
            $obj | Add-Member -MemberType NoteProperty –Name OldParentContainer –Value $ff.ParentContainer 
         }
    $obj | Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'


    Wednesday, April 16, 2014 11:57 AM
  • You need both sides because one side has the original parent and the other the new parent.  For each difference you will have a => and a <= side.


    ¯\_(ツ)_/¯

    Wednesday, April 16, 2014 9:02 PM
  • Created the following script but still the same issue what am I missing here?

    # Import new and week old .csv file and compare the differents moved
    $objcol = @()
    $CSV1 = './Servers moved Last Week/servers.csv'
    $CSV2 = './Servers moved Last Week/servers_lstwk.csv'
    $ff = Import-Csv $csv1
    $sf = Import-Csv $csv2
    #$compare = Compare-Object -referenceobject $ff -differenceobject $sf -includeequal -Property "Name", "ParentContainer" -PassThru
    $compare = Compare-Object $sf $ff  -Property Name, ParentContainer | 
    Select-Object  Name, ParentContainer, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled 
    
    ForEach ($objItem in $compare)
        {
            $obj = New-Object PSobject 
            $obj | Add-Member -MemberType NoteProperty –Name Name –Value $objItem.Name
            $obj | Add-Member -MemberType NoteProperty –Name ParentContainer –Value $objItem.ParentContainer | Where-Object{$_.SideIndicator -eq '=>' }
            $obj | Add-Member -MemberType NoteProperty –Name OldParentContainer –Value $objItem.ParentContainer | Where-Object{$_.SideIndicator -eq '<=' }
        }
    
    $obj | Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'

    Thursday, April 17, 2014 9:16 AM
  • What is not working?  Why not try placing trace statements in the script to see what is happening.


    ¯\_(ツ)_/¯

    Thursday, April 17, 2014 12:41 PM
  • Question can you write something out in script for me to help me out. The information I have in the csv files 1 and 2 are like this.

    servers.csv

    Name		ParentContainer			OSName						operatingSystemServicePack	whenCreated		lastLogonTimestamp	AccountIsDisabled
    INVSFOXWDC03	corp.com/Workstations		Windows Server? 2008 Enterprise without Hyper-V	Service Pack 2			10/13/2008 11:08	2/6/2014 12:29		FALSE
    INVSDDOWDC01	corp.com/Domain Controllers	Windows Server? 2008 Enterprise without Hyper-V	Service Pack 2			10/6/2008 14:21		2/6/2014 19:23		FALSE
    INVSHOUWDC03	corp.com/Domain Controllers	Windows Server? 2008 Enterprise without Hyper-V	Service Pack 2			9/18/2008 12:15		2/9/2014 5:10		FALSE
    

    servers_lstwk.csv

    Name		ParentContainer			OSName						operatingSystemServicePack	whenCreated		lastLogonTimestamp	AccountIsDisabled
    12345		corp.com/Domain Controllers	Windows Server? 2008 Enterprise without Hyper-V	Service Pack 2			10/13/2008 11:08	2/6/2014 12:29		FALSE
    INVSFOXWDC03	corp.com/Domain Controllers	Windows Server? 2008 Enterprise without Hyper-V	Service Pack 2			10/13/2008 11:08	2/6/2014 12:29		FALSE
    INVSDDOWDC01	corp.com/Domain Controllers	Windows Server? 2008 Enterprise without Hyper-V	Service Pack 2			10/6/2008 14:21		2/6/2014 19:23		FALSE
    

    Powershell script to find the ParentContainers moved is like this.

    # Import new and week old .csv file and compare the differents moved
    $objcol = @()
    $CSV1 = './Servers moved Last Week/servers.csv'
    $CSV2 = './Servers moved Last Week/servers_lstwk.csv'
    $ff = Import-Csv $csv1
    $sf = Import-Csv $csv2
    #$compare = Compare-Object -referenceobject $ff -differenceobject $sf -includeequal -Property "Name", "ParentContainer" -PassThru
    $compare = Compare-Object $ff $sf  -Property Name, ParentContainer | 
    Select-Object  Name, ParentContainer, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled 
    
    ForEach ($objItem in $compare)
        {
            $obj = New-Object PSobject 
            $obj | Add-Member -MemberType NoteProperty –Name Name –Value $objItem.Name
            $obj | Add-Member -MemberType NoteProperty –Name ParentContainer –Value $objItem.ParentContainer | Where-Object{$_.SideIndicator -eq '=>' }
            $obj | Add-Member -MemberType NoteProperty –Name OldParentContainer –Value $objItem.ParentContainer | Where-Object{$_.SideIndicator -eq '<=' }
        }
    
    $obj | Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'

    Output I get from the csv is as followed:

    Name            ParentContainer           OldParentContainer

    INVSFOXWDC03    corp.com/Workstations     corp.com/Workstations

    The information I would like to get out is:

    Name            ParentContainer           OldParentContainer

    INVSFOXWDC03    corp.com/Workstations     corp.com/Domain Controllers


    Friday, April 18, 2014 8:07 AM
  • The files you have posted are useless as they are not CSV files.  CSV files are comma separated with quotes and a header.  Your files are just plain text.


    ¯\_(ツ)_/¯

    Friday, April 18, 2014 8:10 AM
  • Did that to make it better visible.

    Here again

    servers.csv

    "Name","ParentContainer","OSName","operatingSystemServicePack","whenCreated","lastLogonTimestamp","AccountIsDisabled"
    "INVSFOXWDC03","corp.com/Workstations","Windows Server? 2008 Enterprise without Hyper-V","Service Pack 2","10/13/2008 11:08:11 AM","2/6/2014 12:29:19 PM","False"
    "INVSDDOWDC01","corp.com/Domain Controllers","Windows Server? 2008 Enterprise without Hyper-V","Service Pack 2","10/6/2008 2:21:14 PM","2/6/2014 7:23:38 PM","False"
    "INVSHOUWDC03","corp.com/Domain Controllers","Windows Server? 2008 Enterprise without Hyper-V","Service Pack 2","9/18/2008 12:15:04 PM","2/9/2014 5:10:43 AM","False"
    

    servers_lstwk.csv

    "Name","ParentContainer","OSName","operatingSystemServicePack","whenCreated","lastLogonTimestamp","AccountIsDisabled"
    "12345","corp.com/Domain Controllers","Windows Server? 2008 Enterprise without Hyper-V","Service Pack 2","10/13/2008 11:08:11 AM","2/6/2014 12:29:19 PM","False"
    "INVSFOXWDC03","corp.com/Domain Controllers","Windows Server? 2008 Enterprise without Hyper-V","Service Pack 2","10/13/2008 11:08:11 AM","2/6/2014 12:29:19 PM","False"
    "INVSDDOWDC01","corp.com/Domain Controllers","Windows Server? 2008 Enterprise without Hyper-V","Service Pack 2","10/6/2008 2:21:14 PM","2/6/2014 7:23:38 PM","False"

    Friday, April 18, 2014 8:30 AM
  • $CSV1='servers.csv'
    $CSV2='servers_lstwk.csv'
    $ff = Import-Csv $CSV1
    $sf = Import-Csv $CSV2
    $compare = Compare-Object $ff $sf  -Property Name, ParentContainer | group name |?{$_.count -eq 2}
    $compare |
         ForEach-Object{
            $oldParent=($_.Group|sort sideindicator)[1].ParentContainer
            $obj=($_.Group|sort sideindicator)[0]
            $obj | Add-Member -MemberType NoteProperty -Name PreviousParent -Value $oldParent
            $obj
        } | 
        Format-Table -Property Name, ParentContainer,PreviousParent -auto


    ¯\_(ツ)_/¯

    • Marked as answer by JNH Budding Friday, April 18, 2014 9:43 AM
    Friday, April 18, 2014 9:05 AM
  • Thanks jrv changed the script a little bit for the export csv and also include the other tables.

    # Import new and week old .csv file and compare the differents moved
    $CSV1 = './Servers moved Last Week/servers.csv'
    $CSV2 = './Servers moved Last Week/servers_lstwk.csv'
    $ff = Import-Csv $CSV1
    $sf = Import-Csv $CSV2
    $compare = Compare-Object $ff $sf  -Property Name, ParentContainer, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled | group name |?{$_.count -eq 2}
    $compare |
         ForEach-Object{
            $oldParent=($_.Group|sort sideindicator)[1].ParentContainer
            $obj = ($_.Group|sort sideindicator)[0] 
            $obj| Add-Member -MemberType NoteProperty -Name PreviousParent -Value $oldParent
            $obj
        }  
         
    $obj| Select-Object Name, ParentContainer,PreviousParent, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled |Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'

    Friday, April 18, 2014 9:45 AM
  • Unfortunately what you have done will not work.  It will never give you an answer or any reliable output.


    ¯\_(ツ)_/¯

    Friday, April 18, 2014 9:50 AM
  • Why not this is the total script with the AD gathering. This will run weekly.

    # Script to discover all Server Operating Systems from Active Directory.
    # Then export the data to .csv file and mail it to with the .csv file as attachment 
    # 
    # Created by Jeroen Budding
    
    # Rename week old .csv file
    Remove-Item './Servers Removed Last Week/servers_lstwk.csv'
    $date = (get-date).AddDays(-3)
    get-childitem './Servers Removed Last Week/servers.csv' | where-object {$_.LastWriteTime -le $date} | rename-item -newname { $_.name -replace '\.csv','_lstwk.csv' }
    
    #AD structure
    $ADTargetall = 'DC=corp,DC=com'
    $ADTarget1 = 'OU=Baarn,OU=EMEA,OU=IOM,OU=Invensys Business Units,DC=corp,DC=com'
    $ADTarget2 = 'OU=Antwerp,OU=EMEA,OU=IOM,OU=Invensys Business Units,DC=corp,DC=com'
    $ADTargets = $ADTargetall
    
    #$comp = GET-QADCOMPUTER –SearchRoot $ADTargets -SizeLimit 0 | WHERE { $_.CreationDate.CompareTo( $COMPAREDATE.AddDays(-7) ) –eq $TRUE -and $_.DN -like "*ou=Servers*" }
    #$comp = GET-QADCOMPUTER –SearchRoot $ADTargets -SizeLimit 0 -OSName 'Windows XP*' | WHERE { $_.CreationDate.CompareTo( $COMPAREDATE.AddDays(-7) ) –eq $TRUE }
    $comp = GET-QADCOMPUTER -IncludedProperties LastLogonTimestamp –SearchRoot $ADTargets -SizeLimit 0 -OSName '*Server*' | WHERE { $_.DN -notlike "*ou=IR*"  } | Select-Object Name, ParentContainer, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled | Export-Csv -NoTypeInformation -Path './Servers Removed Last Week/servers.csv'
    
    # Import new and week old .csv file and compare the differents moved
    $CSV1 = './Servers moved Last Week/servers.csv'
    $CSV2 = './Servers moved Last Week/servers_lstwk.csv'
    $ff = Import-Csv $CSV1
    $sf = Import-Csv $CSV2
    $compare = Compare-Object $ff $sf  -Property Name, ParentContainer, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled | group name |?{$_.count -eq 2}
    $compare |
         ForEach-Object{
            $oldParent=($_.Group|sort sideindicator)[1].ParentContainer
            $obj = ($_.Group|sort sideindicator)[0] 
            $obj| Add-Member -MemberType NoteProperty -Name PreviousParent -Value $oldParent
            $obj
        }  
         
    $obj| Select-Object Name, ParentContainer,PreviousParent, OSName, operatingSystemServicePack, whenCreated, LastLogonTimestamp, AccountIsDisabled |Export-Csv -NoTypeInformation -Path './Servers moved Last Week/serversmoved.csv'

    Friday, April 18, 2014 9:59 AM
  • Mike and I gave you exactly what you said you needed.  You then immediately made changes without understanding how it works.  THe changes have invalidated the compare and you are not getting the results.

    After you have run sufficient data to detect the errors you can post back with questions as to why.

    I will leave it up to you.  Just realize that your output is not what you asked for and not what you think it is going to be.  Run 30 or 40 records through the filter and you will see what I mean.  Your test is with just one record which will always work. 


    ¯\_(ツ)_/¯

    Friday, April 18, 2014 10:07 AM
  • agree you can see it only as format table only how do I export this data to csv?

    Because a the moment I only get one line exported

    Friday, April 18, 2014 10:42 AM
  • Hi guys you didn't really gave me the information I needed because I asked to get it exported to CSV already now have a powershell engineer here in my company will ask him what's going wrong.

    I posted this information to get some help now get played with Guys see my question also showed the csv exports which you didn't setup see why you get the info only because of the empty $obj

    Friday, April 18, 2014 11:01 AM
  • Hi guys you didn't really gave me the information I needed because I asked to get it exported to CSV already now have a powershell engineer here in my company will ask him what's going wrong.

    I posted this information to get some help now get played with Guys see my question also showed the csv exports which you didn't setup see why you get the info only because of the empty $obj

    Your issue is with the compare.  It will not work as you expect.  You will get mis-compares because you are including all fields.

    We did this to show you how.  You asked specifically for three fields.  I did that so you could see how you have to compare objects and select the correct components.  Apparently you want something else.  You have a new spec that says you want to compare all fields and create an output if any field changes.  This will nearly always produce a match.

    We are not here to write scripts on spec.  This form is for technicians and not end users. We thought you were trying to learn how to write a script and not asking for us to provide a complete and tested solution.

    I will tell you as I already have.  You cannot compare all fields.  YOU can pass the whole object.  You do NOT want to use the select as you have.

    To export you must use the pipeline and not just a single object.

    You have all of the pieces you need to make this work.  Now you need to try and understand how it works.

    If you have an expert in your company then why are you posting here?  It would seem more efficient to let you company expert do this for you.

    Anyway - good luck. It may take a while but, if you keep working at it, you will learn PowerShell.


    ¯\_(ツ)_/¯

    Friday, April 18, 2014 11:17 AM
  • Guys that guy is moved to the business so not really IT anymore send him a mail to look at it. Still say to me before it not possible and how it needs to be done then I can look into it. Already now senior information engineer only want to improve my powershell knowledge that why I ask these questions on the forum.

    I don't want to send it normally to him because I want to understand how it and setup the scripts myself (learn allot more from it).

    Also think I already know how to get the information out of the system. I think the trick is to compare the parentcontainer and name field export that one. And then combine the data with csv import.

    Hopefully it can also be done nicer but maybe the export from my company can help me out with this.

    Friday, April 18, 2014 11:45 AM
  • Start here:

    HELP Compare-Object -full
    HELP Export-Csv -Full

    Read carefully all parts and study the examples:

    For more information go here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Friday, April 18, 2014 12:31 PM