从字符串中提取数字的正则表达式 (gwmi Win32_OperatingSystem).Name
Regex to extract number from string (gwmi Win32_OperatingSystem).Name
我正在使用 (gwmi Win32_OperatingSystem).Name 来获取操作系统版本,以便配置脚本可以根据该版本选择适当的变量文件。但是,我想尽可能具体。我想将 OS 版本限制为 OS 数字(即 Windows 7 为 7,Windows 8 为 8,Windows Server 2012 为 2012, ETC)。这是我目前所拥有的:
(gwmi Win32_OperatingSystem).Name
产生:
Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition4
当我尝试将其限制为数字时:
(gwmi Win32_OperatingSystem).Name -replace '\D+(\d+)\D+',''
我得到:
100\Partition4
我只需要输出“10”。如何将我的正则表达式调整为仅 select 10(或 7,或 2012)?我考虑过以某种方式限制正则表达式在第一个遇到数字块后停止处理字符串(即点击一个数字然后在你点击 space 时停止)但我不知道如何做这样的事情.或者也许有更好的方法来获取我需要的信息?
提前致谢。
这输出“10”:
'Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition4' -replace '.*\s(\d+)\s.*',''
.*
: 任意字符 (.
) 0 次或多次 (*
)
\s
: 一个 space
\d+
: 一个数字 (\d
) 1 次或多次 (+
)
首先删除第一个 |
之后的所有内容,您不需要:
$OSName,$null = (Get-WmiObject Win32_OperatingSystem).Name.Split("|")
然后将正则表达式替换为结果字符串:
$OSVersion = $OSName -replace '\D+(\d+)\D*',''
执行此操作的最简单方法之一如下:
(gwmi Win32_OperatingSystem).Name -replace "(Harddisk.*)|(Partition.*)|([a-z])|(\)|(\|)|(:)"
其工作方式是首先替换 Harddisk
或 Partition
之后的所有数据,然后我们删除所有字母和特殊字符(\
或 |
) .
在那之后,我们只剩下数字和点了。
与其使用 win32_operatingsytem class 的名称 属性,不如使用 class 的标题或版本 属性。
在 win32_operatingsystem class 中查看这 3 个属性的区别:
name : Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk3\Partition1
caption : Microsoft Windows 10 Pro
version : 10.0.10586
解析标题或版本要容易得多。
老实说:我总是选择 'version' 因为它还包含内部版本号。
使用 [版本] 加速器将版本 属性 中返回的字符串转换为 [System.Version] 对象。 returns 一个包含主要、次要、构建和修订号的整数表示的对象:
[version](Get-WmiObject -Class Win32_OperatingSystem).Version
或仅获取主要编号:
([version](Get-WmiObject -Class Win32_OperatingSystem).Version).Major
我正在使用 (gwmi Win32_OperatingSystem).Name 来获取操作系统版本,以便配置脚本可以根据该版本选择适当的变量文件。但是,我想尽可能具体。我想将 OS 版本限制为 OS 数字(即 Windows 7 为 7,Windows 8 为 8,Windows Server 2012 为 2012, ETC)。这是我目前所拥有的:
(gwmi Win32_OperatingSystem).Name
产生:
Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition4
当我尝试将其限制为数字时:
(gwmi Win32_OperatingSystem).Name -replace '\D+(\d+)\D+',''
我得到:
100\Partition4
我只需要输出“10”。如何将我的正则表达式调整为仅 select 10(或 7,或 2012)?我考虑过以某种方式限制正则表达式在第一个遇到数字块后停止处理字符串(即点击一个数字然后在你点击 space 时停止)但我不知道如何做这样的事情.或者也许有更好的方法来获取我需要的信息?
提前致谢。
这输出“10”:
'Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition4' -replace '.*\s(\d+)\s.*',''
.*
: 任意字符 (.
) 0 次或多次 (*
)
\s
: 一个 space
\d+
: 一个数字 (\d
) 1 次或多次 (+
)
首先删除第一个 |
之后的所有内容,您不需要:
$OSName,$null = (Get-WmiObject Win32_OperatingSystem).Name.Split("|")
然后将正则表达式替换为结果字符串:
$OSVersion = $OSName -replace '\D+(\d+)\D*',''
执行此操作的最简单方法之一如下:
(gwmi Win32_OperatingSystem).Name -replace "(Harddisk.*)|(Partition.*)|([a-z])|(\)|(\|)|(:)"
其工作方式是首先替换 Harddisk
或 Partition
之后的所有数据,然后我们删除所有字母和特殊字符(\
或 |
) .
在那之后,我们只剩下数字和点了。
与其使用 win32_operatingsytem class 的名称 属性,不如使用 class 的标题或版本 属性。 在 win32_operatingsystem class 中查看这 3 个属性的区别:
name : Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk3\Partition1
caption : Microsoft Windows 10 Pro
version : 10.0.10586
解析标题或版本要容易得多。 老实说:我总是选择 'version' 因为它还包含内部版本号。
使用 [版本] 加速器将版本 属性 中返回的字符串转换为 [System.Version] 对象。 returns 一个包含主要、次要、构建和修订号的整数表示的对象:
[version](Get-WmiObject -Class Win32_OperatingSystem).Version
或仅获取主要编号:
([version](Get-WmiObject -Class Win32_OperatingSystem).Version).Major