让 PowerShell 根据当前月份名称读取对象名称的语法

Syntax to let PowerShell read an object name based on current month name

我正在尝试让 PowerShell 从 cmdlet 中筛选多个对象并选择一个具有特定名称格式的对象。

名称格式始终如下所示: 当前月份名称 + 年份 + 工作站

例如,当前月份的格式如下所示: 2021 年 8 月工作站

我尝试了以下方法,但没有成功获得所需的输出。

$currentMonth = 
    (Get-UICulture).DateTimeFormat.GetMonthName((Get-Date).Month.ToString())
$LatestSUG = 
    Get-CMSoftwareUpdateGroup | 
        Select-Object LocalizedDisplayName  | 
            Where-Object -Property LocalizedDisplayName -EQ '$currentMonth'

cmdlet 是 Get-CMSoftwareUpdateGroup 我要过滤的对象名称是 LocalizedDisplayName

非常感谢您对此提供的任何帮助。

尝试删除 $currentMonth 周围的单引号,看看是否更适合您。

$currentMonth = (Get-UICulture).DateTimeFormat.GetMonthName((Get-Date).Month.ToString())
    
$LatestSUG = Get-CMSoftwareUpdateGroup | Select-Object LocalizedDisplayName | Where-Object -Property LocalizedDisplayName -match $currentMonth

为了平等,您应该检查 LocalizedDisplayName 是否与 $CurrentMonth 相匹配……可能像这样:

$currentMonth = 
    (Get-UICulture).DateTimeFormat.GetMonthName((Get-Date).Month.ToString())
$LatestSUG = 
    Get-CMSoftwareUpdateGroup | 
        Where-Object -Property LocalizedDisplayName -Match $currentMonth