Home > Blockchain >  Powershell manage set of variables
Powershell manage set of variables

Time:03-17

I have a script that read from excel collect files according to a column, Before the running I need to define 4 variables, the issue is that I need to run it for several versions. That means that for each run I need to leave active 4 variables and the rest should be in comment (#) and I do it manually (need to add 12 times # There is a smart way to choose all the 4 variables per version?

I will give an example:

enter image description here

Each time only one set is enabled (According to version I want to work on)

The using of those parameters are:

Add-VSTeamWorkItem -ProjectName "Client-Server" -Title "[Side VIP] [$VersionNumber]"

$Versionlocationforpatches = "$Sourceversion\Installer\Patches" 
$Versionlocationforcabs = "$Sourceversion\Installer" 

Add-VSTeamBuild -ProjectName "Client-Server" -BuildDefinitionId $PipelineID

function RenameVerFile ([string] $buildver) 
{
    $DefaultFile = Get-ChildItem $Sourceversion | Where-Object {$_.Name -like "BuildVer.txt"}
    Clear-Content $DefaultFile.FullName -Force 
    $Newcontent = Add-Content $DefaultFile.FullName -Value ($buildver   "."   (Get-Random)) 
    
}

Some of them are using in other places

CodePudding user response:

You could store and choose it that way with a hashtable:

$myVars = @{}
$myVars.Version2000 = @{"Path"="c:\";"Title"="MySensitiveData"}
$myVars.Version2000.Path
$myVars.Version2000.Title

it could then look like

PS C:\> $myVars = @{}
PS C:\> $myVars.Version2000 = @{
>> "Path"="c:\Prog2000"
>> "Title"="MySensitiveData"
>> "Var3"="Value3"
>> "Var4"="value4"
>> }
PS C:\> $myVars.Version2013 = @{
>> "Path"="c:\Prog2013"
>> "Title"="MySensitiveData"
>> "Var3"="Value3"
>> "Var4"="value4"
>> }
PS C:\> $myVars.Version2000

Name                           Value
----                           -----
Var4                           value4
Path                           c:\
Var3                           Value3
Title                          MySensitiveData
PS C:\> $myVars.Version2000.Path
c:\

EDIT: Added a new answer as more information came in OP

CodePudding user response:

Regarding your new OP data i assume you mean you have a configuration file wich define the variables. Then this answer would help.

$FilePath = ".\VersionDataFile.txt"
$FileData = gc $FilePath
$NewFileData = @()
$AllVersions = @()
foreach ($line in ($FileData | where {$_ -match "VersionNumber"})) {
    $AllVersions  = [string]($line -split "`"")[1]
}
"The following Versions were found"
$AllVersions
$VersionToUse = Read-Host "Enter version number"

foreach ($line in $Filedata) {
Switch ($true) {
($line -match '^#?\$VersionNumber') {if ($line -match "`"$VersionToUse`"") {$uncomment = $true} else {$uncomment = $false}}
($line -notmatch '^#?\$') {$NewFileData  = $line;break}
($uncomment -eq $true) {$NewFileData  = $line.TrimStart('#'); break}
($uncomment -eq $false) {$NewFileData  = "#"   $line.TrimStart('#')}
}
}
$NewFileData | Out-File -Encoding default $FilePath
Write-Output "`r`n`r`n`t The Following Data should have been written to $FilePath`r`n`r`n"
Write-Output $NewFileData
  • Related