【问题标题】:powershell - loop through file directory of xml files and replace specific nodes for each file [duplicate]powershell - 遍历xml文件的文件目录并替换每个文件的特定节点[重复]
【发布时间】:2018-12-15 05:29:16
【问题描述】:

我正在尝试遍历仅包含 xml 文件的特定文件目录,并替换每个文件中特定定义的节点。我现在的问题是文件路径被定义为 c:\users\myusername\filepath 而不是坚持脚本中定义的原始文件路径。这发生在我的 $xml 分配期间,因为 $filepath 变量对于脚本中的其他命令正常工作。我不能在这里使用 Get-Content 吗?我的印象是 get-content 应该可以工作,因为它一次编辑 1 个文件。

当前代码:

#updating xml and not including sub-directories
           $filepath = "\\server1\SP\ConsensusModeling" #folder with multiple xml files, this is NOT located in my \users\ folder.
           Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){

            $xml = [xml](Get-Content $file)

            #updating attributes at each node
            $node = $xml.ConsensusModelingConfig #location of internal node tree
            $node.APPServer = "server1"
            $node.APPDB = "ConsensusModelingAPP"
            $node.SSASServer = "server1SSAS"
            $node.SSASDB = "ConsensusModelingDB"
            $xml.Save($filePath)

            }

我正在尝试编辑的文件夹中的文件示例/我正在尝试创建的结果。这些当前是不同的值,我想将它们设置为以下值。其中有 4 个。 ConsensusModeling.xml:

<ConsensusModelingConfig>
  <APPServer>Server1</APPServer>
  <APPDB>ConsensusModelingAPP</APPDB>
  <SSASServer>Server1SSAS</SSASServer>
  <SSASDB>ConsensusModelingDB</SSASDB>
</ConsensusModelingConfig>

错误信息:

Get-Content : Cannot find path 'C:\Users\myusername\mytestfile
At \\scriptlocation.ps1:628 c
+                 $xml = [xml](Get-Content $file)
+                              ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: 
(C:\Users\Alex.p...eling_Score.xml:String) [G
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

根据错误消息,我的路径被分配给 C:\users 等,而不是保留在 $filepath 变量中定义的原始文件路径。这是为什么呢?

解决方案: 根据@mklement0 的评论,以下代码修复了我的问题,现在所有文件的属性都已正确更新。

#updating xml and not including sub-directories
           $filepath = "\\server1\SP\ConsensusModeling" #folder with multiple xml files, this is NOT located in my \users\ folder.
           Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){

            $xml = [xml](Get-Content $file.FullName)

            #updating attributes at each node
            $node = $xml.ConsensusModelingConfig #location of internal node tree
            $node.APPServer = "server1"
            $node.APPDB = "ConsensusModelingAPP"
            $node.SSASServer = "server1SSAS"
            $node.SSASDB = "ConsensusModelingDB"
            $xml.Save($file.FullName)

            }

【问题讨论】:

  • 使用PSv3+时建议改成:Foreach($file in (Get-ChildItem "$filePath\*.xml" -File)){
  • $file 字符串化为 仅仅是文件名,而不是完整路径,因为使用了 Get-ChildItem 命令的细节 - 有关详细信息,请参阅链接帖子。使用$file.FullName 是一种简单的解决方法。

标签: xml powershell


【解决方案1】:

您是从 c:\users... 运行脚本吗?

这是你可以做的事情:

$filepath = "\\server1\SP\ConsensusModeling"
Get-ChildItem $filePath | % {

        $XML = import-clixml $_.FullName #this will get you the childitem full path.
        #
        #updating attributes at each node
        $node = $xml.ConsensusModelingConfig #location of internal node tree
        $node.APPServer = "server1"
        $node.APPDB = "ConsensusModelingAPP"
        $node.SSASServer = "server1SSAS"
        $node.SSASDB = "ConsensusModelingDB"
        $xml.Save($filePath)


}

【讨论】:

  • 嗯,我可能是。我通过键入 .(space) "\\scriptpath" 然后调用该函数来运行脚本,这可能意味着我正在从 c:\users 运行它。我尝试了您的代码但得到了错误:Element 'Objs' with namespace找不到名称“schemas.microsoft.com/powershell/2004/04”。我现在在谷歌上搜索它
  • @Hamza:使用$_.FullName 强制使用完整路径是一个不错的技巧,但请注意Import-CliXml cmdlet不是通用XML-解析 cmdlet,所以这不起作用。
  • 我不知道。谢谢。
猜你喜欢
  • 2019-01-16
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多