I am creating a Powershell script that takes as input list of cluster names from file. Then it mades some changes to the names in order to derive the 2 nodes names by adding 1 letter to the name. After that it checks 2 files in each cluster node and compairs them by date modify. Stores the result as Custom PS opject in array. All works great. The only issue I have is that if I call in the end the two arrays for the 2 files the output is not OK.
The Code:
$clusters=Get-Content C:\temp\clusters.csv
#Variables
$FileA="C$\Program Files\fileA.txt"
$FileB="C$\Program Files\fileB.txt"
$FileA_ar=@()
$FileB_ar=@()
foreach ($cluster in $clusters)
{
# Write-Host " "
# Write-Host "=============================================================="
# Write-Host "Checking $cluster" -ForegroundColor Cyan
$Xnode = "s"+$cluster.substring(1,9) + "X"
$Ynode = "s"+$cluster.substring(1,9) + "Y"
#checking if FileA is identical
# write-host "Checking iTernity.User.config files on $cluster" -ForegroundColor Yellow
$file1=\\$xnode\$FileA
$file2=\\$ynode\$FileA
$f1t=(Get-Item $file1).LastWriteTime
$f2t=(Get-Item $file2).LastWriteTime
if ($f1t -lt $f2t) #Checking if file1 last modified time is later than that on file 2
{
# Copy-Item $file1 $file1"old" -Force
# Copy-Item $file2 $file1 -Force
# write-host "$ynode newer than $xnode Copying file..."-ForegroundColor red
$FileA_arr+= New-Object -TypeName psobject -Property @{
Cluster=$cluster
UserConfig ="Not Synced"
Newer =$Ynode}
}
else
{if($f2t -lt $f1t)
{
# Copy-Item $file2 $file2"old" -Force
# Copy-Item $file1 $file2 -Force
# write-host "$xnode newer than $ynode Copying file..."-ForegroundColor red
$FileA_ar+= New-Object -TypeName psobject -Property @{
Cluster=$cluster
UserConfig ="Not Synced"
Newer =$xnode}
}
else
{
#Write-Host "Files identical"-ForegroundColor Green
$FileA_ar+= New-Object -TypeName psobject -Property @{
Cluster=$cluster
UserConfig ="Synced"
Newer ="NA"}
}}
# Write-Host "--------------------------------------------------------------"
# write-host "Checking FileB files on $cluster" -ForegroundColor Yellow
$file1=\\$xnode\$FileB
$file2=\\$ynode\$FileB
$f1t=(Get-Item $file1).LastWriteTime
$f2t=(Get-Item $file2).LastWriteTime
if ($f1t -lt $f2t)
{
# Copy-Item $file1 $file1"old"
# Copy-Item $file2 $file1 -Force
# write-host "File on $yNode newer than $xnode! Copying file..."-ForegroundColor red
$FileB_ar+= New-Object -TypeName psobject -Property @{
Cluster=$cluster
$FileB ="Not Synced"
Newer =$Ynode}
}
else
{if($f2t -lt $f1t) {
# Copy-Item $file2 $file2"old"
# Copy-Item $file1 $file2 -Force
# write-host "File on $xnode newer than $ynode! Copying file..."-ForegroundColor red
$FileB_ar+= New-Object -TypeName psobject -Property @{
Cluster=$cluster
$FileB ="Not Synced"
Newer =$xnode}
}
else
{
# Write-Host "Files identical"-ForegroundColor Green
$FileB_ar+= New-Object -TypeName psobject -Property @{
Cluster=$cluster
$FileB ="Synced"
Newer ="NA"}
}} }
$FileB_ar
$FileA_ar
The Output of this code looks like that:
Cluster FileA Newer
------- ---------- -----
Cluster1nodeA Synced NA
Cluster1nodeB Synced NA
Cluster2nodeA Synced NA
Cluster2nodeB Synced NA
Cluster3nodeA Synced NA
Cluster3nodeB Synced NA
Cluster1nodeA NA
Cluster1nodeB NA
Cluster2nodeA NA
Cluster2nodeB NA
Cluster3nodeA NA
Cluster3nodeB NA
If I call the variables manualy after the script is executed I got the correct output:
PS C:\ $FileA
Cluster FileA Newer
------- ---------- -----
Cluster1nodeA Synced NA
Cluster1nodeB Synced NA
Cluster2nodeA Synced NA
Cluster2nodeB Synced NA
Cluster3nodeA Synced NA
Cluster3nodeB Synced NA
PS C:\ $FileB
Cluster FileB Newer
------- ---------- -----
Cluster1nodeA Synced NA
Cluster1nodeB Synced NA
Cluster2nodeA Synced NA
Cluster2nodeB Synced NA
Cluster3nodeA Synced NA
Cluster3nodeB Synced NA
I do not know why this happens. I tried diferent varians by adding wait time between the variables and so on but it aways connects the output of both variables.. Does someone knows why this happens?
Thanks in advance!