[英]How do I pass the json object to an instance of the PowerShell as a parameter to the script in C#?
我正在構建一個使用System.Management.Automation與Active Directory進行交互的應用程序(不使用Directory Services,因為該庫目前是新知識並正在學習)。 為了更新活動目錄中某個組的組成員身份,我在視圖上創建一個JSON對象,並調用一個函數以通過控制器中的函數從前端到后端傳遞對象和URI。
基本思想是通過將JSON對象作為參數傳遞給外殼腳本來允許批量刪除AD組成員,該腳本將在該函數中創建的PowerShell實例中執行。 我正在使用.ajax調用來調用控制器函數,並將作為參數生成的JSON對象與當前URI一起傳遞。 shell.commands.AddParameter()函數僅接受字符串格式的參數。 因此,我使用ToString()進行了類型轉換,並在PowerShell腳本中將其轉換為JSON。 我正在從后面的代碼傳遞URL,因為URL可能會更改。 我沒有任何錯誤但是,我也看不到AD中的成員資格有任何更新。 Json對象是從HTML Table生成的。
我的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();
}
我在按鈕上調用的Ajax調用在HTML頁面上單擊。
//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 Object的問題引起的嗎?
第一個問題在這里:
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#中的組中添加和刪除成員的示例,例如此處 -問題本身的代碼(使用System.DirectoryServices
)和接受的答案中的代碼(使用System.DirectoryServices.AccountManagement
)將工作。 但是,雖然直接使用System.DirectoryServices
稍微復雜一些,但其執行速度總是比System.DirectoryServices.AccountManagement
快。 (我在寫的文章中談到了原因)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.