Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Single item to array – fails

$
0
0

Hi!

I have a function that returns users (AD) based on search properties. The output then gets converted to an array. My issue is that if the search yields one result it does not convert to an array – if returned result count is 2 or higher it works perfectly.

Unfortunately I cannot modify the search function so I need to handle the output as-is and will need to deal with the output.

So let’s say I am searching for the following string: “Doe” – my results are (in $users):

@{UserPrincipalName=john.doe@contoso.com; givenName=John; CanonicalName=contoso.com/Users/John Doe; distinguishedName=CN=John Doe,OU=Users,DC=contoso,DC=com; sAMAccountName=jdoe01; sn=Doe} @{UserPrincipalName=jane.doe@contoso.com; givenName=Jane; CanonicalName=contoso.com/Users/Jane Doe; distinguishedName=CN=Jane Doe,OU=Users,DC=contoso,DC=com; sAMAccountName=jdoe02; sn=Doe} 

on this I can call:

$array = [System.Collections.ArrayList]$users 

and it converts to an array perfectly.

However if I search for “John Doe” the value in $users is:

@{UserPrincipalName=john.doe@contoso.com; givenName=John; CanonicalName=contoso.com/Users/John Doe; distinguishedName=CN=John Doe,OU=Users,DC=contoso,DC=com; sAMAccountName=jdoe01; sn=Doe} 

and this is something that doesn’t convert, error is:

Cannot convert the "@{UserPrincipalName=john.doe@contoso.com; givenName=John; CanonicalName=contoso.com/Users/John Doe; distinguishedName=CN=John Doe,OU=Users,DC=contoso,DC=com; sAMAccountName=jdoe01; sn=Doe}" value of type "System.String" to type "System.Collections.ArrayList". At line:1 char:1 + $array = [System.Collections.ArrayList]$users + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : ConvertToFinalInvalidCastException 

How can I convert this to an array?

Thank you!

submitted by /u/colonial113
[link] [comments]

The post Single item to array – fails appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Latest Images

Trending Articles



Latest Images