powershell - inline variable replace

343 views Asked by At

I have a SQL script that contains statements such as the following:

CREATE GROUP $MyGroup;
CREATE USER $MyUser;

Is there a way using Powershell that I can echo out the file contents with those variables replaced with their current values as set previously?

So if I had previously issued:

$MyGroup = 'group';
$MyUser = 'user';

the resultant string, after reading the file contents and substituting the variables, would be:

CREATE GROUP group;
CREATE USER user;
1

There are 1 answers

0
carlpett On BEST ANSWER

Yes, through the $ExecutionContext you can access the ExpandString function:

$fileContents = Get-Content myfile.sql
$ExecutionContext.InvokeCommand.ExpandString($fileContents)