从C#运行时,Powershell脚本返回null [英] Powershell script returns null when run from C#

查看:118
本文介绍了从C#运行时,Powershell脚本返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我有一个有效的PowerShell脚本,可以从我们网络中的计算机上获取Windows产品密钥。该脚本在powershell命令行中运行良好,但在我尝试从C#运行时返回错误。我需要这个来制作我们公司的许可证。



Powershell代码:

Hello.

I have a working powershell script to get the Windows product key from an machine in our network. The script runs well in powershell comand line but returns error when I try to run it from C#. I need this working to chart licenses in our corporation.

Powershell code:

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







运行脚本的C#代码:




C# code that runs script:

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。我无法使其正常工作。


Please help if you can. I already know the error is that in C# GetBinaryValue returns null. I just can not make it work.

推荐答案

target = '
target = '


server'
server'


hklm = 2147483650
hklm = 2147483650


这篇关于从C#运行时,Powershell脚本返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