Problem invoking Exchange Management Shell from C#...

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im currently using .NET 3.5 / C# to develop a Windows service that performs automated Exchange operations. This service basically watches a SQL database for operations to perform then spawns PowerShell and redirects the output so that results can be monitored
from a UI residing elsewhere. Below is the code Im using to invoke the process...
<pre class="prettyprint Action<object, DataReceivedEventArgs> DataReceived = (sender, data) =>
{
// Log data in SQL
};
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "powershell.exe"
p.StartInfo.Arguments = arguments;

// Arguments are (theyre coming from SQL, didnt feel like escaping everything just for this example)
// -command ". C:Program FilesMicrosoftExchange ServerV14binRemoteExchange.ps1; Connect-ExchangeServer -auto; Get-Mailbox –ResultSize unlimited | Search-Mailbox -SearchQuery ... stuff ...
p.StartInfo.LoadUserProfile = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler(DataReceived);
p.Start();[/code]
<br/>
This code can do things like run ping, tracert, nslookup, echo, dir, and all of the usual command-line suspects with behavior identical to as if I typed it into a command prompt. For instance, I could copy-paste the above into the Run box and it would work
flawlessly. Whenever I try to run it as above, however, I receive the following:
Get-ItemProperty : Cannot find path HKLM:SOFTWAREMicrosoftExchangeServerv14Setup because it does not exist. <br/>
At C:Program FilesMicrosoftExchange ServerV14binRemoteExchange.ps1:46 char:34 <br/>
+ $global:exbin = (get-itemproperty <<<< HKLM:SOFTWAREMicrosoftExchangeServerv14Setup).MsiInstallPath + "bin" <br/>
+ CategoryInfo : ObjectNotFound: (HKLM:SOFTWARE...erverv14Setup:String) [Get-ItemProperty], ItemNotFo
<br/>
undException <br/>
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand <br/>
<br/>
Get-ItemProperty : Cannot find path HKLM:SOFTWAREMicrosoftExchangeServerv14Setup because it does not exist. <br/>
At C:Program FilesMicrosoftExchange ServerV14binRemoteExchange.ps1:47 char:38 <br/>
+ $global:exinstall = (get-itemproperty <<<< HKLM:SOFTWAREMicrosoftExchangeServerv14Setup).MsiInstallPath <br/>
+ CategoryInfo : ObjectNotFound: (HKLM:SOFTWARE...erverv14Setup:String) [Get-ItemProperty], ItemNotFo
<br/>
undException <br/>
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand <br/>
<br/>
Get-ItemProperty : Cannot find path HKLM:SOFTWAREMicrosoftExchangeServerv14Setup because it does not exist. <br/>
At C:Program FilesMicrosoftExchange ServerV14binRemoteExchange.ps1:48 char:38 <br/>
+ $global:exscripts = (get-itemproperty <<<< HKLM:SOFTWAREMicrosoftExchangeServerv14Setup).MsiInstallPath + "scri <br/>
pts" <br/>
+ CategoryInfo : ObjectNotFound: (HKLM:SOFTWARE...erverv14Setup:String) [Get-ItemProperty], ItemNotFo
<br/>
undException <br/>
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand <br/>
<br/>
The term binCommonConnectFunctions.ps1 is not recognized as the name of a cmdlet, function, script file, or operable <br/>
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. <br/>
At C:Program FilesMicrosoftExchange ServerV14binRemoteExchange.ps1:52 char:2 <br/>
+ . <<<< $global:exbin"CommonConnectFunctions.ps1" <br/>
+ CategoryInfo : ObjectNotFound: (binCommonConnectFunctions.ps1:String) [], CommandNotFoundException <br/>
+ FullyQualifiedErrorId : CommandNotFoundException

A whole slew of other errors follows after this, but from going over the RemoteExchange PowerShell script Ive determined that it all comes down to those first three errors: not being able to read from the registry. Does anyone have any idea as to why this
might be happening?
Things Ive tried to get this to work:

Running this code in a console app as opposed to a service context.Every time Ive run it Ive done so as a domain and Exchange admin and I never got an UAC prompts, so I doubt the issue is one of credentialsChecked registry keys... The HKLM key its looking at also has full read permissions granted to everbodyIve enabled unsigned PowerShell script execution on the serverPutting the command into a PowerShell script and invoking that programmaticallyHardcoding the registry keys values into the PowerShell script (which just gives me another set of registry read errors further down the line)Using ShellExecute on the process (this cant be done with output redirection, which I require)Explicitly setting environment variables on the StartInfo to match the ones in the spawning environment
To anyone that can give me a hand... thanks a billion !

View the full article
 
Back
Top