c# - 为什么我收藏的字符串会截断单词?

标签 c# powershell collections stringbuilder ellipsis

我正在专门为 Office 365 编写 PowerShell 应用程序,但遇到了一个问题。

var result = pipeline.Invoke();
// close the runspace
runspace.Close();

// convert the script result into a single string

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<----------------Results---------------->");
foreach (var item in result)
{
    stringBuilder.AppendLine(item.ToString());
}

现在,一切正常,直到我得到结果。问题是如果我得到这样的回复:

UserPrincipalName          DisplayName                isLicensed                
-----------------          -----------                ----------                
johnsonadmin@johnsoncom... Jack M*****                False  

如果我在 Powershell 中运行相同的命令,我会得到相同的结果,但格式会更有条理。它不会说“johnsonadmin@johnsoncom...”,而是会给我完整的电子邮件地址。

我认为这可能是集合如何自动格式化我的字符串的问题,但我不确定。当我试图解析电子邮件地址的文本时,这就成了一个大问题;)

如有任何帮助,我们将不胜感激!

谢谢!

最佳答案

如果您尝试将管道的结果解析为字符串,您将完全失去 powershell 的存在理由:您不必解析字符串。那是 1970 年 1 月 1 日。这些天,当您坐在现代 Windows 机器前时,awk、sed 和 grep 就在佛罗里达州的一个养老院里。如果您觉得自己想要使用 Cygwin,那您就错了。

我正在以您的脚本为起点来做这件事,但您会明白这一点(我希望):

//...
// result is a Collection<PSObject>
foreach (PSObject item in result) 
{ 
     // properties are not case-sensitive
     string userPrincipalName = item.Properties["userprincipalname"].Value as string;
     string displayName = item.Properties["displayname"].Value as string;
     bool isLicensed = item.Properties["islicensed"].Value as bool;
     // ...
} 

盖迪特?顺便说一句,您看到输出中的所有内容都被截断的原因是您正在捕获针对窄控制台窗口优化的显示友好格式化输出。

关于c# - 为什么我收藏的字符串会截断单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8662754/

相关文章:

java - 哪个 Java 集合允许最快的元素删除?

c# - 使用 C# 反序列化 XML HTTP 响应

c# - JArray - 按键名搜索对象

powershell - 新-AzureRmADAppCredential : Exception of type 'System.Exception' was thrown

json - 如何在 Powershell 中检查文件是否具有有效的 JSON 语法

java - 如何获得hashmap中的两个最大值

c# - IdentityServer4多个WSFederation-providers导致异常

c# - XmlWriter 在 C# 中使用 StringWriter 编码 UTF-8

azure - 从规模集中的 VM 检索应用程序运行状况

java - 如何创建一个固定大小的 IntArray 并稍后在 Kotlin 中初始化该数组?