0

I am getting strange behaviour when passing an array object from my Main script into RunSpace code block. I am trying to retrieve values using a Where-Object statement within the RunSpace code block. But produces an error relating to null method.

If I perform the same Where-Object statement on the array before passed to runspace then it works?

For example if I perform the following on the array before its passed to RunSpace it works fine:

$ARRAY | Where-Object {$_.object -eq $Variable}).PropertyofwhatIWant.ToString()

If I pass this array to runspace and perform the same thing it fails with:

You cannot call a method on a null-valued expression.
At line:25 char:13
+             $strSourceGivenName = ($arr_Src_Users1 | Where-Object {$_.objectsid  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I have compared both array objects and they have the exact same properties? There is no difference of ARRAY and ARRAY1 when checking Get-Member? What is going on?

Module                     : CommonLanguageRuntimeLibrary
Assembly                   : mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
TypeHandle                 : System.RuntimeTypeHandle
DeclaringMethod            : 
BaseType                   : System.Array
UnderlyingSystemType       : System.Object[]
FullName                   : System.Object[]
AssemblyQualifiedName      : System.Object[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Namespace                  : System
GUID                       : 00000000-0000-0000-0000-000000000000
IsEnum                     : False
GenericParameterAttributes : 
IsSecurityCritical         : False
IsSecuritySafeCritical     : False
IsSecurityTransparent      : True
IsGenericTypeDefinition    : False
IsGenericParameter         : False
GenericParameterPosition   : 
IsGenericType              : False
IsConstructedGenericType   : False
ContainsGenericParameters  : False
StructLayoutAttribute      : 
Name                       : Object[]
MemberType                 : TypeInfo
DeclaringType              : 
ReflectedType              : 
MetadataToken              : 33554432
GenericTypeParameters      : {}
DeclaredConstructors       : {Void .ctor(Int32)}
DeclaredEvents             : {}
DeclaredFields             : {}
DeclaredMembers            : {Void Set(Int32, System.Object), System.Object& Address(Int32), System.Object Get(Int32), Void .ctor(Int32)}
DeclaredMethods            : {Void Set(Int32, System.Object), System.Object& Address(Int32), System.Object Get(Int32)}
DeclaredNestedTypes        : {}
DeclaredProperties         : {}
ImplementedInterfaces      : {System.ICloneable, System.Collections.IList, System.Collections.ICollection, System.Collections.IEnumerable...}
TypeInitializer            : 
IsNested                   : False
Attributes                 : AutoLayout, AnsiClass, Class, Public, Sealed, Serializable
IsVisible                  : True
IsNotPublic                : False
IsPublic                   : True
IsNestedPublic             : False
IsNestedPrivate            : False
IsNestedFamily             : False
IsNestedAssembly           : False
IsNestedFamANDAssem        : False
IsNestedFamORAssem         : False
IsAutoLayout               : True
IsLayoutSequential         : False
IsExplicitLayout           : False
IsClass                    : True
IsInterface                : False
IsValueType                : False
IsAbstract                 : False
IsSealed                   : True
IsSpecialName              : False
IsImport                   : False
IsSerializable             : True
IsAnsiClass                : True
IsUnicodeClass             : False
IsAutoClass                : False
IsArray                    : True
IsByRef                    : False
IsPointer                  : False
IsPrimitive                : False
IsCOMObject                : False
HasElementType             : True
IsContextful               : False
IsMarshalByRef             : False
GenericTypeArguments       : {}
CustomAttributes           : {[System.SerializableAttribute()]}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Anon
  • 1

2 Answers2

0

I solved my issue: Nothing to do with Runspace. It was to do with the way PS deals with: TypeName: Microsoft.ActiveDirectory.Management.ADUser

So I filtered out the properties I needed and created a custom object and passed this to my Function :-)

Anon
  • 1
0

As soon as you pass the Microsoft.ActiveDirectory.Management.ADUser Object to another function, the properties are no longer available even though if you pipe out the Array you can see all the properties as you would in the main call.. I will do more digging to why this is but for now my workaround works well

Anon
  • 1
  • This is basically an addendum to your other self-answer. Please don't do this. SO is neither a forum nor a chat. Use the "edit" link below your question or answer if you need to add something to it. – Ansgar Wiechers Oct 23 '16 at 21:54