【问题标题】:If Not Null Replace with regex, if is Null, do nothingIf Not Null 用正则替换,如果是 Null,什么也不做
【发布时间】:2017-04-21 06:51:42
【问题描述】:

我只希望在 $_.Skuname 不是 Null 且不是空白的情况下应用正则表达式,这样它就不会将 Null 转换为字符串。这可能吗,如果可以,怎么做?

(Get-SQLInstance -Computername $Computer | foreach { $_.Skuname }) -replace '(\w+) .+', '$1'

【问题讨论】:

    标签: regex string powershell replace null


    【解决方案1】:

    只需在你的 foreach 中包含 if/then 语句,如下所示:

    (Get-SQLInstance -Computername $Computer | foreach { if ($_.Skuname) { $_.Skuname -replace '(\w+) .+', '$1'} }

    希望这会有所帮助!

    【讨论】:

    • 这是我的荣幸!
    猜你喜欢
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2022-11-21
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多