Home > Mobile >  How to convert String to Object[] in powershell?
How to convert String to Object[] in powershell?

Time:01-14

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[] ?

CodePudding user response:

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

CodePudding user response:

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}
  • Related