简体   繁体   English

连续将XML导入Powershell脚本

[英]Consecutively import XMLs into powershell script

I have a project to create a script to handle our Lync provisioning - trying to keep it as modular as possible. 我有一个项目来创建一个脚本来处理我们的Lync设置-尝试使其尽可能保持模块化。 I think this works best by keeping each script file in its own directory, with its own 'scopedConfig' & a 'centralXML' with tags that are globally useful. 我认为,通过将每个脚本文件保存在自己的目录中,使用自己的“ scopedConfig”和带有全局可用标签的“ centralXML”,效果最好。

The problem I have is when trying to import 2 XML files consecutively using Get-content. 我遇到的问题是尝试使用Get-content连续导入2个XML文件时。

The code I have is: 我的代码是:

# Import CentralConfig
[xml]$centralXML = Get-content ".\centralConfig.xml"

# Import ScopedConfig
[xml]$scopedXML = Get-content ".\ExecutionResources\ScopedConfigfiles\HostConsole\config.xml"

I'm testing successful import with the following: 我正在测试使用以下命令成功导入:

"------------------"
"------------------"
$centralXML
"------------------"
$scopedXML
"------------------"
"------------------"

The result and where the issue lies is the output: 结果和问题所在是输出:

------------------
------------------
Global_ConfigRoot
-----------------
Global_ConfigRoot
------------------

------------------
------------------

I expected the root nodes of both XMLs as output, instead I only get the root node of the first. 我希望两个XML的根节点都作为输出,相反,我只能得到第一个XML的根节点。 If I swap it round so that $scopedXML is first - I only get $scopedXML root node and nothing from $centralXML. 如果我将它四处交换,那么$ scopedXML是第一个-我只会得到$ scopedXML根节点,而从$ centralXML中什么也不会得到。

An interesting thing to note is that if I pipe the 2nd XML to Get-member - where you'd usually expect to see all XML related things and it's properties, I get a whole load of blankness: 需要注意的一件有趣的事情是,如果我将第二个XML传递给Get-member,您通常希望在其中看到所有与XML相关的事物及其属性,那么我会感到很空白:

------------------
------------------
Global_ConfigRoot
-----------------
Global_ConfigRoot
------------------














































------------------
------------------

I have never come across this before, does anyone have any suggestions? 我以前从未见过,有人有任何建议吗?

(PS Tried my best to keep to the posting rules, let me know if anything is wrong it's my first time posting here) (PS竭尽全力遵守发布规则,如果有任何问题,请通知我,这是我第一次在此处发布)

A function, cmdlet or script can only return AN object. 函数,cmdlet或脚本只能返回AN对象。 It you return mulitple like you do by calling both variables, it actually returns an object[] array containing both items (+ the lines you printed). 它像调用两个变量一样返回多个字符,它实际上返回包含两个项目(+打印行)的object[]数组。 The objects are untouched and just added to this collection. 这些对象保持不变,仅添加到此集合中。

Now, when your script outputs(displays) the object that was returned, it formats it as a table since there were multiple objects inside. 现在,当脚本输出(显示)返回的对象时,由于其中包含多个对象,因此会将其格式化为表格。 A table can only contain 1 header(column-names), and when displaying an array, it takes the columns(properties) from the first item, in this case $centralxml . 一个表只能包含1个标题(列名),并且在显示数组时,它会从第一项(在本例中$centralxml )中获取column(属性)。 That's why you get 2 columns: xml and servers. 这就是为什么您会获得2列的原因:xml和server。

If you want to seperate them you have to tell the script to format them in seperate tables. 如果要分隔它们,则必须告诉脚本以分隔表的格式对其进行格式化。 Ex: 例如:

# Import CentralConfig
[xml]$centralXML = Get-content ".\test.xml"

# Import ScopedConfig
[xml]$scopedXML = Get-content ".\test2.xml"

$centralXML | Format-Table
$scopedXML | Format-Table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM