Using non-string as $env: variable
Asked Answered
C

3

6

I'd like to store a hashtable in an environmental variable--is that possible in Powershell?

Here's how I'm testing this:

$env:test = @{}
$env:test
$env:test|gm

And getting confused, because output is:

System.Collections.Hashtable

TypeName: System.String

Name MemberType Definition

---- ---------- ---------- Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB) ...

... So when I return $env:test directly, I get 'Hashtable', but when I pipe it to get-member, system.string is returned. Can someone please explain this behaviour?

Cadwell answered 29/10, 2012 at 19:26 Comment(5)
Why do you require doing this in an env variable?Leipzig
Also, env variables are always strings. Period.Leipzig
I have a hash of {[int]$id,[System.Management.Automation.PSCredential]$cred} which I want to be able to access from anywhere in the script. env vars being always strings makes sense, but why the odd behavior described in original question?Cadwell
A variable with global or script scope would be accessible from anywhere in the script. At a prompt type: Get-Help about_scopesLeipzig
No particular reason to need env var. I have a few vars I store in a custom env space (e.g.: $env:MYSPACE:foo) and was wondering if I can store objects or references in that space as well.Cadwell
A
1

Environment variables ($env:stringVariable) are strings. However, it appears you want to make a non-string variable of type hashtable global. That can be done by using the global modifier ($global:hashtableVariable). This will allow you to access the variable from one script file in another as demostrated here

PowerShellScriptFile-1.ps1

$global:hashtableVariable = @{
   'key1' = 'value1'
   'key2' = 'value2'
}

PowerShellScriptFile-2.ps1

$global:hashTableVariable.GetEnumerator() | ForEach-Object {
       Write-Host "$($_.Key):$($_.Value)"
}
Aniakudo answered 12/7, 2016 at 5:33 Comment(0)
C
0

Got it. "System.Collections.Hashtable" is the string value assigned to $env:test

Cadwell answered 29/10, 2012 at 20:6 Comment(1)
Yes. Since an env variable can only ever be a string, when you assigned a hash to it, Pwershell attempted to coerce the hash into being a string. To do this, Powershell calls the .ToString() method on the hashtable object which returns System.Collections.Hashtable. This is thus stored in the env variable.Leipzig
K
0

More than a bit late to the party, but now you can do this, though since environment variables are strictly strings, you'll need to assign it to something else in your code, and you will need to use the Microsoft.PowerShell.Utility module.

import-module Microsoft.PowerShell.Utility 
$hash = ConvertFrom-StringData -StringData $env:myvar

you will need to appropriately format your environment variable eg:

$env:myvar = "Fname = John ``n Lname= Smith ``n DOB = 01/01/1980"

Use only one backtick, I can't figure out how to get only one to display right.

see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-stringdata?view=powershell-7

Kassala answered 31/3, 2020 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.