0

I have a csv file that contains fields with values that begin with "$" that are representative of variables in different powershell scripts. I am attempting to import the csv and then replace the string version of the variable (ex. '$var1') with the actual variable in the script. I have been able to isolate the appropriate strings from the input but I'm having difficulty turning the corner on modifying the value.

Example:

CSV input file -

In Server,Out Server
\\$var1\in_Company1,\\$var2\in_Company1
\\$var1\in_Company2,\\$var2\in_Company2

Script (so far) -

$Import=import-csv "C:\temp\test1.csv"
$Var1="\\server1"
$Var2="\\server2"
$matchstring="(?=\$)(.*?)(?=\\)"
$Import| %{$_ | gm -MemberType NoteProperty | 
%{[regex]::matches($Import.$($_.name),"$matchstring")[0].value}}

Any thoughts as to how to accomplish this?

2 Answers 2

2

The simplest way to address this that I could think of was with variable expansion as supposed to replacement. You have the variables set in the CSV so lets just expand them to their respective values in the script.

#  If you dont have PowerShell 3.0 -raw will not work use this instead
#  $Import=Get-Content $path | Out-String

$path = "C:\temp\test1.csv"
$Import = Get-Content $path -Raw
$Var1="\\server1"
$Var2="\\server2"
$ExecutionContext.InvokeCommand.ExpandString($Import) | Set-Content $path

This will net the following output in $path

In Server,Out Server
\\\\server1\in_Company1,\\\\server2\in_Company1
\\\\server1\in_Company2,\\\\server2\in_Company2

If the slashes are doubled up here and you do not want them to be then just change the respective variable values in your script of the data in the CSV.

Caveat

This has the potential to execute malicious code you did not mean too. Have a look at this thread for more on Expanding variables in file contents. If you are comfortable with the risks then this solution as presented should be fine.

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I am looking for. The quadruple slashes are just due to me trowing together a quick simplified example and missing that detail. Thanks!
Ultimately I'll be applying the expand string method into what I was doing previously as I'm looking to work with the current variable values though not store them back to the source CSV file. $Import| %{$_ | gm -MemberType NoteProperty | %{$ExecutionContext.InvokeCommand.ExpandString($Import.$($_.name))}}
@TreyNuckolls Whatever works for you. Glad I could help.
1

Maybe I misunderstood, but do you need this?

$Import=import-csv "C:\temp\test1.cvs"
$Var1="\\server1"
$Var2="\\server2" 

Foreach ($row in $Import )
{
    $row.'In Server' = ($row.'In Server' -replace '\\\\\$var1', "$var1")
    $row.'Out Server' = ($row.'Out Server' -replace '\\\\\$var2', "$var2")    
}

$import | set-content $path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.