Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用某些字符串时出现关闭的TextWriter异常_C#_.net_Powershell - Fatal编程技术网

C# 使用某些字符串时出现关闭的TextWriter异常

C# 使用某些字符串时出现关闭的TextWriter异常,c#,.net,powershell,C#,.net,Powershell,我正在尝试实现一个功能,在网页上显示一些ActiveDirectory信息,但是当我出于某种原因试图显示某些信息(用户的管理器)时,它开始向我抛出下面的错误,而以前它没有这样做。有人能解释一下这里发生了什么或提出解决办法吗 在PowerShell的using()块期间,我没有在处理后向StringBuilder写入任何内容,因此我不知道为什么在manager\u name字符串调用GetManager函数时会突然抛出异常,根据VisualStudio的调试工具,我知道它会返回正确的值,在转入第7

我正在尝试实现一个功能,在网页上显示一些ActiveDirectory信息,但是当我出于某种原因试图显示某些信息(用户的管理器)时,它开始向我抛出下面的错误,而以前它没有这样做。有人能解释一下这里发生了什么或提出解决办法吗

在PowerShell的using()块期间,我没有在处理后向StringBuilder写入任何内容,因此我不知道为什么在manager\u name字符串调用GetManager函数时会突然抛出异常,根据VisualStudio的调试工具,我知道它会返回正确的值,在转入第752行时显示正确的值

PrintADInfo_displayresults.InnerHtml=html_结果

在处理所有对象并返回函数外部之前。奇怪的是,当将manager_name设置为空字符串或仅“test”时,代码将正常运行,但使用GetUsersManager函数将引发异常

在下面,您将找到有问题的方法和错误文本

错误文本

System.ObjectDisposedException:无法写入已关闭的TextWriter。 在System.IO.\u错误。WriterClosed()在 System.IO.StreamWriter.Flush(布尔flushStream,布尔 flushEncoder)在System.IO.StreamWriter.Flush()处 System.Management.Automation.Host.TranscriptionOption.Dispose()位于 System.Management.Automation.Host.PSHostUserInterface.StopAllTranscribing() 在 System.Management.Automation.Runspaces.LocalRunspace.DoCloseHelper() 在 System.Management.Automation.Runspaces.LocalRunspace.CloseHelper(布尔值 同步呼叫)在 System.Management.Automation.Runspaces.RunspaceBase.CoreClose(布尔值) 同步呼叫)在 System.Management.Automation.Runspaces.LocalRunspace.Close()位于 System.Management.Automation.Runspaces.LocalRunspace.Dispose(布尔值 处理)位于System.Management.Automation.PowerShell.Dispose(布尔值 处置)位于System.Management.Automation.PowerShell.Dispose()处 _h:\Visual Studio 2015\WebSite2\Default.aspx.cs中的Default.PrintADInfo(字符串用户名):第753行

