我正在使用C#来执行New-LocalUser cmdlet。如果用户已存在,则此cmdlet将在ErrorStream中存储异常:
using (PowerShell powerShellInstance = PowerShell.Create())
{
powerShellInstance.AddCommand("New-LocalUser");
powerShellInstance.AddParameter("Name", username);
powerShellInstance.AddParameter("Password", password);
PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
output.DataAdded += OutputAdded;
powerShellInstance.Invoke(null, output);
if (powerShellInstance.Streams.Error.Count > 0)
{
throw powerShellInstance.Streams.Error[0].Exception;
}
}
当我打印出上面的异常类型时,它是Microsoft.Powershell.Commands.UserExistsException。但我似乎无法找到包含此定义的DLL。有谁知道吗?
答案 0 :(得分:4)
首先,在PowerShell会话中运行New-LocalUser -?
以确保加载包含所关注类型的程序集。
然后运行以下命令:
[Microsoft.Powershell.Commands.UserExistsException].Assembly.Location
在我的Windows 10计算机上,这会产生:
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.LocalAccounts\1.0.0.0\Microsoft.Powershell.LocalAccounts.dll