$LogonScriptsLocation = "\\$DomainFQDN\LogonScript\"

Hi,

I'd like to share a ps1 which contains variables. How do I replace all values which are between ### with blanks via Powershell?

F.e. myvariables.ps1 contains
###
$Fileserver = 'MyFileserver'
$SmtpServer = 'MySMTPServer"
###
$LogonScriptsLocation = "\\$DomainFQDN\LogonScript\"

I could work via get-content ... | replace content but howto replace the " ..." or ' ...' by " " correctly via Powershell?

Thanks for your input.
J.
janhoedtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

random0Commented:
Given that replacevars.ps1 contains

###
$myFileserver = 'MyFileserver'
$mySmtpServer = 'MySMTPServer'
###
$LogonScriptsLocation = '\\$DomainFQDN\LogonScript\'

Open in new window


Get-Content .\replacevars.ps1 | % {$_ -replace "^(\s*\$.*my.*\s*=\s*\').*(\')$", '$1$2'}

Open in new window


Would return

###
$myFileserver = ''
$mySmtpServer = ''
###
$LogonScriptsLocation = '\\$DomainFQDN\LogonScript\'

Open in new window


I believe you didn't want to blank all values, therefore I prefixed the "custom" variable names with my.


Hope that helps.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
janhoedtAuthor Commented:
Thanks! So how does it recognizes the ###? If I would replace it by something else like ### or ###variables###, would that ask a major change?
0
random0Commented:
The script does not recognize the ### but it does recognize variables which name is prefixed with my such as $myVar1 $myVar2 where $Var3 wouldn't match.

I believe it achieves the same goal as you first described.

Does my explanation cleared it up for you ?

Another approach could be to have the variables in a separate file which is dot sourced and that you clean the variables only in that file.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

random0Commented:
Also, if you really want to use the ### block delimiter, i am pretty sure there is a way to do this with a loop going through the array returned by get-content looking for beggining marker changing values until it finds the end marker.
0
janhoedtAuthor Commented:
My variables do not start with my, it was just an example ....
0
janhoedtAuthor Commented:
What bout working with clear-variable?

F.e.
$Test = 'Hello'
$test
Clear-Variable test
$test

If I could import the ps1, then clear the variables and write them back to another file. But how can I make sure only the variables are cleared from a specific file ...?
0
janhoedtAuthor Commented:
Hope below (content of my variables.ps1 file) clarifies what I'm doing (my variables.ps1 which is cleared out of  values for variables and which I can share publicly.
So how to remove them  by psscript, f.e.  $Global:Company = 'MyCompany'  should become  $Global:Company = '' etc

###
$Global:Company = ''
$Global:Organization = ''
$Global:SCCMSiteCode01 = ''
$Global:SCCMSiteServer01 = ''
$Global:PublicSMTPServer01 = 'SMTP' #SMTP server which does not need authentication
#Might also be variables which need = " "
###

$Global:DomainFQDN = $env:USERDNSDOMAIN
$Global:EmailSuffix = "@DomainFQDN"
$Global:MailFromAddress = "noreply$EmailSuffix"
$Global:MailToAddressDeploySoftware = "mymailaddress$EmailSuffix"

$Global:SCCMSiteServer01_FQDN =  ($SCCMSiteServer01 + '.' + $DomainFQDN)
$Global:PublicSMTPServer01_FQDN = ($PublicSMTPServer01 + '.' + $DomainFQDN)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.