Hi guys,
I've just written an ASP.NET app that run a PS script in order to create new AD user accounts. The web server (IIS 7.0) is running on a Win2008R2 server (not domain controller) joined to the domain.
The PS script is working fine locally on the server both via CLI and via web browser (through the ASP.NET app), but when I run it though a web browser on a remote client I get the following message: "Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.".
The problem rises when the script calls the "get-aduser" command.
The web app is using classic Windows authentication (AppPool) and, if I try to get the user who is running the script, I get my username as process owner, as expected.
Why do I get different behaviours if I use the web app locally on the server or remotely?
I hereby attach part of the ASP.NET code:
ASP.NET C# code:
protected void Submit_Button_Click(object sender, EventArgs e)
{
// Clean the Result TextBox
ResultBox.Text = string.Empty;
if (MacAddressList.Text != "")
{
// Create the string for the full filename
string myfile = Server.MapPath("~/data") + "\\" + GetUniqueKey(20) + ".csv";
myfile.Replace("\\\\", "\\");
// Write the DeviceType textbox value into a file
Write_CSV_File(MacAddressList.Text, myfile);
// Import Active Directory module
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "activedirectory" });
// Create a user's runspace
using (var psRunspace = RunspaceFactory.CreateRunspace(iss))
{
psRunspace.Open();
//Create a pipeline
using (Pipeline psPipeline = psRunspace.CreatePipeline())
{
// Create the command for the script to be launched
string myscript = @"E:\Scripts\webscript.ps1 -file '" + myfile + "' -description '" + Description.Text + "' -devicetype '" + DeviceType.SelectedValue + "' 2>&1";
// Add the PowerShell script to the pipeline object
psPipeline.Commands.AddScript(myscript);
// Execute the script
Collection<PSObject> results = psPipeline.Invoke();
// Display results, with BaseObject converted to string
if (results.Count > 0)
{
// We use a string builder to create our result text
var builder = new StringBuilder();
foreach (var psObject in results)
{
// Convert the Base Object to a string and append it to the string builder.
builder.Append(psObject.BaseObject.ToString() + "\r\n");
}
// Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
ResultBox.Text = Server.HtmlEncode(builder.ToString());
}
psPipeline.Commands.Clear();
}
psRunspace.Close();
}
// Delete the temp file
Delete_CSV_File(myfile);
}
}