开发者

Can't access XML element while iterating through Powershell loop

开发者 https://www.devze.com 2023-03-22 04:13 出处:网络
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.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消