【问题标题】:How can I use xml for Powershell GUI to get version num of PS tool?如何使用 xml for Powershell GUI 获取 PS 工具的版本号?
【发布时间】:2021-10-31 02:16:14
【问题描述】:

我最近开始学习使用 xml 和 Powershell 来创建 GUI。我的目标是创建一个关于框。我使用 VSCode 和 Notepad++(对我来说没有花哨的 Visual Studio,无法获得许可)。我还没有弄清楚如何将我的 powershell 工具的版本号放入 xml 中以便显示。以下是一些代码片段

<#
File: tool.ps1

Author: Me   Updated: 2021/09/01   Version: 0.1.2
   Change notes ... 
   Blah Blah .

Author: Me   Created: 2021/08/01   Version: 0.1.1
   Basic Script notes for version number

#>

我们从编译脚本中获取文件版本信息,它从 .ps1 文件并将其插入到可执行文件中。 “authorLines”看起来像 我的评论区在上面。注意:我们使用 Microsoft 的 PS2Exe 和我们的编译器。这 检索版本的代码块是:

$authorLines = get-content $srcFile | Where-Object {$_ -match 'Author'}
      if((Get-ArraySize $authorLines) -gt 1) {
         $fileVersion = ($authorLines[0] -split ' ')[-1]
      } elseif ((Get-ArraySize $authorLines) -eq 1){
         $fileVersion = ($authorLines -split ' ')[-1]
      }else{
         $fileVersion = $null
      }

这是我的 .ps1 中用于从可执行文件中获取文件版本信息的代码

$fileVersion = (Get-Command '.\tool.exe').FileVersionInfo.FileVersion

以下是一个单独的 xml 文件(对于我们使用的工具是必需的。 我们不能在 Powershell 脚本中包含我们的 xml!)。我的 xml 更长 有更多的数据,但这是重要的部分

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="about"
Width="400"
Height="550"
Title="About">
<Grid x:Name="Grid"
      Margin="10,10,10,10">
   
   <Grid.RowDefinitions>
      <RowDefinition Height="40"/>
      <RowDefinition Height="40"/>
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"/>
   </Grid.ColumnDefinitions>

<!-- Begin About Properties -->    
   <Label Grid.Row="0" 
          Grid.Column="0" 
          x:Name="label1" >
   <Run FontFamily="Calibri" FontWeight="Bold" FontSize="20" Text="Tool Name Here"  /> 
      </Label>
   <Label Grid.Row="1" 
          Grid.Column="0" 
          x:Name="label2" 
         >
   <Run FontFamily="Calibri" FontWeight="Bold" FontSize="20" Text="Tool Version Number: "  /> 
   </Label>
</Grid>
</Window>

提取 GUI xml 的代码(retrieveFile 是我的团队创建的一个 powershell 模块,它在命名目录中搜索命名文件;所有错误检查都在模块中完成):

$aboutXaml = retrieveFile -folderPath "F:\powershellGUI" -fileSelect "about.xml"
$global:aboutWindow = New-Object xml
$global:aboutWindow.Load($aboutXaml)

那么,有没有一种从工具中提取文件版本信息并将其放入 xml 的好方法?除了手动输入 - 我们要确保它保持最新?例如,我正在添加 VScode about 框的屏幕截图。我不太担心大多数其他信息,只是如何在其中获取版本号。任何帮助表示赞赏

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    在基本 XML 中使用易于识别的模板字符串,并将其替换为文件中的版本作为构建步骤。在您的 XML 中,将 XML 元素的 Text 属性更改为使用可替换值,例如 {{VERSION_NUMBER}}

    <Run FontFamily="Calibri" FontWeight="Bold" FontSize="20" Text="Tool Version Number: {{VERSION_NUMBER}}" /> 
    

    然后作为构建步骤,将{{VERSION_NUMBER}} 替换为$fileVersion PS2EXE 将在运行后分配。假设 $xmlPath 是 XML 文件的路径:

    ( Get-Content -Raw $xmlPath ) -replace '{{VERSION_NUMBER}}', $fileVersion | Out-File $xmlPath -Force
    

    【讨论】:

      【解决方案2】:

      我还找到了一种相当简单的方法,使用上面已经存在的代码(将 label2 名称更改为 fileVersionInfo,并加载 $aboutBox 作为阅读器)。

      $fvInfo = $aboutBox.FindName("fileVersionInfo")
      $fvInfo.Content.Text = $fileVersion
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 2012-07-01
        相关资源
        最近更新 更多