【发布时间】: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