I'm trying to execute a PowerShell script from a c# application. The script has to be executed under a special usercontext.
I've tried different scenarios some are working some not:
1. direct call from PowerShell
I've called the script directly from a ps-console which is running under the correct usercredentials.
C:ScriptsGroupNewGroup.ps1 1
Result: Successfully running the script.
2. from a c# console application
I've called the script from a c# consoleapplication which is started under the correct usercredentials.
Code:
string cmdArg = "C:\Scripts\GroupNewGroup.ps1 1"
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
runspace.Close();
if (error.Count >= 1)
{
string errors = "";
foreach (var Error in error)
{
errors = errors + " " + Error.ToString();
}
}
Result: No Success. And a lot of "Null-Array" exceptions.
3. from a c# console application - code side impersonating
(http://platinumdogs.me/2008/10/30/net-c-impersonation-with-network-credentials)
I've called the script from a c# consoleapplication which is started under the correct usercredentials and the code contains impersonation.
Code:
using (new Impersonator("Administrator2", "domain", "testPW"))
{
using (RunspaceInvoke invoker = new RunspaceInvoke())
{
invoker.Invoke("Set-ExecutionPolicy Unrestricted");
}
string cmdArg = "C:\Scripts\GroupNewGroup.ps1 1";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
runspace.Close();
if (error.Count >= 1)
{
string errors = "";
foreach (var Error in error)
{
errors = errors + " " + Error.ToString();
}
}
}
Results:
- The term 'Get-Contact' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
- The term 'C:ScriptsFunctionsObjects.ps1' is not recognized as the
name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the
path is correct and try again.
- No snap-ins have been registered for Windows PowerShell version 2.
Microsoft.Office.Server, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c
- System.DirectoryServices.AccountManagement, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089
- Exception calling ".ctor" with "1" argument(s): "The Web application
at
http://XXXX/websites/Test4/
could not be found. Verify that you
have typed the URL correctly. If the URL should be serving existing
content, the system administrator may need to add a new request URL
mapping to the intended application."
- You cannot call a method on a null-valued expression. Cannot index
into a null array.
Until now there is no working answer
Does anyone know why there are differences and how to solve the problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…