简体   繁体   English

如何从输出中获取版本号

[英]How to get a version number from an output

R&D has asked us to deploy a new outlook plugin to all the systems in the company... however, there is a catch. 研发部门已要求我们在公司的所有系统中部署新的Outlook插件...但是,有一个陷阱。 If you have any other plugins that are outdated from our company it will break them.. As such we need to determine if an endpoint has the plugin installed and what version it is. 如果您还有我们公司提供的其他任何过时的插件,则会破坏它们。.因此,我们需要确定端点是否安装了插件以及插件的版本。 If its installed and less than version "X" then we need to update it. 如果已安装且版本低于“ X”,则我们需要对其进行更新。 Another issue is that not everyone uses the plugins so we cant just push it to everyone and call it a day. 另一个问题是,并非每个人都使用这些插件,因此我们不能仅将其推给所有人并每天使用。

As such we found a query that will call WMI and spit out the output which we then write to a file. 这样,我们发现了一个查询,该查询将调用WMI并吐出输出,然后将其写入文件。 Running 运行

"wmic product where "Vendor like '%Microsoft%'" get Name, Version > C:\temp\test.txt" 

gives us output: 给我们输出:

Name                                       Version  
First Plug-in for Microsoft Outlook      1.1.9.0  
Second Plug-in for Microsoft Outlook  2.0.2.0  
Third Plug-in for Microsoft Outlook    1.2.5.0  
etc.

What we want to do is to parse each one of those and write the version to a file so we can then see if we need to deploy the new plugin or not. 我们要做的是解析每个版本,并将版本写入文件中,这样我们就可以查看是否需要部署新插件。

Ex: 例如:

First.txt contains 1.1.9.0
Second.txt contain 2.0.2.0
etc.

If for example Test.txt has no "First" in it don't make a file at all. 例如,如果Test.txt中没有“ First”,则根本不制作文件。 We pretty much have to use batch file to do this but should be able to also do this with PowerShell (although that will take some tweaking as our deployment system isn't very powerful). 我们几乎必须使用批处理文件来执行此操作,但也应该能够使用PowerShell来执行此操作(尽管由于我们的部署系统功能不十分强大,因此需要进行一些调整)。

As such I was wondering if someone can point me in the right direction regarding how to do this on a Windows system. 因此,我想知道是否有人可以为我指出有关在Windows系统上执行此操作的正确方向。

Note : This answer answers the question as stated and may be of interest from a regex-matching perspective, but for a fundamentally better approach see rojo's comment on the question . 注意 :该答案回答了上述问题并且从正则表达式匹配的角度来看可能是有趣的,但是要获得从根本上更好的方法,请参阅rojo对这个问题的评论


The following Powershell pipeline writes to individual files, named for the product (with extension .txt ) and containing the version number, as requested: 以下Powershell管道根据要求写入单个文件,这些文件以产品命名(扩展名为.txt )并包含版本号:

Get-Content C:\temp\test.txt | Select-Object -Skip 1 | ForEach-Object `
  { 
     if ($_ -match '^(.*[^ ]) +([^ ]+) *$') { 
       $fname = $matches[1] + '.txt'
       $ver = $matches[2]
       $ver > $fname 
     } 
  }

Note that the output files are written to the current directory. 请注意,输出文件将被写入当前目录。

  • Get-Content C:\\temp\\test.txt sends the contents of the file line by line through the pipeline. Get-Content C:\\temp\\test.txt通过管道逐行发送文件的内容。

  • Select-Object -Skip 1 skips the header line. Select-Object -Skip 1跳过标题行。

  • $_ -match '^(.*[^ ]) +([^ ]+) *$' matches the line at hand ( $_ ) against a regular expression that uses capture groups ( (...) ) to capture the product name and the version number substrings, accessible through the elements of special match-info variable $matches . $_ -match '^(.*[^ ]) +([^ ]+) *$'将当前行( $_ )与使用捕获组( (...) )捕获正则表达式的正则表达式进行匹配产品名称和版本号子字符串,可通过特殊的match-info变量$matches的元素访问。

  • $fname = $matches[1] + '.txt' appends .txt to the 1st capture group - the product name - and stores it in variable $fname . $fname = $matches[1] + '.txt'.txt附加到第一个捕获组-产品名称-并将其存储在变量$fname

  • $ver = $matches[2] saves the version number - the 2nd capture group - in variable $ver . $ver = $matches[2]将版本号(第二捕获组$ver = $matches[2]保存在变量$ver

  • $ver > $fname simply writes the version number to the output file. $ver > $fname只是将版本号写入输出文件。

    • Note that PowerShell creates UTF-16 LE-encoded files when you use output redirection ( > ); 请注意,当您使用输出重定向( > )时,PowerShell会创建UTF-16 LE编码的文件。 to use a different encoding, use, eg, $ver | Out-File -Encoding utf8 使用不同的编码,请使用例如$ver | Out-File -Encoding utf8 $ver | Out-File -Encoding utf8 . $ver | Out-File -Encoding utf8

If you want to call the entire PowerShell command from cmd.exe (a batch file) , use the following: 如果要从cmd.exe (批处理文件)调用整个PowerShell命令 ,请使用以下命令:

powershell.exe -noprofile -command "Get-Content C:\temp\test.txt | Select-Object -Skip 1 | ForEach-Object { if ($_ -match '^(.*[^ ]) +([^ ]+) *$') { $fname = $matches[1] + '.txt'; $ver = $matches[2]; $ver > $fname } }"

To only use the 2nd whitespace-separated token of the plug-in name as the file name: 要将插件名称的第二个用空格分隔的标记用作文件名,请执行以下操作:

In both commands above, replace: 在以上两个命令中,替换为:

$fname = $matches[1] + '.txt'

with: 有:

$fname = (-split $matches[1])[1] + '.txt'

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

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