简体   繁体   中英

PSCustomObject Works in ISE but not in powershell.exe

New to powershell. I'm creating a list of custom objects with user info, then iterating through this list and adding the users to AD. My script works perfectly in the ISE, but I want to set it as a scheduled task within windows. When doing this I discovered it doesn't work when called directly in powershell.exe. The important bits:

    $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
        }

In the commandline the add cmdlet fails because a parameter is null. Echoing out the $user variable gives me:

@{param1=stuff; param2=stuff2}

In ISE echoing it gives:

param1 : stuff
param2    : stuff2

EDIT: full text echos Then in the command line $user | Get-Member:

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

So its like the shell isn't recognizing the PSCustomObject. I did modify the script to use Hashtables and it works fine. But there a chance of duplicate keys on both parameters so I need a custom object. Is there a module I need to import for PSCustomObject?

Since we don't really know what data is coming from $listofobjs here is the best I can come up with. If you update your question with sample data to work with, I can modify this to work with your situation.

$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
}

What this does :

$usersToAdd is just a simple powershell array that will contain your user objects. Doing it this way is a performance measure because it will loop through all the objects and create the psobject and then return the array of objects just created to the array $usersToAdd rather than adding each one individually. This is a big time saver with lots of objects, otherwise it could work the way that you made it.

The New-Object psobject command creates a new powershell custom object where you can create the names of the properties (on the left) and the values assigned to them (on the right)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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