Reputation: 203
I am looking to do a comparison on two variables $list
and $oldList
that I generated using Import/Export-Clixml
.
Here is the my reference question that I had previously asked. It contains more details if needed.
How should I store a reference variable for continued iteration in Powershell
Since, I tested the following script successfully:
$list = Get-ChildItem C:\localApps\AutomationTest\Loading | where {$_.PSIsContainer}
$list | Export-Clixml C:\LocalApps\AutomationTest\Loading\foldernames.xml
$oldList = Import-Clixml C:\LocalApps\AutomationTest\Loading\foldernames.xml
$oldList
My goal is to compare the $list.LastWriteTime
to the $oldList.LastWriteTime
and obtain any new directory names that were added to the list since the "oldList" was generated. These new Directory names will then be processed and added to the "oldList"...and so on.
Was thinking maybe something like the following could work?
Compare-Object -ref $oldList -diff $list
if ($list.LastWriteTime -gt $oldList.LastWriteTime}
"Continue....(Load lastest folder names into process)"
Upvotes: 0
Views: 1370
Reputation: 46730
Compare-Object
is what you are asking for. Just need to be sure you are doing the correct post processing. If you are just looking for changes that exist in the $list
then use the correct side indicator when filtering with Where-Object
.
Compare-Object $oldList $list | Where-Object{$_.Sideindicator -eq "=>"} | Select-Object -expandProperty InputObject
That will just return the Directory.Info objects that correspond to folders that didn't exist in the $oldList
. Capturing the output from that command is what you are doing to need for your other processing.
After that then just take $list
and output that to the location that $oldList
came from. Not much to it other than that.
Upvotes: 0
Reputation: 678
Here is an example of doing date time checking against the old XML for each folder item that existed previously. It will skip any that weren't in the old list.
Hopefully this gives you a good starting point.
$oldList = Import-Clixml #etc
function Check-Against ([PsObject]$oldList, [string]$path){
$currentItems = Get-ChildItem $path | ? {$_.PSIsContainer}
foreach ($oldItem in $oldList){
$currentItem = $currentItems | ? Name -like ($oldItem.Name)
if ($currentItem -ne $null){
$oldWriteTime = $oldItem.LastWriteTime
$val = $currentItem.LastWriteTime.CompareTo($oldWriteTime)
if ($val -gt 0){
# Folder has been changed since then
# Do your stuff here
}
if ($val -eq 0){
# Folder has not changed
# Do your stuff here
}
if ($val -lt 0){
# Somehow the folder went back in time or was restored
# Do your stuff here
}
}
}
}
Upvotes: 1