我正在通过Powershell脚本获取林中的域列表,我想在dropdownlist中填充这些值。基于所选域,我想将AD组加载到另一个下拉列表中。我已经准备好脚本,但是无法找到填写下拉列表的方法。
我正在使用System.Automation.Management库来管理Powershell脚本。这是我的功能和脚本。
private string PowerShellExecutor(string script) {
string outString = "";
var shell = PowerShell.Create();
shell.Commands.AddScript(script);
var results = shell.Invoke();
if (results.Count>0)
{
var builder = new StringBuilder();
foreach (var psObj in results)
{
builder.Append(psObj.BaseObject.ToString() + "\r\n");
}
outString = Server.HtmlEncode(builder.ToString());
}
return outString;
}
<div class="col-md-12">
@Html.DropDownList("ddlDirectory", new SelectList(PowerShellExecutor(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Get\\ADDomain.ps1")), "Select Domain", new { @class = "form-control"})
</div>
我的脚本具有以下代码:Get-ADForest |选择-ExpandProperty域 返回域列表。
该下拉列表需要填写以下值: 域1 域2 ..
答案 0 :(得分:0)
有效的更新功能:
public List<SelectListItem> PowerShellExecutorLst(string scriptPath)
{
string outString = "";
var shell = PowerShell.Create();
shell.Commands.AddCommand(scriptPath);
var results = shell.Invoke();
if (results.Count > 0)
{
var builder = new StringBuilder();
foreach (var psObj in results)
{
builder.Append(psObj.BaseObject.ToString() + "\r\n");
}
outString = Server.HtmlEncode(builder.ToString());
}
List<string> result = outString.Split(new char[] { '\n' }).ToList();
List<SelectListItem> listItems = result.Select(s => new SelectListItem { Value = s, Text=s }).ToList();
shell.Dispose();
return listItems;
}