我最近得到了获得Windows服务器的友好名称的答案,例如,Windows Server 2008 R2 Enterprise,我想删除“Windows Server”,这是正确执行此操作的代码:
$Build = (Get-WmiObject -Class Win32_OperatingSystem -Computername $Computer | Select-Object -ExpandProperty Caption) -replace "Microsoft Windows Server ",""
但是,我发现Windows 2003的命名约定略有不同:
Microsoft(R) Windows(R) Server 2003, Standard Edition.
所以我希望脚本删除“Microsoft(R)Windows(R)”(如果是这种情况)。我尝试添加-or -replace "Microsoft(R) Windows(R) Server ",""
,但这似乎不起作用。
答案 0 :(得分:2)
您可以使用RegEx
的OR运算符(|
)来匹配要替换的两个单词。
-replace [Regex]"Microsoft Windows Server |Microsoft\(R\) Windows\(R\) ",""
或者你可以简单地在第一个替换之后使用另一个-replace
。
-replace "Microsoft Windows Server ","" -replace "Microsoft\(R\) Windows\(R\) ",""
注意:您需要转义括号才能将其替换为例如\(
& \)