'Create unique variables in a loop powershell/Jenkins

I am trying save urls created from an array into unique variables created in a loop in a Jenkins freestyle project. I need to use these variables in the next step to send in an email.

As per this answer to get the value of the variable var$i with the index $i we need to use this expression Get-Variable -Name "var$i" -ValueOnly .

So, to create the env.properties files with the keys as indexed variables and each containing it's repective url as value, I am using the following code. However, I get the error.

var$i is not recognized as a cmdlet... on this line. var$i = [uri]::EscapeUriString($prefix+$files1[$i]) .

$files1 = @(Get-ChildItem c:\Users\rocky\Documents)
$count = ($files1).Count
$prefix="https://my.sharepoint.com/personal/abhishek/Documents/Desktop/"
for($i =0;$i -le $count; $i++){
    New-Variable -Name "var$i" -Value $i
    var$i = [uri]::EscapeUriString($prefix+$files1[$i])
    "var$i" = Get-Variable -Name "var$i" -ValueOnly | Out-File env.properties -Encoding ASCII
    
 }

What would be the correct way to create this?

The env.properties file is simply a key value pair. Inside it has Key Value pairs like so

Link1=https://example.com
Link2=https://example1.com

It appears I will have to write some groovy code in the body of the email itself. That would be better than to go with this avoidable idea.



Solution 1:[1]

I finally solved it like this

$files1 =Get-ChildItem $ENV:WORKSPACE/ToscaPDFLogs
write-output $ENV:WORKSPACE
$linklist = [System.Collections.ArrayList]@()
$prefix="http://x.x.x.x:8080/job/OCM_Pipeline_OneDriveLink_Test_POC/ws/ToscaPDFLogs/"
foreach ($file in $files1) {
  $url1 = '<a href='+'"'+[uri]::EscapeUriString($prefix+$file.Name)+'">'+'PDF LINK'+'</a>'+'<br>'
  $null = $linklist.Add($url1) 
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Rai