繁体   English   中英

Powershell GWMI win32_operatingsystem 修剪输出

[英]Powershell GWMI win32_operatingsystem trim output

当你使用

(GWMI -ComputerName $server -Class Win32_OperatingSystem -ErrorAction Stop).Caption

获得像这样的字幕

Microsoft(R) Windows(R) Server 2003 Standard x64 Edition
Microsoft Windows Server 2008 R2 Standard
Microsoft Windows Server 2012 Datacenter

从结果中删除"Microsoft Windows""Microsoft(R) Windows"的简单方法是什么?

我想出了:

(GWMI Win32_OperatingSystem -Comp $server).Caption -Replace "^Microsoft Windows "

这将"Microsoft Windows Server 2012 Datacenter"变成"Server 2012 Datacenter" ,但 2003 和 2008 上的旧机器与替换正则表达式不匹配。

这是我的

PS > gwmi Win32_OperatingSystem | % Caption
Microsoft Windows 7 Ultimate

和你想要的

PS > gwmi Win32_OperatingSystem | % Caption | % split ' ' 3 | select -last 1
7 Ultimate

我在我的环境中找到了所有操作系统作为测试。 我不会为这个答案担心 WMI,因为这不是问题的重点。

使用以下包含我所有测试示例的此处字符串

$OSes = @"
Microsoft Windows 7 Professional
Microsoft Windows 8.1 Pro
Microsoft Windows Server 2008 R2 Datacenter
Microsoft Windows Server 2008 R2 Enterprise
Microsoft Windows Server 2008 R2 Standard
Microsoft Windows Storage Server 2008 R2 Standard
Microsoft Windows XP Professional
Microsoft(R) Windows(R) Server 2003, Standard Edition
Microsoft® Windows Server® 2008 Standard
"@.Split("`r`n")

我运行一个正则表达式,用可选的 (R) 和 ® (<-- 版权符号) 找到 Microsoft Windows

$OSes -replace "Microsoft(\(R\)|®)?\sWindows(\(R\))?\s"

可以在此处找到有关正则表达式的更多详细信息

哪个网络输出

7 Professional
8.1 Pro
Server 2008 R2 Datacenter
Server 2008 R2 Enterprise
Server 2008 R2 Standard
Storage Server 2008 R2 Standard
XP Professional
Server 2003, Standard Edition
Server® 2008 Standard

我将继续使用正则表达式方法,并使用“Microsoft {optionally 其次是 (R)}”,如下例所示:

$s = @(   'Microsoft(R) Windows(R) Server 2003 Standard x64 Edition'
        , 'Microsoft Windows Server 2008 R2 Standard'
        , 'Microsoft Windows Server 2012 Datacenter'
    )

write "`n`n"

$s | % { $_ -replace "Microsoft(\(R\)|) Windows(\(R\)|) " }

暂无
暂无

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

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