1

How can I declare variables and assign values to them at run time.

Reason: I am fetching these variables values from sql server and these variable values are configurable in nature

Code which I have tried till now

   [array]$varArray = @($($ServerName),$($HostName)) 

 foreach($varname in $varArray)
        {
          $varname = "some test value"
        }

Write-Host $ServerName
Write-Host $HostName
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206

2 Answers2

7

The simplest way of using dynamically named variables would be a dictionary:

$vars = @{}  # create empty dictionary

# add key/value pairs to dictionary:
$vars["foo"] = 23
$vars["bar"] = "foobar"
$vars["baz"] = Get-Content C:\sample.txt

Another way would be to declare variables on the fly:

$name  = "foo"
$value = "bar"

New-Variable $name $value

echo $foo

Or you could create a custom object and add properties as Kyle C suggested. That approach is similar to a dictionary, although technically different.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Note that for the second method you can also use `Get-Variable -Name $name -ValueOnly`. You never actually need to know the name of the dynamic variable. – Bacon Bits Aug 18 '15 at 18:35
2

You could try adding a NoteProperty to the object.

$varname | Add-Member -type NoteProperty -name TestProperty -value "some test value" -PassThru

Also see this for what types of objects you can add a member to: What objects are suitable for Add-Member?

Community
  • 1
  • 1
Kyle C
  • 1,627
  • 12
  • 25
  • I have tried replacing $varname = "some text value" with *$varname | Add-Member -type NoteProperty -name $varname -value "some test value"* but it didn't work. I am quite sure I have missed the context of your answer, can you assist me as I am completely new to powershell. (started 5 hours back) – Zerotoinfinity Mar 26 '13 at 21:23
  • Do you have a little more information about the object type you are getting back? can you do $varname | Format-List ? – Kyle C Mar 26 '13 at 21:27
  • All those variables which is defined in array (ex:servername, hostname.. etc) are of string type only. – Zerotoinfinity Mar 26 '13 at 22:14
  • with this code *[array]$varArray = @($($ServerName),$($HostName)) foreach($varname in $varArray) { $varname = "some test value" Write-Host $varname | Format-List }* I am getting some test value some test value – Zerotoinfinity Mar 26 '13 at 22:14
  • you don't want to assign $varname - can you try $varname.GetType().FullName without assigning anything to it? – Kyle C Mar 26 '13 at 22:34
  • *[array]$varArray = @($($ServerName),$($HostName)) foreach($varname in $varArray) { Write-Host $varname.GetType().FullName }* I am getting below exception *Write-Host $varname.GetType <<<< ().FullName + CategoryInfo : InvalidOperation: (GetType:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull * I am not sure but is there any issue with the declaration with the array? My only purpose is to get variable values from sql server. – Zerotoinfinity Mar 27 '13 at 08:26