VMware Cloud Community
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Replace placeholders in script

Hi,

I have a script (master.ps1), inside which I calculate the value of $installerFile.

I then need this to read in the contents of a second script (agent.ps1), and overwrite the placeholder (it currently has dummy data in there like "$installerFile = "temp.exe").

Does anyone know how to do this? I have a feeling it's to do with Invoke-Expression, but I'm not sure.

Any help would be great, thank you.

-Mark

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think I got what you are trying to do.
Luckily the -replace operator works with RegEx.
This replaces "test.exe" with "WhatEver.exe" on the $installer line

# Preamble

$fileName = '.\test.file'

$data = @'

$webserver = "intranet.hobbitcloud.com" 

$url = "http://" + $webserver 

$installer = "test.exe" 

$listConfig = "/s /v ""/qn REBOOT=ReallySuppress ADDLOCAL=Core,RTAV,ClientDriveRedirection,VmwVaudio""" 

'@

Set-Content -Path $fileName -Value $data


# The actual code

$installerFile = 'WhatEver.exe'

(Get-Content -Path $fileName) -replace '(^\$installer = )(.+)',"$('$1')""$installerFile"""


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Not 100% sure I got the question, but I give it a shot.

The following creates a sample file with the text '$installerFile' in there.

Next we set the content of variable $installerFile

Then it reads the file from before and replaces the placeholder string '$installerFile' with the content of variable $installerFile.

Something like this

# Preamble

$fileName = '.\test.file'

$data = @'

Sample file

with $installerFile in the content.

And one more line

'@

Set-Content -Path $fileName -Value $data


# The actual code

$installerFile = 'WhatEver'

(Get-Content -Path $fileName) -replace '\$installerFile',$installerFile


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Hi LucD, thanks for replying. I think it's nearly there, but not quite.

The contents of $fileName are:

$webserver = "intranet.hobbitcloud.com"

$url = "http://" + $webserver

$installer = "test.exe"

$listConfig = "/s /v ""/qn REBOOT=ReallySuppress ADDLOCAL=Core,RTAV,ClientDriveRedirection,VmwVaudio"""

When I run your code, I end up with:

Whatever = "test.exe"

The aim is to try and swap out the value of $installer in $fileName - so "Whatever" overwrites "test.exe"

Hope that makes sense (quite possibly doesn't)?

-Mark

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think I got what you are trying to do.
Luckily the -replace operator works with RegEx.
This replaces "test.exe" with "WhatEver.exe" on the $installer line

# Preamble

$fileName = '.\test.file'

$data = @'

$webserver = "intranet.hobbitcloud.com" 

$url = "http://" + $webserver 

$installer = "test.exe" 

$listConfig = "/s /v ""/qn REBOOT=ReallySuppress ADDLOCAL=Core,RTAV,ClientDriveRedirection,VmwVaudio""" 

'@

Set-Content -Path $fileName -Value $data


# The actual code

$installerFile = 'WhatEver.exe'

(Get-Content -Path $fileName) -replace '(^\$installer = )(.+)',"$('$1')""$installerFile"""


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Bang on, works perfectly!

Thank you Luc!

-Mark

0 Kudos