Ben
Ben

Reputation: 21

C# Application Calling Powershell Script Issues

I have a C# Winforms application which is calling a simple powershell script using the following method:

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.

$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.

The results from running from the c# app are:

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:

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.

Any help or pointers would be greatly apreciated :)

Ben

Upvotes: 2

Views: 3688

Answers (3)

Ben
Ben

Reputation: 1

I did look at alternative methods for calling Powershell and came across that API.

Am I right in thinking though that they rely on a Microsoft SDK??

I'm not really a fan of dependencies on external SDKs. I work in a rather large company and ensuring that the SDK is installed on all of the developers machines would be a nightmare.

If I'm wrong in my thinking, I am open to a better way of calling 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

Upvotes: 0

Start-Automating
Start-Automating

Reputation: 8377

This is actually not a particularly good way to embed PowerShell within a C# api. There are APIs for that.

You can find an example of them on MSDN, but in your case the could would look something like

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.

Hope this helps

Upvotes: 3

Goyuix
Goyuix

Reputation: 24370

Are you running a 64bit version of Windows by chance? 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. 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

Upvotes: 1

Related Questions