PSCustomObject在ISE中工作,但在powershell.exe中不起作用

时间:2018-03-19 18:31:54

标签: powershell

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
        }

在命令行中,add 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

所以就像shell没有识别PSCustomObject一样。我确实修改了脚本以使用Hashtables,它工作正常。但是两个参数都有可能重复键,所以我需要一个自定义对象。我需要为PSCustomObject导入一个模块吗?

1 个答案:

答案 0 :(得分:0)

因为我们真的不知道来自$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只是一个包含用户对象的简单PowerShell数组。这样做是一种性能测量,因为它将循环遍历所有对象并创建psobject,然后将刚刚创建的对象数组返回到数组$usersToAdd,而不是单独添加每个对象。这可以节省大量的时间,否则它可以像你制作它一样工作。

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