【发布时间】:2012-07-13 02:21:48
【问题描述】:
我已经尝试从 asp.net 运行 PowerShell 脚本几天了,但没有成功。
C# 是:
using (var process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.Arguments = "arguments that call the script here";
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
}
它调用的 PowerShell 脚本包含 ff:
robocopy "\\network-server\location" "C:\localfolder" "testmovefile.txt"
显然,这里的问题是正确的凭据。但我尝试过各种模拟工作,无论是从 C# 还是在脚本级别。我尝试对 PowerShell 脚本执行此操作:
$username = "domain\user"
$password = ConvertTo-SecureString –String "password" –AsPlainText -Force
$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
start-process powershell -argument "C:\localfolder\CopyFile.ps1" -Credential $pp
当我在本地运行 PowerShell 控制台中的脚本时,它可以工作,即使使用对网络没有权限的帐户,但是当从 Web 应用程序调用时.. 没有任何反应。
不过,App Pool Identity 只是设置为默认的 App Pool Identity。我发现如果我将身份更改为具有适当权限的自定义帐户......它可以工作。
我仍在尝试寻找不同的解决方案。我想要一个场景,您只需更改脚本中的任何内容,它仍然会运行。只要不更改应用程序池标识,任何都可以。
我也试过这些:
- http://huddledmasses.org/using-alternate-credentials-with-the-filesystem-in-powershell/
- 在 c# 中使用运行空间而不是进程并使用模仿者How do you impersonate an Active Directory user in Powershell?
但它仍然不起作用。我继续被拒绝访问。问题是,是否可以通过在 PowerShell 中模拟某人来使其工作?
【问题讨论】:
-
“什么都没有发生。”:尝试使用调试器或日志记录(甚至进程监视器)来确定它有多远。也许
process.Start无法执行任何操作? -
如果我在本地运行它就可以了。如果我将 App Pool Identity 设置为自定义帐户,它也可以工作。
-
AppPool 的默认(原始:失败)身份是什么?
-
默认应用程序池标识。
标签: asp.net powershell iis-7 impersonation