Forum Discussion
John_Dodo
Oct 26, 2022Brass Contributor
Powershell : interprete string as variable name
Hello, I want to importe values from a csv file. Some column have this sort of values : $myvar. So it would be like this : log_message;server;scriptfolder;scriptname;action blabla;myserver;$s...
LainRobertson
Oct 26, 2022Silver Contributor
There's a number of ways to do this - none of them particularly pretty.
I've gone with a basic example that makes use of Invoke-Expression:
Get-Content -Path D:\Data\Temp\forum.csv |
ForEach-Object {
if (-not $Header -and -not [string]::IsNullOrWhiteSpace($_))
{
$Header = $_.Split(';');
}
elseif ($Header)
{
ConvertFrom-Csv -InputObject (Invoke-Expression -Command "`"$_`"") -Delimiter ';' -Header $Header;
}
}
Here's what the example looks like in action.
Cheers,
Lain