在遍历Powershell循环时不能访问XML元素

时间:2022-09-22 23:52:08

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.

首先,我要说我是一个没有Powershell经验的实习生。我已经尽我所能把这个剧本写好了。

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.

基本上,我有一个Powershell脚本,它将查询服务器和文件的XML列表,以查看其中一个文件是否已被修改。在农场和服务器之间进行迭代没有问题,但是在访问文件的属性时什么也得不到。

$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.

如果我遍历一个服务器的文件,一切都很好。但是,一旦我向XML文件添加了另一个服务器,当我尝试使用$ file属性构建$fullPath时,就会得到空值。

Any help would be greatly appreciated.

如有任何帮助,我们将不胜感激。

1 个解决方案

#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.

第一次尝试的基本问题是,在$Server循环中,需要迭代$Farm的子循环,在$File循环中需要迭代$Server的子循环。

#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.

第一次尝试的基本问题是,在$Server循环中,需要迭代$Farm的子循环,在$File循环中需要迭代$Server的子循环。