標題可能有點混亂,因為如果有人知道更好的標題讓我知道,我不知道如何問這樣的問題。
所以我在這里做的是一個應用程式,它將為我提供所有啟用的用戶。問題是我不知道如何獲取這些資料并在我的代碼中進一步使用它。因為我想洗掉所有非活動的,但保留活動的和活動的,我將繼續我的專案。
所以,簡而言之,問題是我如何使用/獲取命令的結果并進一步處理我的應用程式?
static void Main(string[] args)
{
GetUserList();
}
public static string GetUserList()
{
SelectQuery query = new SelectQuery("Win32_UserAccount");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject User in searcher.Get())
{
Process cmd = new Process();
cmd.StartInfo.FileName = $@"powershell ";
cmd.StartInfo.Arguments = $@"Get-LocalUser -Name {(string)User["Name"]} | Where-Object Enabled";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.WaitForExit();
}
return null;
}
這是結果之一
如果有人知道更好的方法或知道如何回答我的問題,請告訴我。
謝謝。
編輯
感謝@mklement0。
(Get-LocalUser -Name {(string)User["Name"]}).Enabled
有了這個,我可以從每個用戶那里得到一個 True 或 False。
uj5u.com熱心網友回復:
通常,您最好使用基于PowerShell SDK的行程內解決方案。除了更快之外,它還允許您取回輸入的資料(而不是通過標準輸出的字串)。
使用您當前的方法(需要在每次迭代中創建一個 PowerShell子行程),使用以下內容獲取(字串化)布林值作為每個用戶的輸出,即"True"or "False",它指示該用戶是否已啟用。
cmd.StartInfo.Arguments = $@"(Get-LocalUser -Name {(string)User[""Name""]}).Enabled"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521963.html
