0

I'm trying to convert String datatype to Object[].

Required Format and Required Datatype:

$demilimitedIds = {/subscriptions/XXX},{/subscriptions/XXX}
echo  $demilimitedIds.GetType().Name  ##datatype of $demilimitedIds is Object[]

My Code:

[System.Collections.Generic.List[System.String]]$IDList = @()
        $IDList.Add("/subscriptions/XXX")   
        $IDList.Add("/subscriptions/XXX")

        $Alist =  '{{{0}}}' -f ($IDList -join '},{') 
        echo "%%%%%%%%%%%%%%%"
        echo  $Alist.GetType().Name //o/p format of $Alist is String
        echo "%%%%%%%%%%%%%%%"

I'm trying to convert the $IDList into required format similar to $demilimitedIds and of same Object[] datatype.

How to convert datatype of $Alist from String to Object[] ?

6
  • What do you actually need? To convert $Alist to an object or just create an object from values instead of the way you're creating $IDlist? Commented Jan 13, 2023 at 7:18
  • $Alist is to create an object from values instead of the way you're creating $IDlist and this datatype should be Object[] to send as a parameter to other powershell command Commented Jan 13, 2023 at 7:22
  • $IDList can already be used as Object[] with a parameter. If you really need to use $Alist then $Alist = @($IDList) will give you Object[] with the contents of $IDList Commented Jan 13, 2023 at 7:33
  • Hi Daniel .. First I need convert the $IDList into this format {/subscriptions/XXX},{/subscriptions/XXX}.After conversion stored in $Alist. This $Alist datatype is coming as String. Need to convert this String datatype to Object[] Commented Jan 13, 2023 at 7:42
  • $Alist = @($Alist) Commented Jan 13, 2023 at 7:45

2 Answers 2

0

Still not clear what you're trying to achieve here, but...

# Just create $IDlist as an object:

$IDList = New-Object PSObject           
$IDList.Add("/subscriptions/XXX")   
$IDList.Add("/subscriptions/XXX")

Having said this, you can use $IDlist as an input object already as Daniel mentions

Sign up to request clarification or add additional context in comments.

1 Comment

$IDList is to be formatted first and should be stored in $Alist and this $Alist should be of Object[] to send as parameter to other powershell command
0

Or use a ForEach-Object loop like

$IDList = [System.Collections.Generic.List[System.String]]::new()
$IDList.Add("/subscriptions/XXX")   
$IDList.Add("/subscriptions/XYZ")

$Alist = $IDList | ForEach-Object {'{{{0}}}' -f $_ }

$Alist is now a string array (Object[]) containing

{/subscriptions/XXX}
{/subscriptions/XYZ}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.