[英]Script running in Powershell but error when run in C#
我有一個有效的Powershell腳本,可以從我們網絡中的計算機上獲取Windows產品密鑰。 該腳本在powershell comand行中運行良好,但是當我嘗試從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。 我只是無法使其工作。
我不確定您是如何通過以下方式以腳本形式工作的:
$target = '$server'
用單引號引起來的字符串不會在其中擴展變量。 這應該可以正常工作:
$target = "$server"
通常$target = $server
就足夠$target = $server
但是由於您正在執行字符串替換,因此最好堅持使用雙引號。
另一種可能性是您正在將C#代碼作為x86運行,並且正在讀取WOW64注冊表而不是64位注冊表。 這也許可以解釋為什么您在運行腳本和通過C#應用程序運行之間看到不同的結果。
更新:理論請參見底部。
從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。 該問題可能會有所幫助: 從32位應用程序讀取64位注冊表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.