【问题标题】:How do I pass the json object to an instance of the PowerShell as a parameter to the script in C#?如何将 json 对象作为 C# 中脚本的参数传递给 PowerShell 实例?
【发布时间】: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


    【解决方案1】:

    第一个问题在这里:

    private string UpdateMemberList(JsonResult objMemberUpdate)
    

    JsonResult 类旨在用于响应(从控制器操作返回),而不是作为参数。

    相反,使用您需要的属性创建您自己的类,然后使用它。像这样的:

    public class MemberUpdate {
        public string Directory { get; set; }
        public Guid Group { get; set; }
        public List<string> Remove { get; set; }
        public List<string> RemoveAfter { get; set; }
        public string ResultFormat { get; set; }
        public string OnBehalfOf { get; set; }
    }
    

    然后您的操作定义将如下所示:

    public string UpdateMemberList(MemberUpdate objMemberUpdate)
    

    ASP.NET 会自动将浏览器发送的 JSON 反序列化为您的类的实例。

    也就是说,我想谈谈你所说的:

    使用 System.Management.Automation(不使用目录服务,因为目前是该库的新手并正在学习它)

    无论您如何实现这一点,您都必须学习一些东西。你不妨学习更好的方法:)

    从 C# 调用 PowerShell 很糟糕。我已经做了好几次了,它总是让我头疼。这意味着更多的先决条件(您需要安装 AD 模块)、更多需要破坏的东西,以及更慢的性能(因为您正在与单独的 powershell.exe 进程通信)。

    网上有很多关于如何在 C# 中添加和删除组成员的示例,例如 here - 问题本身的代码(使用 System.DirectoryServices)和已接受答案中的代码(使用 @ 987654330@) 将起作用。但是直接使用System.DirectoryServices,虽然有点复杂,但总是比System.DirectoryServices.AccountManagement 执行得更快。 (我在an article I wrote 中谈到了原因)。

    【讨论】:

    • Yes for Sure Gabriel 感谢您的回复,我发现的另一个问题是我将私有用于该函数,这就是它没有被调用的原因,当我将其公开时,我得到了解析错误遵循:System.Management.Automation.ParseException HResult=0x80131501 消息=系统错误。 Source= StackTrace:
    • 是的,这是真的。将您的操作设为私有将使其什么也不做 - 我没有注意到(但我以前也遇到过)。
    • 您现在遇到的异常是我建议您不要从 C# 调用 PowerShell 的原因 :)
    • 所以,我意识到发生这种情况的原因是因为我的 JSON 对象提供了空值,但 ajax 调用已经将值传递给了参数。 imgur.com/LDdbjTW 这就是我得到的。
    • 哦,我认为这是因为您的 JavaScript 中有这个:data: { objMemberUpdate: MemberUpdate }。将其更改为 data: MemberUpdate
    猜你喜欢
    • 2015-06-07
    • 2011-08-01
    • 2020-09-04
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2020-04-04
    相关资源
    最近更新 更多