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

PowerShell向AD账户扩展属性添加日期时遇类型错误求助

解决Set-ADUser设置扩展属性时的Invalid type错误

这个问题我之前帮人排查过,本质是类型不匹配导致的:Active Directory的extensionAttribute系列属性只接受字符串类型的值,但你直接调用Get-Date返回的是一个PowerShell的DateTime对象(归类为System.Management.Automation.PSObject),AD无法直接解析这种对象类型,因此抛出错误。

解决方案:将DateTime对象转换为字符串格式

只需要把Get-Date的输出转换成AD能识别的字符串格式即可,推荐两种常用方式:

1. 使用Get-Date的-Format参数指定格式

这是最直观的方式,你可以自定义日期时间的显示格式,比如ISO标准格式:

Set-ADUser -Identity tst_lawsonja -Add @{extensionAttribute15 = Get-Date -Format "yyyy-MM-dd HH:mm:ss"}

如果需要UTC时间格式(AD中很多场景偏好这种格式),可以用:

Set-ADUser -Identity tst_lawsonja -Add @{extensionAttribute15 = Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffZ"}

2. 使用ToString()方法转换格式

如果你更习惯对象方法调用,也可以直接对Get-Date的结果调用ToString():

Set-ADUser -Identity tst_lawsonja -Add @{extensionAttribute15 = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")}

注意事项

如果extensionAttribute15已经存在值,使用-Add参数会报错,此时需要换成-Replace参数来覆盖原有值:

Set-ADUser -Identity tst_lawsonja -Replace @{extensionAttribute15 = Get-Date -Format "yyyy-MM-dd HH:mm:ss"}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:12:35