C# C WCF模拟不适用于从IIS上托管的webservice调用的powershell脚本(WIn 2012)

C# C WCF模拟不适用于从IIS上托管的webservice调用的powershell脚本(WIn 2012),c#,wcf,web-services,iis,C#,Wcf,Web Services,Iis,我们在IISOS win 2012上托管了WCF c Web服务。 想要调用powershell命令想要运行需要以管理员权限访问本地文件系统的DISM命令 到目前为止采取的步骤如下:- -尝试在wcf C中模拟管理员用户。 -如果我们在模拟后打印用户,模拟用户详细信息将按预期打印,在这种情况下,我们有域管理员。 -在模拟块中,如果执行任何进程、线程或powershell,则模拟将丢失 下面是模拟块的代码sinppet在下面的代码中,我们试图使用mkdir创建一个文件夹 由于powershell的

我们在IISOS win 2012上托管了WCF c Web服务。 想要调用powershell命令想要运行需要以管理员权限访问本地文件系统的DISM命令

到目前为止采取的步骤如下:- -尝试在wcf C中模拟管理员用户。 -如果我们在模拟后打印用户,模拟用户详细信息将按预期打印,在这种情况下,我们有域管理员。 -在模拟块中,如果执行任何进程、线程或powershell,则模拟将丢失

下面是模拟块的代码sinppet在下面的代码中,我们试图使用mkdir创建一个文件夹

由于powershell的模拟失败,我们尝试了一次又一次的模拟过程,在这两种情况下都失败了

过程:- //c模拟,正在按预期工作

Impersonate.ImpersonateUser("mydomain", "administrator", "mypasswd");
                {

           // Printed windows idenitty and impersonated user details are printed  
                      here.
             ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
                    processStartInfo.RedirectStandardInput = true;
                    processStartInfo.RedirectStandardOutput = true;
                    processStartInfo.UseShellExecute = false;
                    processStartInfo.UserName = "administrator";
                    processStartInfo.Domain = "mydomain";
                    processStartInfo.Password = secureString;
                    Process process = Process.Start(processStartInfo);
                    process.StandardInput.WriteLine(@"mkdir c:\test\test123"); // 
                     'mydomain\administrator' has access to test folder.
                    process.StandardInput.Close(); 
                    string output = process.StandardOutput.ReadToEnd();

         // folder is not getting and created(no output), not getting any 
                  exception.

                }
Powerhell:-

//After c# impersonation, tried the below c#.
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;



WindowsImpersonationContext ctx = null;
        try
        {
            ctx = winId.Impersonate();
            Runspace myRunSpace = RunspaceFactory.CreateRunspace();
            myRunSpace.Open();
    Command cmd1 = new Command("get-process", true);
            pipeline.Commands.Add(cmd1);

        Pipeline pipeline =                 
        myRunSpace.CreatePipeline(“WindowsIdentity]::GetCurrent().Name”);
        System.Collections.ObjectModel.Collection<PSObject> objectRetVal =  
         pipeline.Invoke();
        myRunSpace.Close();


        ctx.Undo();
    }

作为替代方法,您可以尝试满足您的请求:

C:\>psexec -s -u {user} -p {password} cmd /c mkdir c:\test\test123
psexec将以具有完全权限的用户身份远程运行,而不使用模拟

您需要下载put psexec并将其放在IIS可用的位置。

这可能是您的答案-