You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell Core7.2.6正常的计算成员表达式在5.1中执行失败

代码在Windows PowerShell 5.1执行失败的原因及修复

失败原因

代码中Get-ADUser命令的Properties参数前使用了全角破折号(–),而非标准的半角减号(-)。Windows PowerShell 5.1的语法解析器无法识别这种非ASCII的全角符号,导致后续代码的语法解析出现混乱,进而抛出"字符串缺失终止符"和"语句块缺少闭合}"的错误。而PowerShell Core 7.x对这类字符的兼容性更强,因此能正常运行该代码。

修正后的代码

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$false, Position=0)]
    [string[]]$UserNames = @($Env:USERNAME)
)

Write-Verbose "Getting password expiration date for user $UserNames"

ForEach ($UserName in $UserNames) {
     Get-ADUser -identity $UserName -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" |
          Select-Object -Property `
               @{Name='UserName';Expression={$_.SamAccountName}},
               "Displayname",
               @{Name='Email';Expression={$_.UserPrincipalName}},
               @{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
}

内容的提问来源于stack exchange,提问作者lit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:40:16