private void PrintADInfo(字符串用户名)
{
//打印给定用户名的广告信息
AD_PrintADInfoDiv.Attributes[“style”]=“display:block”//移动到帐户显示
尝试
{
使用(PowerShell ps=PowerShell.Create())
{
var sb=新的StringBuilder();
sb.Append(“抓取“+username.ToUpper()+”的广告信息”);
//运行命令Get ADUser从AD获取用户信息
//命令信息->https://technet.microsoft.com/en-us/library/hh852208(v=wps.630).aspx
//使用-Properties获取我们想要的特定属性,然后使用Select忽略一些其他属性
ps.AddScript(“Get ADUser-Properties SamAccountName,CN,EmailAddress,Title,Department,Manager,OfficePhone,Mobile,PasswordLastSet,EmployeeID,co,physicalDeliveryOfficeName”+用户名+“|选择SamAccountName、CN、Title、Department、EmailAddress、Manager、OfficePhone、Mobile、PasswordLastSet、EmployeeID、co、physicalDeliveryOfficeName |转换为Html”);
集合psOutput=ps.Invoke();
//将用户信息添加到StringBuilder以进行输出
foreach(psOutput中的PSObject p)
{
sb.追加(p.ToString());
}
//邪恶的正则表达式黑客
//引号加倍以解释C#文字
//https://stackoverflow.com/questions/9289357/javascript-regular-expression-for-dn
(3)将,,,::[A-Za-z-A-A-A-A-A-A-A-A-A-f-A-A-A-A-Fa-f[[2})的,,,,((::::::[A-A-A-A-A-A-A-A-A-A-z-A-A-A-A-A-A-A-A-A-A-A-A-A-f[A-A-A-A-A-A-f[[[[[[[[2})A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-A-f[[[[[[[[[[[[[[2]]]]]]]]]]]的[[[2}}}}}}}}}})的[[[2]]]]z][\w-]*.\d+(?:\.\d+*)=(?:)(?:[\dA-Fa-f]{2})+124;(?:[^,=\+\\\\\\\\\\\\\\][,=\+\\\\\\\\.\\\\[\ dA-Fa-f]{2})*\\(?:[?:[^\\\\\\\\\\\\\\\\\\\\\\\\\\\+\\\\\\\\\\\\\\\\\\\\\\\\\+\\\\\\\\\\\\\\\\\\\\\[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\(3)鉴于目前的[w-.\w-[[[3\w-]鉴于鉴于鉴于目前的[[[3\dA-Fa-FAF-f]{{2},[[[3\w-.\w-.\3\w-.\3\维维鉴于鉴于鉴于目前的[[[[3\w-.\4-.\3\3-....\3\3-.......\\\3-..[[目前目前目前,,,,[[[[[[[[[[3\3\3-\3-\维维维维维维维维维维维维维维维维维维维维维维维维维抢抢抢抢抢抢手手手手手手手手手手手手手手手手手手手手手手手手手手手手手手手手手手维维维维维维维维维维维维维维维维维维维维维维维维维维维维维维维维维?:\.\d+*)=(?:\\.-(?:[\dA-Fa-f]{2})+124;(?:[^,=\+\\\\\\\\\\\][,=\+\\\\\\\\\\\\\\][\ dA-Fa-f]{2})*124;“(?:[\\\\\\\\\\\\\\\\][,=\+\\\\\\\\\\\\\\\\\[\dA-Fa f]{2*]);
正则表达式rgx=新正则表达式(模式);
//将通常为CN格式的用户管理器字段替换为NTID
字符串管理器\u name=GetUsersManager(用户名);

//string manager_name=“”;//我认为您需要使用(PowerShell ps=PowerShell.Create()){}将对
string manager_name=GetUsersManager(username);
的调用移到
之外
块。您在using中有一个using,我认为您的对象被过早地处理了。

如果您从
字符串管理器\u name=GetUsersManager(username);
开始,在
using(Powershell…)之外,是否有效
block,通过提供一个示例用户名?例如,将
username
设置为一个有效的用户名,然后看看是否可以从那里调用它?我认为您的问题可能是,您实际上在另一个块中有一个using Powershell块。这似乎已经完成了,谢谢!将GetUsersManager()调用移动到try()中但在using()块似乎解决了这个问题之前。
private void PrintADInfo(string username)
{
    //Print AD information for given username
    AD_PrintADInfoDiv.Attributes["style"] = "display:block"; //Move to the account display
    try
    {
        using (PowerShell ps = PowerShell.Create())
        {
            var sb = new StringBuilder();
            sb.Append("Grabbing AD info for " + username.ToUpper() + " ...");

            //Run command Get-ADUser to get user information from AD
            //Command Information -> https://technet.microsoft.com/en-us/library/hh852208(v=wps.630).aspx
            // Use -Properties to get specific proprties we want, then use Select to ignore some additional ones
            ps.AddScript("Get-ADUser -Properties SamAccountName, CN, EmailAddress, Title, Department, Manager, OfficePhone, Mobile, PasswordLastSet, EmployeeID, co, physicalDeliveryOfficeName " + username + " | select SamAccountName, CN, Title, Department, EmailAddress, Manager, OfficePhone, Mobile, PasswordLastSet, EmployeeID, co, physicalDeliveryOfficeName | ConvertTo-Html");
            Collection<PSObject> psOutput = ps.Invoke();

            //Add user informtion to StringBuilder for output
            foreach (PSObject p in psOutput)
            {
                sb.Append(p.ToString());
            }

            //Evil regex hacking
            //Quotes are doubled to account for C# literals
            //https://stackoverflow.com/questions/9289357/javascript-regular-expression-for-dn
            string pattern = @"(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*|""(?:[^\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*"")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*|""(?:[^\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*""))*(?:,(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*|""(?:[^\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*"")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*|""(?:[^\\""]|\\[,=\+<>#;\\""]|\\[\dA-Fa-f]{2})*""))*)*";
            Regex rgx = new Regex(pattern);

            //Replace the user's manager field that's normally in CN format to NTID
            string manager_name = GetUsersManager(username);
            //string manager_name = ""; // <-- Making the manager blank runs fine
            string html_results = rgx.Replace(sb.ToString(), manager_name);


            PrintADInfo_displayresults.InnerHtml = html_results;

            return; // <--- Error gets thrown here
        }
    }
    catch (Exception e)
    {
        PrintADInfo_displayresults.InnerHtml = "Exception caught:" + e;
        return;
    }
}

private string GetUsersManager(string username)
{
    //Returns the NTID of the user's manager, instead of the CN
    string manager_name = "";
    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddScript("(Get-ADUser (Get-ADUser " + username + " -Properties manager).manager).samaccountName");
        Collection<PSObject> psOutput = ps.Invoke();
        foreach (PSObject p in psOutput)
        {
            manager_name = manager_name + p.ToString();
        }
    }
    return manager_name;
}