Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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# 为什么我收藏的字符串会截断单词?_C#_Powershell_Collections_Stringbuilder_Ellipsis - Fatal编程技术网

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

C# 为什么我收藏的字符串会截断单词?,c#,powershell,collections,stringbuilder,ellipsis,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("<----

我正在专门为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());
}
如果我在Powershell中运行相同的命令,我将获得相同的结果,但格式会更加有序。而不是说“johnsonadmin@johnsoncom...“,它实际上会给我整个电子邮件地址

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

任何帮助都将不胜感激


谢谢

而不是
item.ToString()
,访问实际属性。

如果您试图将管道结果解析为字符串,那么您完全没有powershell的理由:您不必解析字符串。那是1970年1月1日。如今,当你坐在一台现代化的Windows电脑前时,Awk、sed和grep正在佛罗里达州的一家养老院。如果你觉得自己在接近Cygwin,那你就错了

我是以你的剧本为出发点,从头开始做这件事的,但你会明白我的意思(我希望):

/。。。
//结果是一个集合
foreach(结果中的PSObject项)
{ 
//属性不区分大小写
字符串userPrincipalName=item.Properties[“userPrincipalName”]。值为字符串;
字符串displayName=item.Properties[“displayName”]。值为字符串;
bool isLicensed=item.Properties[“isLicensed”]。值为bool;
// ...
} 

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

我非常感谢您对整个物业项目的帮助——我不知道我能做到这一点

然而,我最近的问题是对象引用问题。我最终通过使用解决了这个问题


结合使用不同类型的Powershell,我可以调用两次,并通过ISS连接到MSOnline服务,我可以在不解析文本的情况下获得我的用户列表:)

有帮助吗?涉及的对象类型是什么?PowerShell在表示方面做了一些groovy的事情,但您只需要调用ToString(),然后返回到该特定对象的C#ToString实现。这可能就是问题所在。(另请看另一条评论中的链接)。我真的刚刚开始用C#做Powershell的东西,所以我不知道我可以从PSObject中获取属性信息。太酷了!但是,我现在有一个对象引用没有设置为对象的实例。。。我已经通过谷歌确认userprincipalname是正确的属性。除了继续研究(我会做)之外,我不知道从这里到哪里去。顺便说一句,谢谢你的帮助!我已经断断续续地离开编程领域有一段时间了,我的知识相当有限。以下是我所掌握的导致对象引用错误的信息:
string userprincipalname=item.Properties[“userprincipalname”].Value as string()我不知道这是否重要,但我将整个内容作为“AddScript”而不是“AddCommand”。无论如何,如果我执行item.ToString(),我将得到从原始问题得到的结果。
UserPrincipalName          DisplayName                isLicensed                
-----------------          -----------                ----------                
johnsonadmin@johnsoncom... Jack M*****                False  
//...
// 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;
     // ...
}