繁体   English   中英

PSCustomObject在ISE中有效,但在powershell.exe中无效

[英]PSCustomObject Works in ISE but not in powershell.exe

Powershell的新功能。 我正在使用用户信息创建自定义对象的列表,然后遍历此列表并将用户添加到AD。 我的脚本在ISE中运行良好,但是我想将其设置为Windows中的计划任务。 这样做时,我发现在powershell.exe中直接调用它不起作用。 重要的位:

    $usersToAdd = New-Object System.Collections.Generic.List[System.Object]

    foreach($obj in $listofobjs)
    {            $user = [PSCustomObject]@{
                'param1' = "username"
                'param2' = "group"
               }
                 $usersToAdd.Add($user)
     } 
        ForEach($user in $usersToAdd)
        {
            Add-ADGroupMember -Identity $user.param1 -Members $user.param2 -ErrorAction Stop
        }

在命令行中,添加cmdlet失败,因为参数为null。 回显$ user变量给我:

@{param1=stuff; param2=stuff2}

在ISE中,它给出了:

param1 : stuff
param2    : stuff2

编辑:全文回显然后在命令行$ user | 获得会员:

TypeName   : System.String
Name       : Clone
MemberType : Method
Definition : System.Object Clone(), System.Object ICloneable.Clone()


TypeName   : System.String
Name       : CompareTo
MemberType : Method
Definition : int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object
             obj), int IComparable[string].CompareTo(string other)


TypeName   : System.String
Name       : Contains
MemberType : Method
Definition : bool Contains(string value)


TypeName   : System.String
Name       : CopyTo
MemberType : Method
Definition : void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)


TypeName   : System.String
Name       : EndsWith
MemberType : Method
Definition : bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool
             EndsWith(string value, bool ignoreCase, cultureinfo culture)


TypeName   : System.String
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value,
             System.StringComparison comparisonType), bool IEquatable[string].Equals(string other)


TypeName   : System.String
Name       : GetEnumerator
MemberType : Method
Definition : System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator(),
             System.Collections.Generic.IEnumerator[char] IEnumerable[char].GetEnumerator()


TypeName   : System.String
Name       : GetHashCode
MemberType : Method
Definition : int GetHashCode()


TypeName   : System.String
Name       : GetType
MemberType : Method
Definition : type GetType()


TypeName   : System.String
Name       : GetTypeCode
MemberType : Method
Definition : System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()


TypeName   : System.String
Name       : IndexOf
MemberType : Method
Definition : int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex,
             int count), int IndexOf(string value), int IndexOf(string value, int startIndex), int IndexOf(string
             value, int startIndex, int count), int IndexOf(string value, System.StringComparison comparisonType), int
             IndexOf(string value, int startIndex, System.StringComparison comparisonType), int IndexOf(string value,
             int startIndex, int count, System.StringComparison comparisonType)


TypeName   : System.String
Name       : IndexOfAny
MemberType : Method
Definition : int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf,
             int startIndex, int count)


TypeName   : System.String
Name       : Insert
MemberType : Method
Definition : string Insert(int startIndex, string value)


TypeName   : System.String
Name       : IsNormalized
MemberType : Method
Definition : bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)


TypeName   : System.String
Name       : LastIndexOf
MemberType : Method
Definition : int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int
             startIndex, int count), int LastIndexOf(string value), int LastIndexOf(string value, int startIndex), int
             LastIndexOf(string value, int startIndex, int count), int LastIndexOf(string value,
             System.StringComparison comparisonType), int LastIndexOf(string value, int startIndex,
             System.StringComparison comparisonType), int LastIndexOf(string value, int startIndex, int count,
             System.StringComparison comparisonType)


TypeName   : System.String
Name       : LastIndexOfAny
MemberType : Method
Definition : int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int
             LastIndexOfAny(char[] anyOf, int startIndex, int count)


TypeName   : System.String
Name       : Normalize
MemberType : Method
Definition : string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)


TypeName   : System.String
Name       : PadLeft
MemberType : Method
Definition : string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)


TypeName   : System.String
Name       : PadRight
MemberType : Method
Definition : string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)


TypeName   : System.String
Name       : Remove
MemberType : Method
Definition : string Remove(int startIndex, int count), string Remove(int startIndex)


TypeName   : System.String
Name       : Replace
MemberType : Method
Definition : string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)


TypeName   : System.String
Name       : Split
MemberType : Method
Definition : string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[]
             Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count,
             System.StringSplitOptions options), string[] Split(string[] separator, System.StringSplitOptions
             options), string[] Split(string[] separator, int count, System.StringSplitOptions options)


TypeName   : System.String
Name       : StartsWith
MemberType : Method
Definition : bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType),
             bool StartsWith(string value, bool ignoreCase, cultureinfo culture)


