【问题标题】:PowerShell Core `Select-Xml` Returns NothingPowerShell Core `Select-Xml` 不返回任何内容
【发布时间】:2019-11-06 18:41:10
【问题描述】:

我有一个想要通过 PowerShell Core 获取值的 XML 文件。

清理示例:

  <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <TargetFrameworks></TargetFrameworks>
    <RootNamespace></RootNamespace>
    <AssemblyName></AssemblyName>
    <Company></Company>
    <Authors></Authors>
    <Description></Description>
    <Copyright></Copyright>
    <AssemblyVersion></AssemblyVersion>
    <FileVersion></FileVersion>
    <Product></Product>
  </PropertyGroup>

  <PropertyGroup>
    <Version>1.0.0</Version>
    <RepositoryUrl></RepositoryUrl>
    <GeneratePackageOnBuild></GeneratePackageOnBuild>
  </PropertyGroup>

</Project>

我想获取&lt;Version&gt;元素的值,即1.0.0

我跑:

pwsh Select-Xml -Path test.csproj -XPath "//Version"

返回:

Node    Path                     Pattern
----    ----                     -------
Version D:\Downloads\test.csproj //Version

当我尝试按照 Microsoft 的文档获取值时,使用:

pwsh Select-Xml -Path test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node

我收到以下错误:

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Node    Path                     Pattern:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (----    ----                     -------:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Version D:\Download\u2026st.csproj //Version:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

如何获得这个 XML 元素的值?

【问题讨论】:

    标签: powershell-core


    【解决方案1】:

    Pwsh 默认使用 -file 而不是 -command。管道符号还有一个引用问题。这可行,假设您从 cmd 运行它:

    pwsh -c "Select-Xml //Version test.csproj | Select -Expand Node"
    
    #text
    -----
    1.0.0
    

    或者

    pwsh -c (Select-Xml //Version test.csproj).node
    

    如果这是从 unix 或其他版本的 powershell 运行,则会出现不同的问题。

    【讨论】:

    • 获取The term '-c' is not recognized as the name of a cmdlet, function, script file, or operable program.。这是针对 PowerShell Core 的? docs.microsoft.com/en-us/powershell/scripting/install/…
    • 这适用于 cmd.exe:pwsh -c 'hi there'(版本 6.2.3)
    • 奇怪。来自 cmd.exe 的pwsh -c 'hi there' 给了我同样的错误; The term '-c' is not recognized as the name of a cmdlet, function, script file, or operable program..
    • -command 给出相同的错误。pwsh 显示版本为PowerShell 6.2.3。它是在几小时前使用上面评论中发布的微软文档中的命令安装的。
    • Microsoft 的文档没有将 Command 显示为现有模块:docs.microsoft.com/en-us/powershell/module/…
    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多