无法在 WMI 类 "DateTime" 属性中插入数据



我正在声明属性" installdate ";在WMI类中如下:

$newClass.Properties.Add("InstallDate",[System.Management.CimType]::DateTime, $false)
$newClass.Properties["InstallDate"].Qualifiers.Add("Key", $true)

然后我使用下面的cmdlet从注册表中获取InstallDate:

$Win10Upgrade = Get-ChildItem -Path HKLM:SystemSetupSource* | ForEach-Object {Get-ItemProperty -Path Registry::$_} | Select @{n="InstallDate"; e={([DateTime]'1/1/1970').AddSeconds($_.InstallDate)}}

然后我试图将其插入WMI类CM_Win10UpgradeHistory中声明的"InstallDate"财产。

Set-WmiInstance -Class CM_Win10UpgradeHistory -Namespace ROOTCustomSCCMInventory -Arguments @{InstallDate=$Win10Upgrade.InstallDate} -ErrorAction SilentlyContinue | Out-Null

注册表中InstallDate的原始值为"1573146514"powershell中的DateTime操作将其更改为"11/7/2019 5:08:34 pm">

但我得到错误的"Set-WmiInstance: Type mismatch"。我也试着插入原始值,但它也给出了同样的错误。如果我将WMI类属性值分类为"字符串",我就可以插入它。但不作为"DateTime">

任何帮助都是非常感谢的。

经过一番研究,我找到了。用于在"日期时间"中插入值属性,则需要转换为

$OSInstallDate = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($Win10Upgrade.InstallDate)

然后$OSInstallDate可以被输入到DateTime属性。

最新更新