简体   繁体   English

C#应用程序调用Powershell脚本问题

[英]C# Application Calling Powershell Script Issues

I have a C# Winforms application which is calling a simple powershell script using the following method: 我有一个C#Winforms应用程序,它使用以下方法调用简单的powershell脚本:

Process process = new Process(); 
process.StartInfo.FileName = @"powershell.exe"; 
process.StartInfo.Arguments = String.Format("-noexit \"C:\\Develop\\{1}\"", scriptName); 
process.Start(); 

The powershell script simply reads a registry key and outputs the subkeys. Powershell脚本仅读取注册表项并输出子项。

$items = get-childitem -literalPath hklm:\software 

foreach($item in $items) 
{ 
Write-Host $item 
} 

The problem I have is that when I run the script from the C# application I get one set of results, but when I run the script standalone (from the powershell command line) I get a different set of results entirely. 我遇到的问题是,当我从C#应用程序运行脚本时,会得到一组结果,但是当我独立运行脚本(从powershell命令行)时,则会得到一组完全不同的结果。

The results from running from the c# app are: 从c#应用程序运行的结果是:

HKEY_LOCAL_MACHINE\software\Adobe 
HKEY_LOCAL_MACHINE\software\Business Objects 
HKEY_LOCAL_MACHINE\software\Helios 
HKEY_LOCAL_MACHINE\software\InstallShield 
HKEY_LOCAL_MACHINE\software\Macrovision 
HKEY_LOCAL_MACHINE\software\Microsoft 
HKEY_LOCAL_MACHINE\software\MozillaPlugins 
HKEY_LOCAL_MACHINE\software\ODBC 
HKEY_LOCAL_MACHINE\software\Classes 
HKEY_LOCAL_MACHINE\software\Clients 
HKEY_LOCAL_MACHINE\software\Policies 
HKEY_LOCAL_MACHINE\software\RegisteredApplications 
PS C:\Develop\RnD\SiriusPatcher\Sirius.Patcher.UI\bin\Debug> 

When run from the powershell command line I get: 从powershell命令行运行时,我得到:

PS M:\> C:\Develop\RegistryAccess.ps1 
HKEY_LOCAL_MACHINE\software\ATI Technologies 
HKEY_LOCAL_MACHINE\software\Classes 
HKEY_LOCAL_MACHINE\software\Clients 
HKEY_LOCAL_MACHINE\software\Equiniti 
HKEY_LOCAL_MACHINE\software\Microsoft 
HKEY_LOCAL_MACHINE\software\ODBC 
HKEY_LOCAL_MACHINE\software\Policies 
HKEY_LOCAL_MACHINE\software\RegisteredApplications 
HKEY_LOCAL_MACHINE\software\Wow6432Node 
PS M:\> 

The second set of results match what I have in the registry, but the first set of results (which came from the c# app) don't. 第二组结果与注册表中的内容匹配,但第一组结果(来自c#应用程序)不匹配。

Any help or pointers would be greatly apreciated :) 任何帮助或指针将不胜感激:)

Ben

This is actually not a particularly good way to embed PowerShell within a C# api. 实际上,这不是将PowerShell嵌入C#API中的特别好方法。 There are APIs for that. 有一些API。

You can find an example of them on MSDN , but in your case the could would look something like 您可以在MSDN上找到它们的示例,但是在您的情况下,它们可能看起来像

PowerShell.Create().AddScript("get-childitem -literalPath hklm:\\software").Invoke() PowerShell.Create()。AddScript(“ get-childitem -literalPath hklm:\\ software”)。Invoke()

You can also check out this blog post , which will show you how to dot source inside of the API and how to use this API to get at the other data streams in PowerShell. 您还可以查看此博客文章 ,该文章将向您展示如何在API内添加源代码以及如何使用此API来获取PowerShell中的其他数据流。

Hope this helps 希望这可以帮助

Are you running a 64bit version of Windows by chance? 您是否偶然运行Windows的64位版本? It could be a difference in how the two "hives" are being shown. 这两个“蜂巢”的显示方式可能有所不同。 Try forcing your C# app to compile to x86/x64 instead of "Any" in the Project properties. 尝试在Project属性中强制将C#应用编译为x86 / x64,而不是“ Any”。 See if that makes any difference. 看看那有什么区别。

Also, your command line syntax is a little strange, see the following thread for better details, but you may want to adjust your syntax: 另外,您的命令行语法有点奇怪,请参见以下线程以获取更多详细信息,但您可能需要调整语法:

String cmd = "-Command "& { . \"" + scriptName + "\" }";
Process process = new Process();
process.StartInfo.FileName = @"powershell.exe";
process.StartInfo.Arguments = cmd;
process.Start();

Calling a specific PowerShell function from the command line 从命令行调用特定的PowerShell函数

I did look at alternative methods for calling Powershell and came across that API. 我确实研究了调用Powershell的替代方法,并发现了该API。

Am I right in thinking though that they rely on a Microsoft SDK?? 我是否在考虑他们依赖Microsoft SDK的想法?

I'm not really a fan of dependencies on external SDKs. 我不是很喜欢依赖外部SDK的人。 I work in a rather large company and ensuring that the SDK is installed on all of the developers machines would be a nightmare. 我在一家相当大的公司工作,并且确保将SDK安装在所有开发人员计算机上将是一场噩梦。

If I'm wrong in my thinking, I am open to a better way of calling Powershell. 如果我的想法是错误的,则可以采用更好的方式调用Powershell。 I didn't particularly like calling the script as a separate process and would like the ability to have values returned from the script. 我不是特别喜欢将脚本作为一个单独的过程来调用,而是希望能够从脚本中返回值。

Ben

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM