【发布时间】:2019-11-13 10:21:33
【问题描述】:
我正在构建一个使用 System.Management.Automation 与 Active Directory 交互的应用程序(不使用目录服务,因为目前是该库的新手并正在学习它)。为了更新活动目录中某个组的组成员资格,我在视图上创建了一个 JSON 对象,并调用一个函数通过我的控制器中的函数将对象和 URI 从前端传递到后端。
基本思想是允许通过将 JSON 对象作为参数传递给 shell 脚本来批量删除 AD 组成员,该脚本将在函数中创建的 PowerShell 实例中执行。我正在使用 .ajax 调用来调用控制器函数,并将生成的 JSON 对象作为参数与当前 URI 一起传递。 shell.commands.AddParameter() 函数只接受字符串格式的参数。因此,我使用 ToString() 对其进行了类型转换,并在 PowerShell 脚本中将其转换为 JSON。我从后面的代码中传递 URL,因为 URL 可能会发生变化。我没有收到任何错误但是,我也看不到 AD 成员资格的任何更新。 Json 对象是从 HTML 表中生成的。
我的 shell 脚本
param($objMemberUpdate, $uri)
$body = $objMemberUpdate | ConvertTo-JSON
Invoke-WebRequest -Uri $uri -Method Post -Body $objMemberUpdate
我的控制器函数调用 PowerShell 实例并从指定位置执行 Shell 脚本文件。
private string UpdateMemberList(JsonResult objMemberUpdate)
{
var uri = HttpContext.Request.Url.AbsoluteUri;
var shell = PowerShell.Create();
shell.Commands.AddCommand(AppDomain.CurrentDomain.BaseDirectory + "Shell\\Set-ADGroupMembership.ps1").AddParameter(objMemberUpdate.ToString(), uri);
var results = shell.Invoke();
shell.Dispose();
return results.ToString();
}
我在 HTML 页面上单击按钮时调用的 Ajax 调用。
//Make Array Object to pass in the API For Membership Update
$("#btnUpdate").click(function () {
var RemoveMembers = [];
var RemoveAfter = [];
var MemberUpdate = {};
var GroupGUID = "";
$("table [id*=ddlReqdAdjustment]").each(function () {
if ($(this).val() != "Keep") {
GroupGUID = $(this).parent().parent().children().eq(4)[0].innerText;
var date = $(this).parent().parent().children().eq(8)[0].firstElementChild.value;
var ObjectGUID = $(this).parent().parent().children().eq(3)[0].innerText + "@@" + $('#ddlDirectory').val();
if ($(this).val() == "Remove") {
var format = ObjectGUID;
RemoveMembers.push(format);
} else {
var format = date + "|" + ObjectGUID;
RemoveAfter.push(format);
}
}
});
MemberUpdate = {
"Directory": $('#ddlDirectory').val(),
"Group": GroupGUID,
"Remove": RemoveMembers,
"RemoveAfter": RemoveAfter,
"ResultFormat": "json",
"OnBehalfOf": "11112201"
};
console.log(MemberUpdate);
$.ajax({
type: "POST",
url: "/Group/UpdateMemberList",
data: { objMemberUpdate: MemberUpdate },
success: function (response) {
alert(response.message);
}
});
表中选定的成员应该从 AD 中提到其 GroupGUID(AD 中的 ObjectGUID 属性)的组中删除。但是,没有遇到编译时间或运行时错误,甚至没有反映任何更改,我认为这一定是由于我的 JSON 对象有问题?
【问题讨论】:
标签: c# asp.net-mvc powershell active-directory