TypeName   : System.String
Name       : Substring
MemberType : Method
Definition : string Substring(int startIndex), string Substring(int startIndex, int length)


TypeName   : System.String
Name       : ToBoolean
MemberType : Method
Definition : bool IConvertible.ToBoolean(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToByte
MemberType : Method
Definition : byte IConvertible.ToByte(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToChar
MemberType : Method
Definition : char IConvertible.ToChar(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToCharArray
MemberType : Method
Definition : char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)


TypeName   : System.String
Name       : ToDateTime
MemberType : Method
Definition : datetime IConvertible.ToDateTime(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToDecimal
MemberType : Method
Definition : decimal IConvertible.ToDecimal(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToDouble
MemberType : Method
Definition : double IConvertible.ToDouble(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToInt16
MemberType : Method
Definition : int16 IConvertible.ToInt16(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToInt32
MemberType : Method
Definition : int IConvertible.ToInt32(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToInt64
MemberType : Method
Definition : long IConvertible.ToInt64(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToLower
MemberType : Method
Definition : string ToLower(), string ToLower(cultureinfo culture)


TypeName   : System.String
Name       : ToLowerInvariant
MemberType : Method
Definition : string ToLowerInvariant()


TypeName   : System.String
Name       : ToSByte
MemberType : Method
Definition : sbyte IConvertible.ToSByte(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToSingle
MemberType : Method
Definition : float IConvertible.ToSingle(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToString
MemberType : Method
Definition : string ToString(), string ToString(System.IFormatProvider provider), string
             IConvertible.ToString(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToType
MemberType : Method
Definition : System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)


TypeName   : System.String
Name       : ToUInt16
MemberType : Method
Definition : uint16 IConvertible.ToUInt16(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToUInt32
MemberType : Method
Definition : uint32 IConvertible.ToUInt32(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToUInt64
MemberType : Method
Definition : uint64 IConvertible.ToUInt64(System.IFormatProvider provider)


TypeName   : System.String
Name       : ToUpper
MemberType : Method
Definition : string ToUpper(), string ToUpper(cultureinfo culture)


TypeName   : System.String
Name       : ToUpperInvariant
MemberType : Method
Definition : string ToUpperInvariant()


TypeName   : System.String
Name       : Trim
MemberType : Method
Definition : string Trim(Params char[] trimChars), string Trim()


TypeName   : System.String
Name       : TrimEnd
MemberType : Method
Definition : string TrimEnd(Params char[] trimChars)


TypeName   : System.String
Name       : TrimStart
MemberType : Method
Definition : string TrimStart(Params char[] trimChars)


TypeName   : System.String
Name       : Chars
MemberType : ParameterizedProperty
Definition : char Chars(int index) {get;}


TypeName   : System.String
Name       : Length
MemberType : Property
Definition : int Length {get;}

ISE:

    TypeName   : System.Management.Automation.PSCustomObject
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)


TypeName   : System.Management.Automation.PSCustomObject
Name       : GetHashCode
MemberType : Method
Definition : int GetHashCode()


TypeName   : System.Management.Automation.PSCustomObject
Name       : GetType
MemberType : Method
Definition : type GetType()


TypeName   : System.Management.Automation.PSCustomObject
Name       : ToString
MemberType : Method
Definition : string ToString()


TypeName   : System.Management.Automation.PSCustomObject
Name       : param1
MemberType : NoteProperty
Definition : string param1=stuff


TypeName   : System.Management.Automation.PSCustomObject
Name       : param2
MemberType : NoteProperty
Definition : string param2=stuff2

因此,就像外壳无法识别PSCustomObject一样。 我确实修改了脚本以使用Hashtables,并且工作正常。 但是两个参数上都有重复的键,因此我需要一个自定义对象。 是否需要为PSCustomObject导入模块?

由于我们真的不知道$listofobjs来自什么数据, $listofobjs这是我能提供的最好的数据。 如果您使用示例数据更新您的问题,我可以对其进行修改以适合您的情况。

$usersToAdd = @(foreach($obj in $listofobjs)
    {           
        New-Object psobject -property @{
            username = # property from obj related to username
            group = # property from obj related to group
       }
    })
ForEach($user in $usersToAdd)
{
    Add-ADGroupMember -Identity $user.username -Members $user.group -ErrorAction Stop
}

这是做什么的

$usersToAdd只是一个简单的$usersToAdd数组,它将包含您的用户对象。 这样做是一种性能度量,因为它将遍历所有对象并创建psobject,然后将刚创建的对象数组返回到数组$usersToAdd而不是单独添加每个对象。 这是一个节省大量对象的省时方法,否则它可以按照您的方式工作。

New-Object psobject命令创建一个新的powershell自定义对象,您可以在其中创建属性的名称(在左侧)和分配给它们的值(在右侧)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM