我有一个有效的PowerShell脚本,可以从我们网络中的计算机上获取Windows产品密钥。该脚本在powershell命令行中运行良好,但在我尝试从C#运行时返回错误。我需要这样做才能在我们公司中制作许可证。
Powershell代码:
function Get-WindowsKey {
$target = '$server'
$hklm = 2147483650
$regPath = "Software\Microsoft\Windows NT\CurrentVersion\DefaultProductKey"
$regValue = "DigitalProductId4"
$productKey = $null
$win32os = $null
$wmi = [WMIClass]"\\$target\root\default:stdRegProv"
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$binArray = ($data.uValue)[52..66]
$charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
For ($i = 24; $i -ge 0; $i--) {
$k = 0
For ($j = 14; $j -ge 0; $j--) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) {
$productKey = "-" + $productKey
}
}
$productkey
}
Get-WindowsKey | Format-Table -AutoSize
只需用$替换$ server。或你的IP
运行脚本的C#代码:
private void RunWindowsKeyScript(string IP)
{
try
{
Assembly keyAssembly = this.GetType().Assembly;
ResourceManager keyResMan = new ResourceManager("LanMap.Resources.PS script", keyAssembly);
string keyScript = keyResMan.GetString("WindowsKey");
keyScript = keyScript.Replace("$server", IP);
Runspace keyRunspace = RunspaceFactory.CreateRunspace();
keyRunspace.Open();
Pipeline keyPipeline = keyRunspace.CreatePipeline();
keyPipeline.Commands.AddScript(keyScript);
keyPipeline.Commands.Add("Out-String");
Collection<psobject> keyResults = keyPipeline.Invoke();
keyRunspace.Close();
StringBuilder keyStringBuilder = new StringBuilder();
foreach (PSObject obj in keyResults)
{
keyStringBuilder.AppendLine(obj.ToString());
}
}
catch (Exception ex)
{
string s = "";
s += " ";
}
}
如果可以,请帮忙。我已经知道错误是在C#GetBinaryValue中返回null。我无法让它发挥作用。
答案 0 :(得分:0)
我不确定你是如何以脚本形式使用它的:
$target = '$server'
单引号字符串不会扩展其中的变量。这应该可以正常工作:
$target = "$server"
这通常就足够了$target = $server
但是因为你正在进行字符串替换,所以最好坚持使用双引号。
另一种可能性是您的C#代码正在以x86身份运行,并且您正在读取WOW64注册表而不是64位注册表。这可以解释为什么在运行脚本和通过C#应用程序运行之间会看到不同的结果。
答案 1 :(得分:0)
更新:请参阅底部了解理论。
从ISE测试PowerShell代码:看起来GetBinaryValue
失败了:
$target = 'localhost'
$hklm = 2147483650
$regPath = "Software\Microsoft\Windows NT\CurrentVersion\DefaultProductKey"
$regValue = "DigitalProductId4"
$productKey = $null
$win32os = $null
$wmi = [WMIClass]"\\$target\root\default:stdRegProv"
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
if ($data -eq $null) { Write-DEbug "Data is NULL"; return "STOP 2" }
write-debug '$Data'
write-debug ("$($data): " + ($data | gm | out-string))
write-debug ("$($data): " + ($data | fl * | out-string))
if ($data.uValue -eq $null) { Write-Debug "Data.uValue is NULL"; return "STOP 3" }
# rest cut for testing
结果:
DEBUG: $Data DEBUG: System.Management.ManagementBaseObject: TypeName: System.Management.ManagementBaseObject#\__PARAMETERS Name MemberType Definition ---- ---------- ---------- PSComputerName AliasProperty PSComputerName = __SERVER ReturnValue Property uint32 ReturnValue {get;set;} uValue Property byte[] uValue {get;set;} __CLASS Property string __CLASS {get;set;} __DERIVATION Property string[] __DERIVATION {get;set;} __DYNASTY Property string __DYNASTY {get;set;} __GENUS Property int __GENUS {get;set;} __NAMESPACE Property string __NAMESPACE {get;set;} __PATH Property string __PATH {get;set;} __PROPERTY_COUNT Property int __PROPERTY_COUNT {get;set;} __RELPATH Property string __RELPATH {get;set;} __SERVER Property string __SERVER {get;set;} __SUPERCLASS Property string __SUPERCLASS {get;set;} DEBUG: System.Management.ManagementBaseObject: PSComputerName : __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 2 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 2 uValue : Properties : {ReturnValue, uValue} SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY...} Qualifiers : {} ClassPath : __PARAMETERS Site : Container : DEBUG: Data.uValue is NULL STOP 3
ReturnValue: 2
对应于ERROR_FILE_NOT_FOUND(假设ReturnValue
成员反映GetBinaryValue
的返回值,其中0表示成功,否则返回标准Windows API错误。)
根据ISE实例,即使得到这个:
PS [32] C:\ #50> gp "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey" gp : Cannot find path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey' because it does not exist. At line:1 char:1 + gp "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (HKLM:\SOFTWARE\...faultProductKey:String) [Get-ItemProperty], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
但是我可以看到密钥......但是ISE的64位实例却有效。
该密钥仅可用于(直接)64位进程,最近Visual Studio版本的默认设置是大多数应用程序构建为32位(大多数应用程序不需要额外的地址空间),32位.NET应用程序将加载32位PowerShell运行时程序集。
您是否尝试构建64位应用程序?
PS。这个问题可能有所帮助:Reading 64bit Registry from a 32bit application