I want to declare variables with random numbers in the name, such as
$var123 = "A"
$var456 = "B"
$var789 = "C"
by using Get-Random
for the number part.
What have I to write behind $var
if $rnd = Get-Random
? Or is there another way to declare such variables?
Update: The real use case is that I have trouble with Add-Type
and hoped I could solve it with this workaround. But it still does not work as expected. It works for the very first time, but after changing the C# code, it still shows the old code.
Any suggestions? I am running PowerShell 7.2.1 on a Mac computer.
$rnd = Get-Random
$code =
@"
using System;
namespace HelloWorld {
public class Program {
public static int Main() {
#return 123; # first try, working well
return 456; # after changing, still returns 123
}
}
}
"@
New-Variable -Name "var$rnd" -value $code
Add-Type -TypeDefinition (Get-Variable -Name "var$rnd" -ValueOnly) -Language CSharp
Write-Host (Invoke-Expression "[HelloWorld.Program]::Main()")