小编Ped*_*nto的帖子

通过String在PowerShell中调用函数

我正在尝试使用字符串"动态"调用Powershell函数.例

$functionToInvoke = "MyFunctionName";

Invoke-Function $functionToInvoke $arg1 $arg2  #  <- what I would like to do
Run Code Online (Sandbox Code Playgroud)

有没有办法用PowerShell 2.0实现这一目标?

powershell function powershell-2.0

18
推荐指数
2
解决办法
2万
查看次数

在PowerShell中打印XmlElement名称

我有一个XML文档:

<Root>
  <ItemA Name="1" />
  <ItemB Name="2" />
  <ItemC Name="3" />
</Root>
Run Code Online (Sandbox Code Playgroud)

以及从该文档访问数据的powershell脚本.我需要按Root的子节点进行迭代并打印其子节点的元素名称.例:

$xml = [xml](gc MyXmlFile.xml);

$xml.Root.Name 
# prints "Root"

$xml.Root.ChildNodes | foreach { $_.Name } 
# prints 1 2 3 because Item(A|B|C) have an attribute named "Name"
# I need to print ItemA ItemB ItemC
Run Code Online (Sandbox Code Playgroud)

更新:正如MrKWatkins在这种情况下正确指出的,我可以使用LocalName属性.但是,如果我使用的命名空间,如果我的XML中也有LocalName属性,那么这将无效.我想知道是否存在解决此问题的解决方案,无论XML文件如何,该问题始终有效.

xml powershell

6
推荐指数
1
解决办法
9873
查看次数

标签 统计

powershell ×2

function ×1

powershell-2.0 ×1

xml ×1