Home > Software engineering >  Combine Variable - PowerShell
Combine Variable - PowerShell

Time:12-04

Is there a way to combine two string and make it as a current variable and get the data from it? I'm not sure if there's another way to do this. If there's any, please let me know. THANK YOU!

Here's my code.

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')
ForEach ($Applications in $Apps) {
    $AppToBeInstalled = '$App'   $_
    $AppExt = '$AppExt'   $_
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

What I want.

Application Google Chrome to be installed with extension .exe
Application Mozilla to be installed with extension .msi

But I'm only getting empty result.

Application  to be installed with extension 
Application  to be installed with extension 

CodePudding user response:

This is doable, though quite unorthodox, you can use Get-Variable to get variables using dynamic names:

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')

ForEach ($Applications in $Apps) {
    $AppToBeInstalled = (Get-Variable App$Applications).Value
    $AppExt = (Get-Variable AppExt$Applications).Value
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

> Application Google Chrome to be installed with extension .exe
> Application Mozilla to be installed with extension .msi

What might be a more common approach to this would be to use a hashtable:

$map = @{
    "Google Chrome" = ".exe"
    "Mozilla" = ".msi"
}

ForEach ($key in $map.Keys) {
    $AppToBeInstalled = $key
    $AppExt = $map[$key]
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

> Application Google Chrome to be installed with extension .exe
> Application Mozilla to be installed with extension .msi

CodePudding user response:

Yes you can you can use

Get-Variable -Name "nameOfVar" -ValueOnly

in your case it would look something like

$App1 = "Google Chrome"
$App2 = "Mozilla"
$AppExt1 = ".exe"
$AppExt2 = ".msi"
$Apps = @('1','2')
ForEach ($Applications in $Apps) {
    $AppToBeInstalled = Get-Variable -Name "App$Applications" -ValueOnly
    $AppExt = Get-Variable -Name "AppExt$Applications" -ValueOnly
    Write-Host "Application $AppToBeInstalled to be installed with extension $AppExt"
}

CodePudding user response:

As some people mentioned you can create custom variables then call them from within the for loop:

$App1 = [pscustomobject]@{
    Name = "Google Chrome"
    Ext = ".exe"
}
$App2 = [pscustomobject]@{
    Name = "Mozilla"
    Ext = ".msi"
}

$Apps=@($App1,$App2)

ForEach ($App in $Apps) {
    $AppName = $App.Name
    $AppExt = $App.Ext
    Write-Host "Application $AppName to be installed with extension $AppExt"
}

Output will be:

Application Google Chrome to be installed with extension .exe
Application Mozilla to be installed with extension .msi

Might make it easier if you have to keep adding more apps to the script in the future.

  • Related