C# Application Calling Powershell Script Issues

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2010-03-17T13:49:44Z Indexed on 2010/03/17 13:51 UTC
Read the original article Hit count: 620

Filed under:
|
|

Hi,

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about powershell