我正在专门为 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/