问题描述
我有一些代码可以从PS get-mailbox cmdlet检索集合作为输出.对于只有一个值的属性,此方法效果很好.同样,对于GrantSendOnBehalfTo之类的多值属性,它将返回一个单个字符串,以 空格.
I have some code which retrieves a collection as output from a PS get-mailbox cmdlet. This works fine for attributes with only one value. Also, for multi-value attributes like GrantSendOnBehalfTo it will return a single string delimited with spaces.
示例:单值属性(邮件)返回"[email protected]" .
Example: Single value attribute (mail) returns '[email protected]'.
如果有3个人,则多值GrantSendOnBehalfTo返回'jbloggs bsmith fjones'.
Multi value GrantSendOnBehalfTo returns 'jbloggs bsmith fjones' if there are 3 people.
我一直在通过基于单个空格定界符将其拆分为一个数组来对其进行处理.但是,我刚刚在另一个服务器环境中进行了尝试,发现该区域中的返回值中包含空格,因此我无法分裂 数组.我已经尝试了ICollection的测试(请参见下文),但无法正常工作.显然,输出实际上是一个对象.任何人都可以建议用于检测多值类型然后枚举值的语法,以便它们不会 只是一个字符串?
I have been processing this by splitting it to an array based on the single space delimiter. However I have just tried this in another server environment and have found that the returned values in that area have spaces in them, so I can't split to an array. I have tried a test for ICollection (see below) but it is not working. Apparently the output is actually an object. Can anyone advise the syntax for detecting the multi-value type and then enumerating the values so they are not just a single string?
Collection<PSObject> results = execPSO365RemoteCmdlet(cmdlet);
foreach (PSObject result in results) {
foreach (PSProperty prop in result.Properties) {
String propName = prop.Name;
Object propValue = prop.Value;
retval = retval + "~" + prop.Name + ":--zzzzz--:";
if (propValue is ICollection) {
Log("is ICollection");
ICollection collection = (ICollection)propValue;
retval = retval + "{";
foreach (object value in collection) {
if (value != null) {
retval = retval + value.ToString() + ";";
}
}
retval = retval + "}";
}
else
{
if (propValue != null) {
retval = retval + propValue.ToString();
}
}
retval = retval + "~";
}
}
OL2LS
推荐答案
谢谢您在这里发布.
对于您的问题,您定义为通用集合的结果.
For your question, the results you defined as a generic collection.
Collection<PSObject> results = execPSO365RemoteCmdlet(cmdlet);
对象propValue通过ICollection进行检查.
The Object propValue is check with ICollection.
if (propValue is ICollection) { }
ICollection 可以在没有Collection< T>的情况下进行如下检查.
The ICollection could check like the following without Collection<T>.
public interface ICollection : IEnumerable
ICollection d 为所有对象定义大小,枚举器和同步方法非常规集合.
The ICollection defines size, enumerators, and synchronization methods for allnongeneric collections.
您可以将ICollection更改为ICollection< PSObject>.
我希望这会有所帮助.
最好的问候,
温迪
这篇关于在GrantSendOnBehalfTo中枚举多个值.使用C#从Powershell返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!