1

Let me start this off by saying that I'm an intern with no Powershell experience at all. I've put together this script as best I could and have hit a wall.

Basically, I've got a Powershell script that will query an XML list of servers & files to see if one of the files has been modified. I have no problem iterating through the farms and servers, but I get nothing when accessing attributes of the file.

$foreach($Farm in $serverList.Environment.Farm) {
Write-Host .FARM: $Farm.Name
foreach($Server in $serverList.Environment.Farm.Server) {
Write-Host ..SERVER: $Server.Name
foreach($File in $serverList.Environment.Farm.Server.File) {

$fullPath = "\\"+$Server.Name+"\"+($File.Path).Replace(":","$")+$File.Version+"\CONFIG\"+$File.Name
$lastModified = (Get-Item $fullPath).LastWriteTime.toString()

write-host ...FILEPATH: $fullPath $lastModified

... processing if/thens ...
}    }    }

If I'm going through 1 server's files, everything works perfectly. But, once I add another server to the XML file, I get null values when I try to build $fullPath with $File attributes.

Any help would be greatly appreciated.

1 Answer 1

5

Without sample data I can't test it, but try something like this:

foreach ($Farm in $serverList.Environment.Farm) {
    Write-Host ('.FARM: ' + $Farm.Name)
    foreach ($Server in $Farm.Server) {
        Write-Host ('..SERVER: ' + $Server.Name)
        foreach($File in $Server.File) {
            $fullPath = "\\"+$Server.Name+"\"+($File.Path).Replace(":","$")+$File.Version+"\CONFIG\"+$File.Name
            $lastModified = (Get-Item $fullPath).LastWriteTime.toString()
            write-host "...FILEPATH: $fullPath $lastModified"
#            ... processing if/thens ...
        }
    }
} 

The basic problem with your first attempt is that in the $Server loop, you need to iterate over children of $Farm and in the $File loop you need to iterate over children of $Server.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that worked! I wish I could give you an upvote, but sadly I don't have enough rep :( Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.