Powershell to add value and remove the string

Kalaimani Thirupathi 411 Reputation points
2021-10-13T16:27:24.937+00:00

Dear All,

I need help to add the Comma value.

for example,

getting some value from azure ID, as below
$Date
ID1
ID2
ID3
ID4
ID5

now we need to add all in between comma (,)

like below

$Data=ID1,ID2,ID3,ID4,ID5

I have used the below one but getting one extra comma (,)

$RCvaults = Get-AzRecoveryServicesVault
$allJobs = @()
ForEach ($RCvault in $RCvaults)
{

$ID=$RCvault.ID

$allJobs+=$ID + ","

}
OUTput
$allJobs = ID1,ID2,ID3,ID4,ID5**,** the last comma need to be removed

Kindly someone help with this, please.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,786 questions
Azure Backup
Azure Backup
An Azure backup service that provides built-in management at scale.
1,120 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,696 Reputation points
    2021-10-13T18:19:07.73+00:00

    If you hadn't already chosen your own code as the answer, I would have told you to use the one @Michael Taylor suggested.

    But, here's one that takes all that code you wrote and does it all with one much more understandable statement:

    $alljobs = (Get-AzRecoveryServicesVault).Id -join ','  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 47,716 Reputation points
    2021-10-13T16:41:03.353+00:00

    Couple of options. The PS native approach is the join operator.

       $items -Join ","  
    

    Personally I prefer String.Join.

       [String]::Join(",", $items)  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Kalaimani Thirupathi 411 Reputation points
    2021-10-13T16:44:17.51+00:00

    The below one worked.

    $allJobs = @()
    ForEach ($RCvault in $RCvaults)
    {
    $a=$RCvault.ID
    $b=$a
    $allJobs+= $b
    }
    foreach($C in $allJobs)
    {
    $d+=$C +","
    }
    $Scope = $d.Substring(0,$d.Length-1)

    0 comments No comments