1

我是 Microsoft 合作伙伴,负责 Surface Pro 3 和 4 的租借和播种流程。我们每天重新映像数百台设备,并且在数字授权方面遇到问题。我需要一种从设备中提取 OEM 密钥并使用该密钥强制激活的方法。我正在尝试通过 powershell 脚本来完成此操作:

$computer = gc env:computername

$key = (Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey | Out-String


$service = get-wmiObject -query “select * from SoftwareLicensingService” -computername $computer

$service.InstallProductKey($key)

$service.RefreshLicenseStatus()

我收到错误消息:

Exception calling "InstallProductKey" : ""
At line:7 char:1
+ $service.InstallProductKey((Get-WmiObject -query ‘select * from Softw ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

任何帮助将不胜感激,无论是修复此错误还是有更简单的方法来完成我正在做的事情。谢谢!

编辑:添加异常陷阱,新错误

Cannot convert the "System.Runtime.InteropServices.COMException (0xC004F025)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32      errorCode, IntPtr errorInfo)
   at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, 
InvokeMethodOptions options)
   at System.Management.Automation.ManagementObjectAdapter.InvokeManagementMethod(ManagementObject obj, String 
methodName, ManagementBaseObject inParams)" value of type    "System.Management.Automation.ErrorRecord" to type 
"System.Management.ManagementException".
At line:3 char:1
+ [System.Management.ManagementException] $_
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
4

1 回答 1

0

尝试添加.Trim()到 $key 的末尾

我在下面的代码中遇到了类似的问题,它抛出了相同的错误异常调用"InstallProductKey" : "" 结果$key是返回了密钥字符串 + 后面的几个空格。感谢@elexis把它捡起来。在任何地方都找不到解决方案。

$computer = gc env:computername
$key = (wmic path softwarelicensingservice get oa3xoriginalproductkey)[2].Trim() #<--The Trim is to remove the white space aftewards which causes an error
Write-Output $key

$service = get-wmiObject -query "select * from SoftwareLicensingService" -computername $computer
$service.InstallProductKey($key)
$service.RefreshLicenseStatus()
于 2018-09-20T02:11:47.447 回答